
- •About the Author
- •Dedication
- •Author’s Acknowledgments
- •Contents at a Glance
- •Table of Contents
- •Introduction
- •Who Should Buy This Book
- •How This Book Is Organized
- •Part I: Programming a Computer
- •Part II: Learning Programming with Liberty BASIC
- •Part III: Advanced Programming with Liberty BASIC
- •Part VI: Internet Programming
- •Part VII: The Part of Tens
- •How to Use This Book
- •Foolish assumptions
- •Icons used in this book
- •Why Learn Computer Programming?
- •How Does a Computer Program Work?
- •What Do I Need to Know to Program a Computer?
- •The joy of assembly language
- •C: The portable assembler
- •High-level programming languages
- •Database programming languages
- •Scripting programming languages
- •The program’s users
- •The target computer
- •Prototyping
- •Choosing a programming language
- •Defining how the program should work
- •The Life Cycle of a Typical Program
- •The development cycle
- •The maintenance cycle
- •The upgrade cycle
- •Writing Programs in an Editor
- •Using a Compiler or an Interpreter
- •Compilers
- •Interpreters
- •P-code: A combination compiler and interpreter
- •So what do I use?
- •Squashing Bugs with a Debugger
- •Writing a Help File
- •Creating an Installation Program
- •Why Learn Liberty BASIC?
- •Liberty BASIC is easy
- •Liberty BASIC runs on Windows
- •You can start using Liberty BASIC today
- •Installing Liberty BASIC
- •Loading Liberty BASIC
- •Your First Liberty BASIC Program
- •Running a Liberty BASIC program
- •Saving a Liberty BASIC program
- •Getting Help Using Liberty BASIC
- •Exiting Liberty BASIC
- •Getting input
- •Displaying output
- •Sending Data to the Printer
- •Storing Data in Variables
- •Creating a variable
- •Assigning a value to a variable
- •Declaring your variables
- •Using Constants
- •Commenting Your Code
- •Using variables
- •Working with precedence
- •Using parentheses
- •Manipulating Strings
- •Declaring variables as strings
- •Smashing strings together
- •Counting the length of a string
- •Playing with UPPERCASE and lowercase
- •Trimming the front and back of a string
- •Inserting spaces
- •Yanking characters out of a string
- •Looking for a string inside another string
- •Using Boolean Expressions
- •Using variables in Boolean expressions
- •Using Boolean operators
- •Exploring IF THEN Statements
- •IF THEN ELSE statements
- •Working with SELECT CASE Statements
- •Checking a range of values
- •Checking a relational operator
- •Boolean expression inside the loop
- •Looping a Fixed Number of Times
- •Counting with different numbers
- •Counting in increments
- •Anatomy of a Computer Bug
- •Syntax Errors
- •Fun with Logic Errors
- •Stepping line by line
- •Tracing through your program
- •Designing a Window
- •Creating a new window
- •Defining the size and location of a window
- •Adding color to a window
- •Putting Controls in a Window
- •Creating a command button
- •Displaying text
- •Creating a check box
- •Creating a radio button
- •Creating text boxes
- •Creating list boxes
- •Creating combo boxes
- •Creating group boxes
- •Storing Stuff in Text Files
- •Creating a new text file
- •Putting stuff in a text file
- •Adding new stuff to an existing text file
- •Retrieving data from a text file
- •Creating a new binary file
- •Saving stuff in a binary file
- •Changing stuff in a binary file
- •Retrieving stuff from a binary file
- •Creating a Graphics Control
- •Using Turtle Graphics
- •Defining line thickness
- •Defining line colors
- •Drawing Circles
- •Drawing Boxes
- •Displaying Text
- •Making Sounds
- •Making a beeping noise
- •Playing WAV files
- •Passing Data by Value or by Reference
- •Using Functions
- •Defining a function
- •Passing data to a function
- •Calling a function
- •Exiting prematurely from a function
- •Using Subroutines
- •Defining a subroutine
- •Passing data to a subroutine
- •Calling a subroutine
- •Exiting prematurely from a subroutine
- •Writing Modular Programs
- •Introducing Structured Programming
- •Sequential instructions
- •Branching instructions
- •Looping instructions
- •Putting structured programming into practice
- •The Problem with Software
- •Ways to Make Programming Easier
- •Breaking Programs into Objects
- •How to use objects
- •How to create an object
- •Creating an object
- •Starting with a Pointer
- •Defining the parts of a linked list
- •Creating a linked list
- •Managing a linked list
- •Making Data Structures with Linked Lists
- •Stacks
- •Queues
- •Trees
- •Graphs
- •Creating a Record
- •Manipulating Data in Records
- •Storing data in a record
- •Retrieving data from a record
- •Using Records with Arrays
- •Making an Array
- •Making a Multidimensional Array
- •Creating Dynamic Arrays
- •Insertion Sort
- •Bubble Sort
- •Shell Sort
- •Quicksort
- •Sorting Algorithms
- •Searching Sequentially
- •Performing a Binary Search
- •Hashing
- •Searching by using a hash function
- •Dealing with collisions
- •Picking a Searching Algorithm
- •Choosing the Right Data Structure
- •Choosing the Right Algorithm
- •Put the condition most likely to be false first
- •Put the condition most likely to be true first
- •Clean out your loops
- •Use the correct data types
- •Using a Faster Language
- •Optimizing Your Compiler
- •Programming Computer Games
- •Creating Computer Animation
- •Making (And Breaking) Encryption
- •Internet Programming
- •Fighting Computer Viruses and Worms
- •Hacking for Hire
- •Participating in an Open-Source Project
- •Niche-Market Programming
- •Teaching Others about Computers
- •Selling Your Own Software
- •Trying Commercial Compilers
- •Windows programming
- •Macintosh and Palm OS programming
- •Linux programming
- •Testing the Shareware and
- •BASIC compilers
- •C/C++ and Java compilers
- •Pascal compilers
- •Using a Proprietary Language
- •HyperCard
- •Revolution
- •PowerBuilder
- •Shopping by Mail Order
- •Getting Your Hands on Source Code
- •Joining a Local User Group
- •Frequenting Usenet Newsgroups
- •Playing Core War
- •Programming a Battling Robot
- •Toying with Lego Mindstorms
- •Index
- •End-User License Agreement

