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

PDF
Linked Lists.pdf
PPTX
singlelinkedlistasdfghzxcvbnmqwertyuiopa
PPTX
data structures lists operation of lists
PPTX
DS_LinkedList.pptx
PPTX
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
PPTX
Lec3-Linked list.pptx
PPT
linked_lists.ppt linked_lists linked_lists
PPT
dynamicList.ppt
Linked Lists.pdf
singlelinkedlistasdfghzxcvbnmqwertyuiopa
data structures lists operation of lists
DS_LinkedList.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Lec3-Linked list.pptx
linked_lists.ppt linked_lists linked_lists
dynamicList.ppt

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

PPTX
Linked list and its operations - Traversal
PPTX
Linked lists a
PPTX
Linked list
PPTX
How to sort linked list using sorthing method.pptx
PPT
Algo>ADT list & linked list
DOCX
Linked list.docx
PPTX
LEC_4,5_linked_list.pptx this is Good for data structure
PPTX
LEC_4,5_linked_list.pptx for single and double linked list
PPT
Lecture 3 List of Data Structures & Algorithms
PPTX
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
PPT
Operations on linked list
PPT
ds 4Linked lists.ppt
PPTX
DSA(1).pptx
PPT
Abstract data types
PPTX
Data Structures_Linked List
PPT
linked-list - Abstract data type (ADT) Linked Lists
PPTX
Linked list
PPT
Data Structures 3
PPTX
Linked List - Insertion & Deletion
PPTX
linkedlist-130914084342-phpapp02.pptx
Linked list and its operations - Traversal
Linked lists a
Linked list
How to sort linked list using sorthing method.pptx
Algo>ADT list & linked list
Linked list.docx
LEC_4,5_linked_list.pptx this is Good for data structure
LEC_4,5_linked_list.pptx for single and double linked list
Lecture 3 List of Data Structures & Algorithms
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
Operations on linked list
ds 4Linked lists.ppt
DSA(1).pptx
Abstract data types
Data Structures_Linked List
linked-list - Abstract data type (ADT) Linked Lists
Linked list
Data Structures 3
Linked List - Insertion & Deletion
linkedlist-130914084342-phpapp02.pptx
Ad

More from RashidFaridChishti (20)

PPTX
DBMS: Week 15 - Database Security and Access Control
PPTX
DBMS: Week 14 - Backup and Recovery in MySQL
PPTX
DBMS: Week 13 - Transactions and Concurrency Control
PPTX
DBMS: Week 12 - Triggers in MySQL Database Server
PPTX
DBMS: Week 11 - Stored Procedures and Functions
PPTX
DBMS: Week 10 - Database Design and Normalization
PPTX
DBMS: Week 09 - SQL Constraints and Indexing
PPTX
DBMS: Week 08 - Joins and Views in MySQL
PPTX
DBMS: Week 07 - Advanced SQL Queries in MySQL
PPTX
DBMS: Week 06 - SQL - Data Manipulation Language (DML)
PPTX
DBMS: Week 05 - Introduction to SQL Query
PPTX
DBMS: Week 04 - Relational Model in a Database
PPTX
DBMS: Week 03 - Data Models and ER Model
PPTX
DBMS: Week 02 - Database System Architecture
PPTX
DBMS: Week 01 - Introduction to Databases
DOCX
Lab Manual Arduino UNO Microcontrollar.docx
DOCX
Object Oriented Programming OOP Lab Manual.docx
DOCX
Lab Manual Data Structure and Algorithm.docx
PPTX
Data Structures and Agorithm: DS 24 Hash Tables.pptx
PPTX
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
DBMS: Week 15 - Database Security and Access Control
DBMS: Week 14 - Backup and Recovery in MySQL
DBMS: Week 13 - Transactions and Concurrency Control
DBMS: Week 12 - Triggers in MySQL Database Server
DBMS: Week 11 - Stored Procedures and Functions
DBMS: Week 10 - Database Design and Normalization
DBMS: Week 09 - SQL Constraints and Indexing
DBMS: Week 08 - Joins and Views in MySQL
DBMS: Week 07 - Advanced SQL Queries in MySQL
DBMS: Week 06 - SQL - Data Manipulation Language (DML)
DBMS: Week 05 - Introduction to SQL Query
DBMS: Week 04 - Relational Model in a Database
DBMS: Week 03 - Data Models and ER Model
DBMS: Week 02 - Database System Architecture
DBMS: Week 01 - Introduction to Databases
Lab Manual Arduino UNO Microcontrollar.docx
Object Oriented Programming OOP Lab Manual.docx
Lab Manual Data Structure and Algorithm.docx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Ad

Recently uploaded (20)

DOCX
573137875-Attendance-Management-System-original
PPT
Project quality management in manufacturing
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
composite construction of structures.pdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Welding lecture in detail for understanding
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Geodesy 1.pptx...............................................
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Sustainable Sites - Green Building Construction
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Lecture Notes Electrical Wiring System Components
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
573137875-Attendance-Management-System-original
Project quality management in manufacturing
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
composite construction of structures.pdf
additive manufacturing of ss316l using mig welding
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Embodied AI: Ushering in the Next Era of Intelligent Systems
Welding lecture in detail for understanding
OOP with Java - Java Introduction (Basics)
CYBER-CRIMES AND SECURITY A guide to understanding
Geodesy 1.pptx...............................................
bas. eng. economics group 4 presentation 1.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Sustainable Sites - Green Building Construction
Automation-in-Manufacturing-Chapter-Introduction.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Lecture Notes Electrical Wiring System Components
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx

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