- •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 249
Making Data Structures with Linked Lists
The simplest linked list is a single-linked list, in which each node contains data and one pointer that points to another node in the linked list (refer to Figure 18-2).
The trouble with a single-linked list is that each node points only to the next node. If you want to find the previous node, you can’t. In Figure 18-2, for example, the middle node (containing the name Dick) points only to the node containing the name Harry. But you can’t tell which name appears before Dick. In this case, an array is much simpler, because an array can identify which data appears before and which data appears after a specific array location.
You can arrange linked lists in a variety of ways to create different types of data structures. Because data structures do nothing more than hold data, choosing the right data structure can make your program easier to write.
(And choosing the wrong data structure can make your program harder to write.)
A personal organizer program that stores names and addresses, for example, may use a linked list of records. The program can expand or shrink the linked list, depending on the number of names and addresses the user stores. The type of program that you’re writing can determine the type of data structure you need (or should use). That’s what makes linked lists so powerful — they can create a variety of different data structures.
Don’t worry about the details of creating different data structures with linked lists. Just remain aware that different data structures exist — and you usually run into these different data structures later on if you continue learning about computer programming.
Double-linked lists
The problem with a single-linked list is that each node can identify only the next node but not the preceding node. To solve this problem, you can create a double-linked list, in which each node contains data and two pointers. One pointer points to the next node in the linked list, and the second pointer points to the previous node in the linked list, as shown in Figure 18-7.
250 Part IV: Dealing with Data Structures
Figure 18-7: |
|
|
|
A double- |
|
|
|
linked list |
|
|
|
uses two |
|
|
|
pointers to |
Tom |
Dick |
Harry |
point at |
nil |
|
|
nodes that |
|
|
|
|
|
nil |
|
appear |
|
|
|
|
|
|
|
before and |
|
|
|
after each |
|
|
|
linked node. |
|
|
|
A personal-organizer program is likely to use a double-linked list to enable the user to scroll forward and backward through the linked list to view all the names and addresses that you store in the personal-organizer program.
Creating a double-linked list means writing instructions to manage twice as many pointers, which essentially doubles the chances that you may leave a pointer dangling (that is, pointing to a nonexistent node) or pointing to the wrong node. In either case, incorrect pointers can cause subtle, yet serious bugs in your program, so use pointers sparingly.
Circular-linked lists
A typical double-linked list looks like a long rectangle with a beginning and an end (refer to Figure 18-7). But instead of creating an artificial end and beginning, you can link both ends of a single-linked list or a double-linked list to create a circular-linked list, as shown in Figure 18-8.
Figure 18-8: |
|
|
|
|
A circular- |
Tom |
Dick |
Harry |
|
linked list |
||||
|
|
|
||
displays no |
|
|
|
|
beginning or |
|
|
|
|
ending. |
|
|
|
A circular-linked list may prove useful for a presentation program that needs to display information continuously, such as a presentation in a museum kiosk. In this case, the presentation program may display information one screen at a time and then begin again after it reaches the end of the presentation.
Chapter 18: Linked Lists and Pointers 251
Stacks
A stack is a special single-linked list that enables you to add and remove nodes only at the beginning of the linked list (see Figure 18-9). In the real world, the most common implementation of a stack occurs as you stack plates. If you want to store a new plate, you simply add it to the top of the stack. If you want to remove a plate, you take it off the top of the stack as well.
One common use for stacks is for calculating formulas by using a method known as Reverse Polish Notation (RPN), which is a method of calculation that many Hewlett-Packard calculators use. If you want to calculate the formula (1 + 2) * 3 by using RPN, you type the following:
1 2 + 3 *
Formula: (1 + 2) * 3
Reverse Polish Notation: 1 2 + 3 *
|
|
|
1 |
|
2 |
|
3 |
|
3 |
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
|
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
||
Figure 18-9: |
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|||
A stack |
|
|
|
|
|
|
|
|
|
||
adds data to |
|
|
|
|
|
|
|
|
|
||
and |
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|||
removes |
|
|
|
|
|
|
|
|
|
||
data from |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
the top of a |
|
|
|
|
|
|
|
|
|
||
stack (linked |
|
|
|
|
|
|
|
|
|
||
Step 1 |
|
Step 2 |
|
Step 3 |
|
Step 4 |
Step 5 |
||||
list). |
|
|
|
||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Figure 18-9 shows how a stack stores the five steps that you need to calculate the formula (1 + 2) * 3 by using Reverse Polish Notation. Here’s how it works:
1.The first step pushes the number 1 to the top of the stack.
2.The second step pushes the number 2 to the top of the stack and pushes the number 1 underneath.
3.The third step pops the number 2 off the stack, pops the number 1 off the stack, and adds the two numbers together to get the number 3. Then it stores the number 3 back on top of the stack.
4.The fourth step pushes the number 3 (from the formula) to the top of the stack and pushes the calculated value of 3 farther down the stack.
252 Part IV: Dealing with Data Structures
5.The fifth step pops the first number 3 off the stack, pops the second number 3 off the stack, and multiplies the two number 3s to get the number 9. Then it stores the number 9 back on top of the stack.
Stacks are often referred to as LIFO (which stands for Last In, First Out) linked lists. This term means that the last data that you store in a stack is the first data that you remove from the stack (just as in stacking and removing plates).
Don’t worry if you don’t understand the logic behind Reverse Polish Notation. Just understand how stacks work and leave Reverse Polish Notation for the engineers at Hewlett-Packard.
Queues
A queue is a special linked list that follows two rules. The first rule is that you can add nodes only to the end of the linked list. The second rule is that you can remove nodes only from the beginning of the linked list.
You often refer to queues as FIFO (which stands for First In, First Out) linked lists. This term means that the first data that you store in the queue is the first data that you remove from the queue. A queue mimics a line of people waiting to board an airplane or see a science-fiction movie. In both cases, the first person (data) put in the queue is the first person (data) who leaves it, as shown in Figure 18-10.
|
|
|
Step 1 |
|
|
Front |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tom |
Dick |
Harry |
John |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Step 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tom |
Dick |
Harry |
|
|
|
|
|
|
|
|
|
|
Figure 18-10: |
|
|
|
|
|
|
|
|
|
|
Step 3 |
|
|
||||
A queue |
|
|
|
|
||||
stores new |
|
|
Tom |
Dick |
|
|||
data at the |
|
|
|
|
|
|||
|
|
|
|
|
||||
end and |
|
|
|
|
|
|||
|
|
|
|
|||||
removes the |
|
Step 4 |
|
|
|
|||
oldest data |
|
|
|
|
|
|
||
|
|
|
|
|
|
|||
|
|
|
|
|
||||
from the |
|
|
Mike |
Tom |
Dick |
|
||
front. |
|
|
|
|
|
|
||
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|