0% found this document useful (0 votes)
2 views

DS 04 Linked List

Linked List ppt

Uploaded by

kinshahra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DS 04 Linked List

Linked List ppt

Uploaded by

kinshahra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Data Structure

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

current 0x12C 0x11C

head 0x130 0x10C


Linked List Operations
head Data = 9
push_front (int Data){
node *newNode ;
// add new node NULL 9 NULL
newNode = new node ;
newNode -> data = Data ; newNode
newNode -> next = head ;
head = newNode ; head
}

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

// skip to desired portion head


for ( int i = 0 ; i < loc ; i++ ){
current = current -> next ; 2 6 1 NULL
// if reached at end of linked list
if ( current == NULL ){
current
cout << "\nThere are less than "
<< loc << " elements in list"
<< endl ;
return ;
} 9
}
newNode = new node ; // insert new node newNode
newNode -> data = Data ;
newNode -> next = current -> next ;
current -> next = newNode ;
}
Linked List Operations
void del ( int Data ) { Data = 1
node *previous, *current ;
current = head ;
while ( current != NULL ){
if ( current -> data == Data ){ head
if ( current == head )
head = current -> next ; 1 NULL
else
previous->next = current->next;
previous current
delete current ;
return ;
}
else{
previous = current ;
current = current -> next ;
}
}
cout << "\n\nElement " << Data << " not found" ;
}
Linked List Operations
void del ( int Data ) { Data = 1
node *previous, *current ;
current = head ;
while ( current != NULL ){
if ( current -> data == Data ){
if ( current == head )
head = current -> next ; previous
else
head
previous->next = current->next;
delete current ;
2 6 1 9 NULL
return ;
}
else{ current
previous = current ; previous
current = current -> next ;
}
}
cout << "\n\nElement " << Data << " not found" ;
}
Example 1: Linked List Linked_List.cpp

#include <iostream> Linked_List :: Linked_List( ){


using namespace std; head = NULL ;
class Linked_List{ }
private :
struct node{ // adds node at the end of linked list
int data ; void Linked_List :: push_back ( int Data )
node * next ; {
} *head ; node *current, *newNode ;
public : // list is empty then create first node
Linked_List ( ) ; if ( head == NULL ){
void push_back ( int Data ) ; head = new node ;
void push_front ( int Data ) ; head -> data = Data ;
void addafter ( int loc, int Data ) ; head -> next = NULL ;
void display ( ) ; }
int count ( ) ; else{ // go to last node
void del ( int num ) ; current = head ;
~Linked_List( ) ; while ( current -> next != NULL )
} ; current = current -> next ;
// add node at the end
1 8 2
Example 1: Linked List
newNode = new node ; node *current, *newNode ;
newNode -> data = Data ; current = head ;
newNode -> next = NULL ; // skip to desired portion
current -> next = newNode ; for ( int i = 0 ; i < loc ; i++ ){
} current = current -> next ;
} // if reached at end of linked list
// adds a node at the beginning if ( current == NULL ){
void Linked_List :: push_front (int Data) cout << "\nThere are less than "
{ << loc << " elements in list"
node *newNode ; << endl ;
// add new node return ;
newNode = new node ; }
newNode -> data = Data ; }
newNode -> next = head ; newNode = new node ; // insert new node
head = newNode ; newNode -> data = num ;
} newNode -> next = current -> next ;
// adds node after numbers of nodes current -> next = newNode ;
void Linked_List::addafter(int loc, }
int num){
3 9 4
Example 1: Linked List
// displays the contents of the link list // deletes the specified node
void Linked_List :: display( ){ void Linked_List :: del ( int Data ) {
node *current = head ; node *previous, *current ;
cout << endl ; current = head ;
// traverse the entire linked list while ( current != NULL ){
while ( current != NULL ){ if ( current -> data == Data ){
cout << current -> data << " " ; // if node to be deleted is the
current = current -> next ; // first node in the linked list
} if ( current == head )
} head = current -> next ;
// counts the number of nodes present else
int Linked_List :: count( ){ previous->next = current->next;
node *current = head ; int c = 0 ; // free memory occupied by node
// traverse the entire linked list delete current ;
while ( current != NULL ){ return ;
current = current -> next ; }
c++ ; // traverse the linked list till
} // the last node is reached
return c ; }
5 10 6
Example 1: Linked List
else{ int main( ) {
previous = current ; Linked_List l ;
// go to the next node l.push_back (14) ; l.push_back (30) ;
current = current -> next ; l.push_back (25) ; l.push_back (42) ;
} l.push_back (17) ;
} cout <<"\nData in the linked list: ";
cout << "\n\nElement " << Data l.display( ) ;
<< " not found" ; l.push_front (999) ; l.push_front
} (888) ;
l.push_front (777) ;
// deallocates memory cout << "\n\nData in the linked list "
Linked_List :: ~Linked_List( ){ " after addition at the "
while ( head != NULL ){ " beginning: " ;
node *current = head; l.display( ) ;
head = head -> next ; l.addafter (7, 0); l.addafter (2,1);
delete current ; l.addafter (5,99);
} cout << "\n\nData in the linked list"
} " after addition at given"
" position: " ;
7 11 8
Example 1: Linked List
l.display( ) ;

cout <<"\nNo. of elements in the "


" linked list " << l.count( );

l.del (99) ;
l.del ( 1) ;
l.del (10) ;

cout << "\n\nData in the linked list"


" after deletion: " ;
l.display( ) ;

cout <<"\nNo. of elements in the"


" linked list: " << l.count();
system("PAUSE");
return 0;
}

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

 Moving forward in a singly-linked list is easy but moving backwards is not so


easy.
 Moving backwards requires traversing the list from the start until the node
whose next pointer points to current node.
Applications of Linked List
 Dynamic memory allocation : We use linked list of free blocks.
 Implementation of stacks and queues
 Maintaining directory of names.
 Performing arithmetic operations on long integers
 Addition and Multiplication of Polynomials.
 Implementation of Graphs. (Adjacency list representation of Graph).
 Representing sparse matrices.
 For separate chaining in Hashtables.
 Undo functionality in Photoshop or Word . Linked list of states.
 Graph Plotting Application.
Circular Linked List
 In single linked list, every node points to its next node in the sequence and the
last node points to NULL.
 But in circular linked list, every node points to its next node in the sequence
but the last node points to the first node in the list.
Applications of Circular Linked List
 The real life application where the circular linked list is used is our Personal
Computers, where multiple applications are running. All the running
applications are kept in a circular linked list and the OS gives a fixed time slot
to all for running. The Operating System keeps on iterating over the linked list
until all the applications are completed.
 Another example can be Multiplayer games. All the Players are kept in a
Circular Linked List and the pointer keeps on moving forward as a player's
chance ends.
 Circular Linked List can also be used to create Circular Queue. In a Queue we
have to keep two pointers, FRONT and REAR in memory all the time, where as
in Circular Linked List, only one pointer is required.

You might also like