SlideShare a Scribd company logo
3
Most read
11
Most read
18
Most read
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

PPTX
Linear data structure concepts
PPT
Lec-07 Queues.ppt queues introduction to queue
PPTX
The presention is about the queue data structure
PPTX
Presentation on Elementary data structures
PDF
Unit - 2.pdf
PPT
Unit7 C
PDF
DS Module 03.pdf
PPTX
Implemention of Linked list concept in Data Structures
Linear data structure concepts
Lec-07 Queues.ppt queues introduction to queue
The presention is about the queue data structure
Presentation on Elementary data structures
Unit - 2.pdf
Unit7 C
DS Module 03.pdf
Implemention of Linked list concept in Data Structures

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

PPTX
DSModule2.pptx
PPTX
linkedlist.pptx
PPTX
Data structure
PPTX
Implementation of queue using singly and doubly linked list.
PPTX
module 3-.pptx
PPTX
ELEMENTARY DATASTRUCTURES
PPTX
Unit II Data Structure 2hr topic - List - Operations.pptx
PPTX
DS ppt1.pptx.c programing. Engineering. Data structure
PPTX
Data structures and algorithms
PPTX
Doubly & Circular Linked Lists
PPTX
Stack and Queue
PPTX
linkedlist-130914084342-phpapp02.pptx
PPT
DS Unit 2.ppt
PPTX
Data Structures in C
PPTX
Data Structure
PPTX
RPT_03_A_Linked List presentation for FE
PDF
DOC-20250201-WA0019..pdf37853675543337784326863467
PPTX
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
PDF
Data Structures and Files
PPT
Data Structures 6
DSModule2.pptx
linkedlist.pptx
Data structure
Implementation of queue using singly and doubly linked list.
module 3-.pptx
ELEMENTARY DATASTRUCTURES
Unit II Data Structure 2hr topic - List - Operations.pptx
DS ppt1.pptx.c programing. Engineering. Data structure
Data structures and algorithms
Doubly & Circular Linked Lists
Stack and Queue
linkedlist-130914084342-phpapp02.pptx
DS Unit 2.ppt
Data Structures in C
Data Structure
RPT_03_A_Linked List presentation for FE
DOC-20250201-WA0019..pdf37853675543337784326863467
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
Data Structures and Files
Data Structures 6
Ad

Recently uploaded (20)

PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PPTX
Simulation of electric circuit laws using tinkercad.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
Project quality management in manufacturing
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Sustainable Sites - Green Building Construction
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
“Next-Gen AI: Trends Reshaping Our World”
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
Simulation of electric circuit laws using tinkercad.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Project quality management in manufacturing
Foundation to blockchain - A guide to Blockchain Tech
Sustainable Sites - Green Building Construction
Internet of Things (IOT) - A guide to understanding
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Arduino robotics embedded978-1-4302-3184-4.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Lesson 3_Tessellation.pptx finite Mathematics
CYBER-CRIMES AND SECURITY A guide to understanding
CH1 Production IntroductoryConcepts.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
“Next-Gen AI: Trends Reshaping Our World”
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Ad

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