SlideShare a Scribd company logo
Data Structure
Lecture No. 4
Linked List
Engr. Rashid Farid Chishti
http://youtube.com/rfchishti
http://sites.google.com/site/chishti
International Islamic University H-10, Islamabad, Pakistan
Video Lecture
Linked List
head
2 6 8 7
current
1 NULL
0x100 6
0x108
2
0x10C
0x110
0x114 7
0x118
0x11C 8
0x120
1
0x124
0x128 0x000
0x12C 0x11C
0x130
head
current
2
6
8 7 1
0x10C
0x100
0x11C
0x114
0x100
push_front (int Data){
node *newNode ;
// add new node
newNode = new node ;
newNode -> data = Data ;
newNode -> next = head ;
head = newNode ;
}
Linked List Operations
head
2 6 1 NULL
head
NULL
NULL 9
newNode
newNode
9
Data = 9
void push_back ( int Data ){
node *current, *newNode ;
// list is empty then create first node
if ( head == NULL ){
head = new node ;
head -> data = Data ;
head -> next = NULL ;
}
else{ // go to last node
current = head ;
while ( current -> next != NULL )
current = current -> next ;
newNode = new node ;
newNode -> data = Data ;
newNode -> next = NULL ;
current -> next = newNode ;
}
}
Linked List Operations
head
current
2 6 1 NULL
head
newNode
NULL
NULL
9
9
NULL
Data = 9
void addafter(int loc, int Data){
node *current = head, *newNode ;
// skip to desired portion
for ( int i = 0 ; i < loc ; i++ ){
current = current -> next ;
// if reached at end of linked list
if ( current == NULL ){
cout << "nThere are less than "
<< loc << " elements in list"
<< endl ;
return ;
}
}
newNode = new node ; // insert new node
newNode -> data = Data ;
newNode -> next = current -> next ;
current -> next = newNode ;
}
Linked List Operations
current
2 6 1 NULL
head
9
newNode
i = 0
1
loc = 1 Data = 9
void del ( int Data ) {
node *previous, *current ;
current = head ;
while ( current != NULL ){
if ( current -> data == Data ){
if ( current == head )
head = current -> next ;
else
previous->next = current->next;
delete current ;
return ;
}
else{
previous = current ;
current = current -> next ;
}
}
cout << "nnElement " << Data << " not found" ;
}
Linked List Operations
head
NULL
1
current
previous
Data = 1
void del ( int Data ) {
node *previous, *current ;
current = head ;
while ( current != NULL ){
if ( current -> data == Data ){
if ( current == head )
head = current -> next ;
else
previous->next = current->next;
delete current ;
return ;
}
else{
previous = current ;
current = current -> next ;
}
}
cout << "nnElement " << Data << " not found" ;
}
Linked List Operations
current
2 6
head
9 NULL
1
previous
previous
Data = 1
#include <iostream>
using namespace std;
class Linked_List{
private :
struct node{
int data ;
node * next ;
} *head ;
public :
Linked_List ( ) ;
void push_back ( int Data ) ;
void push_front ( int Data ) ;
void addafter ( int loc, int Data ) ;
void display ( ) ;
int count ( ) ;
void del ( int num ) ;
~Linked_List( ) ;
} ;
8
Example 1: Linked List
Linked_List :: Linked_List( ){
head = NULL ;
}
// adds node at the end of linked list
void Linked_List :: push_back ( int Data ){
node *current, *newNode ;
// list is empty then create first node
if ( head == NULL ){
head = new node ;
head -> data = Data ;
head -> next = NULL ;
}
else{ // go to last node
current = head ;
while ( current -> next != NULL )
current = current -> next ;
// add node at the end
1 2
newNode = new node ;
newNode -> data = Data ;
newNode -> next = NULL ;
current -> next = newNode ;
}
}
// adds a node at the beginning
void Linked_List :: push_front (int Data){
node *newNode ;
// add new node
newNode = new node ;
newNode -> data = Data ;
newNode -> next = head ;
head = newNode ;
}
// adds node after numbers of nodes
void Linked_List::addafter(int loc,
int num){
9
Example 1: Linked List
node *current, *newNode ;
current = head ;
// skip to desired portion
for ( int i = 0 ; i < loc ; i++ ){
current = current -> next ;
// if reached at end of linked list
if ( current == NULL ){
cout << "nThere are less than "
<< loc << " elements in list"
<< endl ;
return ;
}
}
newNode = new node ; // insert new node
newNode -> data = num ;
newNode -> next = current -> next ;
current -> next = newNode ;
}
3 4
// displays the contents of the link list
void Linked_List :: display( ){
node *current = head ;
cout << endl ;
// traverse the entire linked list
while ( current != NULL ){
cout << current -> data << " " ;
current = current -> next ;
}
}
// counts the number of nodes present
int Linked_List :: count( ){
node *current = head ; int c = 0 ;
// traverse the entire linked list
while ( current != NULL ){
current = current -> next ;
c++ ;
}
return c ; }
10
Example 1: Linked List
// deletes the specified node
void Linked_List :: del ( int Data ) {
node *previous, *current ;
current = head ;
while ( current != NULL ){
if ( current -> data == Data ){
// if node to be deleted is the
// first node in the linked list
if ( current == head )
head = current -> next ;
else
previous->next = current->next;
// free memory occupied by node
delete current ;
return ;
}
// traverse the linked list till
// the last node is reached
5 6
else{
previous = current ;
// go to the next node
current = current -> next ;
}
}
cout << "nnElement " << Data
<< " not found" ;
}
// deallocates memory
Linked_List :: ~Linked_List( ){
while ( head != NULL ){
node *current = head;
head = head -> next ;
delete current ;
}
}
11
Example 1: Linked List
int main( ) {
Linked_List l ;
l.push_back (14) ; l.push_back (30) ;
l.push_back (25) ; l.push_back (42) ;
l.push_back (17) ;
cout <<"nData in the linked list: ";
l.display( ) ;
l.push_front (999) ; l.push_front (888) ;
l.push_front (777) ;
cout << "nnData in the linked list "
" after addition at the "
" beginning: " ;
l.display( ) ;
l.addafter (7, 0); l.addafter (2,1);
l.addafter (5,99);
cout << "nnData in the linked list"
" after addition at given"
" position: " ;
7 8
l.display( ) ;
cout <<"nNo. of elements in the "
" linked list " << l.count( );
l.del (99) ;
l.del ( 1) ;
l.del (10) ;
cout << "nnData in the linked list"
" after deletion: " ;
l.display( ) ;
cout <<"nNo. of elements in the"
" linked list: " << l.count();
system("PAUSE");
return 0;
}
12
Example 1: Linked List
9 10
Example 1: 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.
Analysis 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.
Applications of 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.
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.
Applications of Circular Linked List

