- •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 253
The following steps (along with Figure 18-10) explain how a queue stores and removes data:
1.The first step shows a queue in which the name John is first, the name Harry is second, the name Dick is third, and the name Tom is fourth and last.
2.The second step removes John from the queue. Now all the remaining names move closer to the front of the queue.
3.The third step removes Harry from the queue. All the remaining names move closer to the front of the queue.
4.The fourth step adds a new name, Mike, to the queue. Because Mike is the newest data adding itself to the queue, its place is at the back of the queue.
Trees
Linked lists don’t always need to resemble a linear or circular shape. The most common nonlinear shape for a linked list is a tree, in which one node (the root) represents the top of the tree with the rest of the nodes (leaves) falling underneath the root, as shown in Figure 18-11.
Root |
Level 0 |
|
|
Level 1 |
|
|
Level 2 |
|
Figure 18-11: |
|
|
A tree is a |
|
|
nonlinear |
Level 3 |
|
linked list. |
||
|
In a tree, a node can point to zero or more nodes. For simplicity, many programmers use a special tree known as a binary tree, in which each node can link only to zero, one, or two other nodes.
254 Part IV: Dealing with Data Structures
Programmers often use trees to mimic artificial intelligence in programs, such as in chess-playing programs. A chess-playing program may use a tree in which the root represents a possible move for the first player and the leaf nodes underneath (at level 1) represent potential moves that the second player can make. Then the leaf nodes at level 2 represent potential moves for the first player, and the leaf nodes at level 3 represent potential moves for the second player, and so on.
Graphs
A graph is a linked list in which each node can point to one or more nodes without regard to mimicking a certain shape, such as a list. Figure 18-12 shows what a typical graph looks like.
Figure 18-12:
A graph enables nodes to link to one another any way that you choose.
Programmers often use graphs in something known as a neural network — a special program that can mimic the way the brain thinks. In a neural network, each node represents a neuron and the links between them represent the synapses linking the neurons.
Graphs, trees, queues, stacks, circular-linked lists, and double-linked lists are just different ways to arrange and use a linked list. The more complicated your programs become, the more you’re likely to need these advanced data structures.
Chapter 19
Playing with Object-Oriented Programming
In This Chapter
Understanding the problem with software
Making programming easier
Breaking programs into objects
Choosing an object-oriented language
Object-oriented programming is the latest fad (or, to use correct programming lingo, the latest methodology) for pursuing the following Holy
Trinity of characteristics that programmers want for their software:
Simple and fast to create
Easy to understand and modify
Reliable and error-free
People often abbreviate object-oriented programming as OOP, as in “Oops — I don’t think that object-oriented programming alone can magically make programming any easier.”
Liberty BASIC isn’t an object-oriented language, which means that you can’t experiment with using objects in Liberty BASIC. Although most versions of BASIC don’t support objects, Microsoft has tortured the BASIC dialect in Visual Basic into offering object-oriented programming features. If you want to find out more about object-oriented programming using BASIC, pick up a copy of Visual Basic and a copy of Visual Basic.NET For Dummies (written by yours truly and published by Wiley Publishing) today. To show how objectoriented programming works, the examples in this chapter use C++.