Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
109
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать

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++.