More Related Content

Similar to Data Structures and Agorithm: DS 04 Linked List.pptx

C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docx
MatthPYNashd
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
rohit219406
 
C++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdfC++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdf
arjunenterprises1978
 
Linked list
Linked listLinked list
Linked list
Md. Afif Al Mamun
 
Linked Stack program.docx
Linked Stack program.docxLinked Stack program.docx
Linked Stack program.docx
kudikalakalabharathi
 
C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked list
Sourav Gayen
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
rozakashif85
 
1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf
sudhinjv
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Balwant Gorad
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
tienlivick
 
Note             Given Code modified as required and required met.pdf
Note             Given Code modified as required and required met.pdfNote             Given Code modified as required and required met.pdf
Note             Given Code modified as required and required met.pdf
Ankitchhabra28
 
Exception to indicate that Singly LinkedList is empty. .pdf
  Exception to indicate that Singly LinkedList is empty. .pdf  Exception to indicate that Singly LinkedList is empty. .pdf
Exception to indicate that Singly LinkedList is empty. .pdf
aravlitraders2012
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
C Assignment Help
C Assignment HelpC Assignment Help
C Assignment Help
Programming Homework Help
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
JamesPXNNewmanp
 
Lab-2.4 101.pdf
Lab-2.4 101.pdfLab-2.4 101.pdf
Lab-2.4 101.pdf
21E135MAHIESHWARJ
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
kingsandqueens3
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
fathimahardwareelect
 
How to delete one specific node in linked list in CThanksSolu.pdf
How to delete one specific node in linked list in CThanksSolu.pdfHow to delete one specific node in linked list in CThanksSolu.pdf
How to delete one specific node in linked list in CThanksSolu.pdf
footstatus
 

Similar to Data Structures and Agorithm: DS 04 Linked List.pptx (20)

C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docx
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
C++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdfC++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdf
 
Linked list
Linked listLinked list
Linked list
 
Linked Stack program.docx
Linked Stack program.docxLinked Stack program.docx
Linked Stack program.docx
 
C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked list
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
 
1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
 
Note             Given Code modified as required and required met.pdf
Note             Given Code modified as required and required met.pdfNote             Given Code modified as required and required met.pdf
Note             Given Code modified as required and required met.pdf
 
