SlideShare a Scribd company logo
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

linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in C
Jabs6
 
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
 
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
ssuser7319f8
 

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

power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
Kamal Acharya
 

Recently uploaded (20)

Introduction to Casting Processes in Manufacturing
Introduction to Casting Processes in ManufacturingIntroduction to Casting Processes in Manufacturing
Introduction to Casting Processes in Manufacturing
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
 
Explosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdfExplosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdf
 
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Kraków
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projection
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdf
 
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxCloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
fluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerfluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answer
 

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; } } }