DS 04 Linked List
DS 04 Linked List
Video Lecture
Lecture No. 4
Linked List
Engr. Rashid Farid Chishti
http://youtube.com/rfchishti
http://sites.google.com/site/chis
hti International Islamic University H-10, Islamabad, Pakistan
Linked List
head 0x100 6
0x108 0x11C
2 6 8 7 1 NULL 0x10C 2
0x110 0x100
current 0x114 7
0x118 0x100
8 7 1
6 0x11C 8
0x120 0x114
2
0x124 1
0x128 0x000
2 6 1 NULL
newNode
Linked List Operations
void push_back ( int Data ){ Data = 9
node *current, *newNode ;
// list is empty then create first node
if ( head == NULL ){ head
head = new node ;
head -> data = Data ; NULL NULL
9
head -> next = NULL ;
}
head
else{ // go to last node
current = head ;
while ( current -> next != NULL ) 2 6 1 NULL
current = current -> next ;
newNode = new node ; current
newNode -> data = Data ;
newNode -> next = NULL ; newNode
current -> next = newNode ;
} NULL
9
}
Linked List Operations
void addafter(int loc, int Data){ loc = 1 Data = 9
node *current = head, *newNode ; i = 0
1
l.del (99) ;
l.del ( 1) ;
l.del (10) ;
9 12 10
Example 1: Linked List
Analysis of Linked List
push_front()
we simply insert the new node after the current node. So add is a one-step operation.
push_back()
we have to traverse the entire list to insert the new node at the end of Linked List.
del()
We first search the node to be deleted.
worst-case: may have to search the entire list