Exception to indicate that Singly LinkedList is empty. .pdf
  Exception to indicate that Singly LinkedList is empty. .pdf  Exception to indicate that Singly LinkedList is empty. .pdf
Exception to indicate that Singly LinkedList is empty. .pdf
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
 
C Assignment Help
C Assignment HelpC Assignment Help
C Assignment Help
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
 
Lab-2.4 101.pdf
Lab-2.4 101.pdfLab-2.4 101.pdf
Lab-2.4 101.pdf
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
 
How to delete one specific node in linked list in CThanksSolu.pdf
How to delete one specific node in linked list in CThanksSolu.pdfHow to delete one specific node in linked list in CThanksSolu.pdf
How to delete one specific node in linked list in CThanksSolu.pdf
 

More from RashidFaridChishti

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
RashidFaridChishti
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
RashidFaridChishti
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
RashidFaridChishti
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch10 Pointers.pptx
Object Oriented Programming using C++: Ch10 Pointers.pptxObject Oriented Programming using C++: Ch10 Pointers.pptx
Object Oriented Programming using C++: Ch10 Pointers.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptxObject Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptx
RashidFaridChishti
 

More from RashidFaridChishti (20)

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
 
Object Oriented Programming using C++: Ch10 Pointers.pptx
Object Oriented Programming using C++: Ch10 Pointers.pptxObject Oriented Programming using C++: Ch10 Pointers.pptx
Object Oriented Programming using C++: Ch10 Pointers.pptx
 
Object Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptxObject Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptx
 

Recently uploaded

原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
LAXMAREDDY22
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
bjmsejournal
 
AI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptxAI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptx
architagupta876
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
KrishnaveniKrishnara1
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
bijceesjournal
 
cnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classicationcnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classication
SakkaravarthiShanmug
 
People as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimalaPeople as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimala
riddhimaagrawal986
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
RamonNovais6
 
Data Control Language.pptx Data Control Language.pptx
Data Control Language.pptx Data Control Language.pptxData Control Language.pptx Data Control Language.pptx
Data Control Language.pptx Data Control Language.pptx
ramrag33
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 

Recently uploaded (20)

原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
 
AI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptxAI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptx
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
 
cnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classicationcnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classication
 
People as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimalaPeople as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimala
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
 
Data Control Language.pptx Data Control Language.pptx
Data Control Language.pptx Data Control Language.pptxData Control Language.pptx Data Control Language.pptx
Data Control Language.pptx Data Control Language.pptx
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 

