Linked Lists: in This Section of Notes You Will Learn How To Create and Manage A Dynamic List
Linked Lists: in This Section of Notes You Will Learn How To Create and Manage A Dynamic List
Arrays
Easy to use but suffer from a number of drawbacks:
1) Fixed size
2) Adding/Deleting elements can be awkward
123
125
135
155
161
165
166
167
167
169
177
178
123
125
135
155
161
166
167
167
169
177
178
Node
Linked
List
NodePointer = ^ Node;
Node = record
data : Client;
nextPointer : NodePointer;
end; (* Declaration of record Node *)
NodePointer = ^ Node;
Node = record
Declaring the
type of node
data : Client;
nextPointer : NodePointer;
end; (* Declaration of record Node *)
Example:
procedure createNewList (var tamjClientList : NodePointer);
begin
tamjClientList := NIL;
end;
Example:
procedure displayList (tamjClientList : NodePointer);
var
currentNode : NodePointer;
begin
currentNode := tamjClientList;
writeln('CLIENT LIST':20);
Main variables:
1. There are two pointers to the list:
a. Current pointer – traverses the list from beginning to end.
b. Previous to first pointer – points to the node that occurs just prior
to the first successful match.
Note: The second pointer is not used when the user only wants to
search the list. It is needed when the person wishes to erase a node
from the list. Since the erase procedure calls the search procedure, it
needs a pointer to the node prior to the one to be deleted.
2. A Boolean that indicates the status of the search.
Linked Lists in Pascal James Tam
Main variables:
1. A flag that indicates the status of the search. If the search was
successful then it was true that the item was found (flag will be set to
true). If the search was a failure then it was false that item was found
(flag will be set to false).
2. A pointer that points to the node just prior to the one to be deleted. If
the flag was set to true then the pointer contains the address of the
previous node. If the pointer is NIL then the node to be deleted is the
first node (nothing is previous to this node so there is no address). If
the the pointer is not NIL then it contains the address of the node to
be deleted.
3. A temporary pointer that points to the node to be deleted. It is needed
so that the program can retain a reference to this node and free up the
memory allocated for it.
Linked Lists in Pascal James Tam
Example:
procedure erase (var tamjClientList : NodePointer);
var
desiredName : NameArray;
previousFirst : NodePointer;
temp : NodePointer;
isFound : boolean;
begin
write('Enter last name of client to delete: ');
readln(desiredName);
search (tamjClientList, desiredName, isFound, previousFirst);