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

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.