Chapter 18: Linked Lists and Pointers 245
The preceding Pascal program tells the computer to perform the following tasks:
1.The first line tells the computer, “This is the beginning of my program that I call LinkedLists.”
2.The second line contains just the word TYPE, which tells the computer, “Get ready to define a pointer and a record.”
3.The third line defines the data type PointerName, which points to any record by the name of RecordType.
4.The fourth line defines the name of the record RecordType.
5.The fifth line tells the computer, “Create the variable FullName, which can hold up to 15 characters.”
6.The sixth line tells the computer, “Create the variable Address, which can hold up to 25 characters.”
7.The seventh line tells the computer, “Create the variable Next, which can point to another record that RecordType defines.”
8.The eighth line tells the computer, “This line is the end of the record definition RecordType.”
9.The ninth line contains just the word VAR, which tells the computer, “Get ready to define one or more variables.”
10.The tenth line tells the computer, “Create the variable Node, which represents a pointer to a record that RecordType defines.” (This variable pointer defines a single node in your linked list.)
Don’t worry if linked lists look confusing at this point. The remaining figures in this chapter clarify how linked lists work.
Creating a linked list
After you define a record to hold your data, a pointer to point to each record, and a variable to represent each node in your linked list, you still need to create your linked list. You need to follow three additional steps to create a linked list. The following steps are as shown in Figure 18-4:
1.Create a node.
2.Store data in that node.
3.Rearrange pointer to insert a new node in the beginning, middle, or end of the linked list.

246 Part IV: Dealing with Data Structures
Figure 18-4:
The three steps to follow to create a linked list.
Step 1: Create a new node.
Tom
Step 2: Stuff data into the new node.
Step 3: Rearrange your pointers to insert the new node in the beginning, middle, or end of your linked list.
Tom |
Dick |
Harry |
nil
To see how a Pascal program creates a node and stores data in it, take a look at the following bit of code:
PROGRAM LinkedLists;
TYPE
PointerName = ^RecordType;
RecordType = RECORD
FullName : string[15];
Address : string[25];
Next : PointerName;
END;
VAR
Node : PointerName;
BEGIN
New(Node);
Node^.FullName := ‘Jonathan Blake’;
Node^.Address := ‘837 Dead-End Avenue’;
Node^.Next := nil;
END.

Chapter 18: Linked Lists and Pointers 247
Starting from the word BEGIN, this program tells the computer to do the following:
If you want to understand the code that appears before the line containing the BEGIN command, refer to the previous section “Defining the parts of a linked list.”
1.The BEGIN line tells the computer, “This line is the start of the instructions to follow.”
2.The second line creates a new node. At this point, the node contains nothing.
3.The third line stores the name Jonathan Blake in the FullName variable of the node record.
4.The fourth line stores the address 837 Dead-End Avenue in the Address variable of the node record.
5.The fifth line stores the value nil in the Next pointer. A nil value means that the pointer doesn’t point to anything. Using a nil value keeps the pointer from accidentally pointing to another part of the computer’s memory. If you create a second node, you want the value of Node^.Next to point to this second node.
Managing a linked list
If you store data in an array and you delete a chunk of data in the middle of the array, you wind up with an empty spot in your array. Although that particular location in the array stores nothing, the computer must still allocate memory for that empty part of the array. Even worse, now your array contains an empty gap that you must skip over to find the next chunk of data in the array, as shown in Figure 18-5.
|
Tom |
Dick |
Harry |
John |
Figure 18-5: |
|
|
|
|
Deleting |
|
|
|
|
data in the |
Tom |
|
Harry |
John |
middle of an |
|
|
|
|
array leaves |
|
|
|
|
an empty |
|
Empty array location |
|
|
gap. |
|
|
|

248 Part IV: Dealing with Data Structures
Deleting data from a linked list is easy — just follow these two steps:
1.Rearrange the pointers of the nodes so that they ignore the node containing the data that you want to erase.
2.Delete the node containing the data that you want to erase.
Figure 18-6 illustrates the process of deleting data in a linked list.
Writing the actual instructions to tell the computer to rearrange pointers and delete a node is often fairly lengthy. So although arrays can prove inefficient in terms of memory storage, arrays are much simpler to use and manage. On the other hand, linked lists are more efficient in storing data in memory, but they can become really troublesome to manage because you must write many instructions just to accomplish something as seemingly simple as rearranging a pointer.
|
Tom |
Dick |
Harry |
|
|
|
nil |
|
Tom |
Dick |
Harry |
|
|
|
nil |
Figure 18-6: |
|
|
|
Deleting |
|
|
|
data in a |
|
|
|
linked list |
Tom |
Dick |
Harry |
saves |
|
|
|
memory by |
|
|
nil |
avoiding |
|
|
|
|
|
|
|
empty gaps. |
|
|
|