Data Structures and Agorithm: DS 04 Linked List.pptx

  • 1. Data Structure Lecture No. 4 Linked List Engr. Rashid Farid Chishti http://youtube.com/rfchishti http://sites.google.com/site/chishti International Islamic University H-10, Islamabad, Pakistan Video Lecture
  • 2. Linked List head 2 6 8 7 current 1 NULL 0x100 6 0x108 2 0x10C 0x110 0x114 7 0x118 0x11C 8 0x120 1 0x124 0x128 0x000 0x12C 0x11C 0x130 head current 2 6 8 7 1 0x10C 0x100 0x11C 0x114 0x100
  • 3. push_front (int Data){ node *newNode ; // add new node newNode = new node ; newNode -> data = Data ; newNode -> next = head ; head = newNode ; } Linked List Operations head 2 6 1 NULL head NULL NULL 9 newNode newNode 9 Data = 9
  • 4. void push_back ( int Data ){ node *current, *newNode ; // list is empty then create first node if ( head == NULL ){ head = new node ; head -> data = Data ; head -> next = NULL ; } else{ // go to last node current = head ; while ( current -> next != NULL ) current = current -> next ; newNode = new node ; newNode -> data = Data ; newNode -> next = NULL ; current -> next = newNode ; } } Linked List Operations head current 2 6 1 NULL head newNode NULL NULL 9 9 NULL Data = 9
  • 5. void addafter(int loc, int Data){ node *current = head, *newNode ; // skip to desired portion for ( int i = 0 ; i < loc ; i++ ){ current = current -> next ; // if reached at end of linked list if ( current == NULL ){ cout << "nThere are less than " << loc << " elements in list" << endl ; return ; } } newNode = new node ; // insert new node newNode -> data = Data ; newNode -> next = current -> next ; current -> next = newNode ; } Linked List Operations current 2 6 1 NULL head 9 newNode i = 0 1 loc = 1 Data = 9
  • 6. void del ( int Data ) { node *previous, *current ; current = head ; while ( current != NULL ){ if ( current -> data == Data ){ if ( current == head ) head = current -> next ; else previous->next = current->next; delete current ; return ; } else{ previous = current ; current = current -> next ; } } cout << "nnElement " << Data << " not found" ; } Linked List Operations head NULL 1 current previous Data = 1
  • 7. void del ( int Data ) { node *previous, *current ; current = head ; while ( current != NULL ){ if ( current -> data == Data ){ if ( current == head ) head = current -> next ; else previous->next = current->next; delete current ; return ; } else{ previous = current ; current = current -> next ; } } cout << "nnElement " << Data << " not found" ; } Linked List Operations current 2 6 head 9 NULL 1 previous previous Data = 1
  • 8. #include <iostream> using namespace std; class Linked_List{ private : struct node{ int data ; node * next ; } *head ; public : Linked_List ( ) ; void push_back ( int Data ) ; void push_front ( int Data ) ; void addafter ( int loc, int Data ) ; void display ( ) ; int count ( ) ; void del ( int num ) ; ~Linked_List( ) ; } ; 8 Example 1: Linked List Linked_List :: Linked_List( ){ head = NULL ; } // adds node at the end of linked list void Linked_List :: push_back ( int Data ){ node *current, *newNode ; // list is empty then create first node if ( head == NULL ){ head = new node ; head -> data = Data ; head -> next = NULL ; } else{ // go to last node current = head ; while ( current -> next != NULL ) current = current -> next ; // add node at the end 1 2
  • 9. newNode = new node ; newNode -> data = Data ; newNode -> next = NULL ; current -> next = newNode ; } } // adds a node at the beginning void Linked_List :: push_front (int Data){ node *newNode ; // add new node newNode = new node ; newNode -> data = Data ; newNode -> next = head ; head = newNode ; } // adds node after numbers of nodes void Linked_List::addafter(int loc, int num){ 9 Example 1: Linked List node *current, *newNode ; current = head ; // skip to desired portion for ( int i = 0 ; i < loc ; i++ ){ current = current -> next ; // if reached at end of linked list if ( current == NULL ){ cout << "nThere are less than " << loc << " elements in list" << endl ; return ; } } newNode = new node ; // insert new node newNode -> data = num ; newNode -> next = current -> next ; current -> next = newNode ; } 3 4
  • 10. // displays the contents of the link list void Linked_List :: display( ){ node *current = head ; cout << endl ; // traverse the entire linked list while ( current != NULL ){ cout << current -> data << " " ; current = current -> next ; } } // counts the number of nodes present int Linked_List :: count( ){ node *current = head ; int c = 0 ; // traverse the entire linked list while ( current != NULL ){ current = current -> next ; c++ ; } return c ; } 10 Example 1: Linked List // deletes the specified node void Linked_List :: del ( int Data ) { node *previous, *current ; current = head ; while ( current != NULL ){ if ( current -> data == Data ){ // if node to be deleted is the // first node in the linked list if ( current == head ) head = current -> next ; else previous->next = current->next; // free memory occupied by node delete current ; return ; } // traverse the linked list till // the last node is reached 5 6
  • 11. else{ previous = current ; // go to the next node current = current -> next ; } } cout << "nnElement " << Data << " not found" ; } // deallocates memory Linked_List :: ~Linked_List( ){ while ( head != NULL ){ node *current = head; head = head -> next ; delete current ; } } 11 Example 1: Linked List int main( ) { Linked_List l ; l.push_back (14) ; l.push_back (30) ; l.push_back (25) ; l.push_back (42) ; l.push_back (17) ; cout <<"nData in the linked list: "; l.display( ) ; l.push_front (999) ; l.push_front (888) ; l.push_front (777) ; cout << "nnData in the linked list " " after addition at the " " beginning: " ; l.display( ) ; l.addafter (7, 0); l.addafter (2,1); l.addafter (5,99); cout << "nnData in the linked list" " after addition at given" " position: " ; 7 8
  • 12. l.display( ) ; cout <<"nNo. of elements in the " " linked list " << l.count( ); l.del (99) ; l.del ( 1) ; l.del (10) ; cout << "nnData in the linked list" " after deletion: " ; l.display( ) ; cout <<"nNo. of elements in the" " linked list: " << l.count(); system("PAUSE"); return 0; } 12 Example 1: Linked List 9 10
  • 14.  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. Analysis of Linked List
  • 15.  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. Applications of Linked List
  • 16.  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. Circular Linked List
  • 17.  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. Applications of Circular Linked List