SlideShare a Scribd company logo
1 of 18
DATA STRUCTURES
Stacks & Queues
using
Linked List
Dr G.Kalyani
Department of Information Technology
VR Siddhartha engineering College
Topics
• Stacks using linked List
• Queues using Linked List
• Instead of using array, we can also use linked list to
implement stack.
• Linked list allocates the memory dynamically.
• In linked list implementation of stack, the nodes are
maintained non-contiguously in the memory.
• Each node contains a pointer to its immediate successor
node in the stack.
• Stack is said to be overflown if the space left in the memory
heap is not enough to create a node.
• The top most node in the stack always contains null in its
address field.
Linked list implementation of stack
Linked list implementation of stack
PUSH Operation
Algorithmpush ()
{
int val;
Allocatememoryto newnode;
if(newnode == NULL)
{
Writenot able to push the element;
}
else
{
read val;
newnode ->data = val;
newnode ->next= TOS;
TOS= newnode ;
writeItempushed;
}
}
PUSH Operation
POP Operation
POP Operation
Algorithm POP()
{
int item;
struct node *temp;
if (TOS == NULL)
{
write Underflow;
}
else
{
item = TOS->val;
temp = TOS;
TOS = TOS->next;
free(temp);
write Itempopped;
}
}
Algorithm display()
{
struct node *ptr;
temp=TOS;
if(TOS == NULL)
{
write Stack is empty;
}
else
{
write Printing Stack elements;
while(temp!=NULL)
{
write temp->val;
temp = temp->next;
}
}
}
Traversing or Display
Topics
• Stacks using linked List
• Queues using Linked List
Linked List Implementation of Queues
• In a linked queue, each node of the queue consists of two parts
i.e. data part and the link part.
• Each element of the queue points to its immediate next element
in the memory.
• In the linked queue, there are two pointers maintained in the
memory i.e. front pointer and rear pointer.
• The front pointer contains the address of the starting element of
the queue while the rear pointer contains the address of the last
element of the queue.
• Insertion and deletions are performed at rear and front end
respectively.
• If front and rear both are NULL, it indicates that the queue is
empty.
Linked List Implementation of Queues
Insert Operation
Insert Operation
AlgorithmInsert_Q()
{
Struct node *newnode;
Allocatememoryto newnode;
if(newnode == NULL)
{
writememorynot allocated
return;
}
else
{
newnode -> data = item;
if(front == NULL and rear==NULL)
{
front = newnode;
rear = newnode;
front -> next= NULL;
rear -> next = NULL;
}
else
{
rear -> next = newnode;
rear = newnode;
rear->next= NULL;
}
}
}
Delete Operation
Algorithm delete ()
{
struct node *ptr;
if(front == NULL and rear==NULL)
{
write UNDERFLOW;
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
Delete Operation
Traversing or Display
Algorithm display()
{
struct node *temp;
temp = front;
if(front == NULL)
{
Write Empty queue;
}
else
{
Write Queue Elements:;
while(temp != NULL)
{
Write temp -> data;
temp = temp -> next;
}
}
}
Stacks and Queues with Linked List.pdf

More Related Content

Similar to Stacks and Queues with Linked List.pdf

Implementation of queue using singly and doubly linked list.
Implementation of queue using singly and doubly linked list.Implementation of queue using singly and doubly linked list.
Implementation of queue using singly and doubly linked list.central university of bihar
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJulie Iskander
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked ListsAfaq Mansoor Khan
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxMeghaKulkarni27
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in CJabs6
 
Data Structures and Files
Data Structures and FilesData Structures and Files
Data Structures and FilesKanchanPatil34
 
QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)Mehedi Hasan
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptxskilljiolms
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queuessuser7319f8
 

Similar to Stacks and Queues with Linked List.pdf (20)

Implementation of queue using singly and doubly linked list.
Implementation of queue using singly and doubly linked list.Implementation of queue using singly and doubly linked list.
Implementation of queue using singly and doubly linked list.
 
module 3-.pptx
module 3-.pptxmodule 3-.pptx
module 3-.pptx
 
ELEMENTARY DATASTRUCTURES
ELEMENTARY DATASTRUCTURESELEMENTARY DATASTRUCTURES
ELEMENTARY DATASTRUCTURES
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in C
 
Data Structure
Data StructureData Structure
Data Structure
 
Data Structures and Files
Data Structures and FilesData Structures and Files
Data Structures and Files
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
Data Structures 3
Data Structures 3Data Structures 3
Data Structures 3
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Queues
QueuesQueues
Queues
 

Recently uploaded

Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxNANDHAKUMARA10
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...ppkakm
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network DevicesChandrakantDivate1
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementDr. Deepak Mudgal
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
 
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...jabtakhaidam7
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Call Girls Mumbai
 

Recently uploaded (20)

Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptx
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 

Stacks and Queues with Linked List.pdf

  • 1. DATA STRUCTURES Stacks & Queues using Linked List Dr G.Kalyani Department of Information Technology VR Siddhartha engineering College
  • 2. Topics • Stacks using linked List • Queues using Linked List
  • 3. • Instead of using array, we can also use linked list to implement stack. • Linked list allocates the memory dynamically. • In linked list implementation of stack, the nodes are maintained non-contiguously in the memory. • Each node contains a pointer to its immediate successor node in the stack. • Stack is said to be overflown if the space left in the memory heap is not enough to create a node. • The top most node in the stack always contains null in its address field. Linked list implementation of stack
  • 6. Algorithmpush () { int val; Allocatememoryto newnode; if(newnode == NULL) { Writenot able to push the element; } else { read val; newnode ->data = val; newnode ->next= TOS; TOS= newnode ; writeItempushed; } } PUSH Operation
  • 8. POP Operation Algorithm POP() { int item; struct node *temp; if (TOS == NULL) { write Underflow; } else { item = TOS->val; temp = TOS; TOS = TOS->next; free(temp); write Itempopped; } }
  • 9. Algorithm display() { struct node *ptr; temp=TOS; if(TOS == NULL) { write Stack is empty; } else { write Printing Stack elements; while(temp!=NULL) { write temp->val; temp = temp->next; } } } Traversing or Display
  • 10. Topics • Stacks using linked List • Queues using Linked List
  • 11. Linked List Implementation of Queues • In a linked queue, each node of the queue consists of two parts i.e. data part and the link part. • Each element of the queue points to its immediate next element in the memory. • In the linked queue, there are two pointers maintained in the memory i.e. front pointer and rear pointer. • The front pointer contains the address of the starting element of the queue while the rear pointer contains the address of the last element of the queue. • Insertion and deletions are performed at rear and front end respectively. • If front and rear both are NULL, it indicates that the queue is empty.
  • 14. Insert Operation AlgorithmInsert_Q() { Struct node *newnode; Allocatememoryto newnode; if(newnode == NULL) { writememorynot allocated return; } else { newnode -> data = item; if(front == NULL and rear==NULL) { front = newnode; rear = newnode; front -> next= NULL; rear -> next = NULL; } else { rear -> next = newnode; rear = newnode; rear->next= NULL; } } }
  • 16. Algorithm delete () { struct node *ptr; if(front == NULL and rear==NULL) { write UNDERFLOW; return; } else { ptr = front; front = front -> next; free(ptr); } } Delete Operation
  • 17. Traversing or Display Algorithm display() { struct node *temp; temp = front; if(front == NULL) { Write Empty queue; } else { Write Queue Elements:; while(temp != NULL) { Write temp -> data; temp = temp -> next; } } }