Objectives
Learn about linked lists Become aware of the basic properties of linked lists Explore the insertion and deletion operations on linked lists Discover how to build and manipulate a linked list
Objectives (contd.)
Learn how to construct a doubly linked list Discover how to use the STL container list Learn about linked lists with header and trailer nodes Become aware of circular linked lists
Lists
1 7 9 17 33 47 57
To delete or to add an item you need to shift the contents of the array to right or to left...
Linked Lists
Collection of components (nodes)
Every node (except last)
Contains address of the next node
Node components
Data: stores relevant information Link: stores address
TABLE 5-1 Values of head and some of the nodes of the linked list in Figure 5-4
Example
info head 5 77 12 Null Link pointer
node
nodeType *head;
(*head) . info head info (*(*head) . link). info head link info
5 5 77 77
headlinklink=new nodeType;
headlinklinkinfo=12; headlinklinklink=NULL;
current = current->link;
Copies value of current->link (2800) into current
10
11
head 5 77 12 Null
current
13
14
p head 3 4 5 8
p New node N 20
head 5 77 12 Null
p New node N 20
head 5 p q
Delete 77
77 12 Null
head 5 12 Null
Backward
New node always inserted at the beginning of the list See example on page 277 See function buildListBackward on page 278
20
head 5
20
head 5 12
Forward
20
Null
23
24
{linkedListIterator temp(first); return temp;} {linkedListIterator temp(NULL); return temp;} {first=NULL; last=NULL; count=0;} {first= NULL; copyList();} {destroyList();}
25
26
Length of a list
Number of nodes stored in the variable count Function length
Returns value of variable count
27
15
37
Then continue..
29
current=current->link;}
32
C L first 3 8 5 1 4
last
ListObj
. copyList(L);
33
34
35
36
Iterator
Object producing each element of a container One element at a time
39
class unorderedLinkedList : public linkedListType { bool search(int searchItem) void insertFirst(int item); void insertlast(int item); void deleteNode(int deleteItem); //later on };
40
41
bool search(int searchItem) current { nodeType *current; bool found =false; current=first; while(current !=NULL && ! found) { if (current ->info == searchItem) found =true; else current=current->link; } return found; }
last
42
43
newNode
45
The list is empty case2 The node is nonempty and the node to be deleted is the first node case3 The node is nonempty and the node to be deleted is not the first node, it is somewhere in the list case4 The node to be deleted is not in the list
See pseudocode on page 295 See definition of function deleteNode on page 297
46
first 3 p Delete p 8 5 1 4
last
3
p
8
q
49
50
Ordered linked list elements are arranged using some ordering criteria
Assume elements of an ordered linked list arranged in ascending order
53
54
40
11
40
Function insertLast
Inserts new item at the proper place
57
58
first 3 p Delete p 4 5 8
last 11
3
p
5
q
11
14
61
Null
65
isEmptyList
Returns true if the list empty
Otherwise returns false
66
69
Insert a node
N
first last count 1 Null Null Null 3 Null
N=new nodeType;
N->info= 3 N->back=NULL N->next=NULL;
First=N;
Last=N; Count++;
70
Insert a node
nodeType *N;
N=new nodeType;
N->info= 2
N->back=NULL
Insert a node
20
nodeType *N;
N=new nodeType;
N->info= 20 N->Next=NULL
Insert a node
q 8
q->back=N
p->next=N; count++
73
74
our cases
75
our cases
our cases
q->next->back= p
delete q; count;
77
78
79
80
count 0
82
Summary
Linked list topics
Traversal, searching, inserting, deleting
Linked list as an ADT Ordered linked lists Doubly linked lists STL sequence container list Linked lists with header and trailer nodes Circular linked lists
85