SlideShare a Scribd company logo
3
Most read
7
Most read
11
Most read
LINKED LIST
IMPLEMENTATION OF
STACK
MODULE 3
DR. SINDHIA LINGASWAMY, VIT 1
DR. SINDHIA LINGASWAMY, VIT 2
• The drawback of using array method is fixed size, but in
this method it’s removed.
• The storage requirement of linked representation of the
stack with n elements is O(n) and the typical time
requirement for the operation is O(1).
• In a linked stack, every node has two parts : one that
stores data and another that stores the address of the
next node.
• The START pointer of the linked list is used as TOP.
DR. SINDHIA LINGASWAMY, VIT
3
OPERATIONS
Push Operation:-
•The push operation is used to insert an element
into the stack.
•The new element is added at the topmost position
of the stack.
LinkedStack
DR. SINDHIA LINGASWAMY, VIT
4
• To insert an element with value 8, we first check if
TOP=NULL.
• If this is the case, then we allocate memory for a new
node, store the value in its DATA part and NULL in its NEXT
part.
• The new node will then be called TOP.
• If TOP!=NULL, then we insert the new node at the
beginning of the linked stack and name this new node
as TOP.
8
Linkedstackafterinsertinga newnode
DR. SINDHIA LINGASWAMY, VIT
5
ALGORITHM
TOPUSH AN ELEMENT INTO A LINKED
STACK
DR. SINDHIA LINGASWAMY, VIT 6
• In Step 1, memory is allocated
for the new node.
• In Step 2, the DATA part of the
new node is initialized with the
value to be storedin the node.
• In Step 3, we check if the new
node is the first node of the
linkedlist.
• This is done by checking if TOP =
NULL. In case the IF statement
evaluates to true, then NULL is
stored in the NEXT part of the
node and the new node is called
TOP. However, if the new node is
not the first node in the list, then
it is added before the first node
of the list (that is, the TOP node)
and termed as TOP.
Step1: Allocatememory for the new
node and name it as NEW_NODE
Step2: SET NEW_NODE -> DATA= VAL
Step3: IF TOP = NULL
SET NEW_NODE -> NEXT = NULL
SET TOP = NEW_NODE
ELSE
SET NEW_NODE -> NEXT = TOP
SET TOP = NEW_NODE
[END OF IF]
Step4: END
DR. SINDHIA LINGASWAMY, VIT 7
POP OPERATION
•The pop operation is used to delete an element
into the stack.
•The element is deleted at the topmost position of
the stack.
•However, before deleting the value, we must first
check if TOP=NULL, because if this is the case, then
it means that the stack is empty and no more
deletions can be done
DR. SINDHIA LINGASWAMY, VIT
8
•If an attempt is made to delete a value from a stackthat
is already empty, an UNDERFLOW message is printed.
•In case TOP!=NULL, then we will delete the node
pointed by TOP,and make TOP point to the second
element of the linked stack. Thus, the updated stack
becomes like this.
Linkedstackafterdeletinga node
TOP
DR. SINDHIA LINGASWAMY, VIT 9
ALGORITHM
TOPOP AN ELEMENT INTO A
LINKED STACK
DR. SINDHIA LINGASWAMY, VIT 10
Step 1: IF TOP=NULL
PRINT “UNDERFLOW”
Goto Step 5
[END OF IF]
Step 2: SET PTR = TOP
Step 3: SET TOP = TOP->NEXT
Step 4: FREE PTR
Step 5: END
•In Step 1,we first check for
the UNDERFLOW condition.
TOP!=NULL
•In Step 2,we use a pointer
PTR that points to TOP.
•In Step 3,TOP is made to
point to the next node in
sequence.
•In Step 4,the memory
occupied by PTR is given back
to the free pool.
DR. SINDHIA LINGASWAMY, VIT 11
Peek Operation
• The Peek operation is used to see the value of the
topmost element of the stack without deleting it
from the stack.
• Peek operation first checks if the stack is empty,
i.e., if TOP=NULL, then an appropriate message is
printed, else the value is returned.
DR. SINDHIA LINGASWAMY, VIT
12
TO PEEK AN ELEMENT FROM A
LINKED STACK
• In step 1, if TOP
• =NULL means that the stack is
empty.
• Else it will return the data on TOP,
which will be our output or Peeped
out Value.
Step 1: IF TOP= NULL
PRINT “UNDERFLOW”
ELSE
RETURN TOP->DATA
[END OF IF]
Step 2: END
DR. SINDHIA LINGASWAMY, VIT 13
Ad

Recommended

My lectures circular queue
My lectures circular queue
Senthil Kumar
 
Stack a Data Structure
Stack a Data Structure
ForwardBlog Enewzletter
 
Data Structure (Stack)
Data Structure (Stack)
Adam Mukharil Bachtiar
 
Part 1 - PROCESS CONCEPTS
Part 1 - PROCESS CONCEPTS
priyasoundar
 
Priority Queue in Data Structure
Priority Queue in Data Structure
Meghaj Mallick
 
Stack data structure
Stack data structure
Tech_MX
 
Numeric functions in SQL | Oracle
Numeric functions in SQL | Oracle
Vimal Raj
 
Data structures using C
Data structures using C
Prof. Dr. K. Adisesha
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
KristinaBorooah
 
trees in data structure
trees in data structure
shameen khan
 
Tree and Binary Search tree
Tree and Binary Search tree
Muhazzab Chouhadry
 
Hashing data
Hashing data
umair khan
 
Data structure - Graph
Data structure - Graph
Madhu Bala
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
Aakash deep Singhal
 
Arrays C#
Arrays C#
Raghuveer Guthikonda
 
Two-dimensional array in java
Two-dimensional array in java
Talha mahmood
 
Swapping | Computer Science
Swapping | Computer Science
Transweb Global Inc
 
Input and output in C++
Input and output in C++
Nilesh Dalvi
 
Stacks, Queues, Deques
Stacks, Queues, Deques
A-Tech and Software Development
 
Sorting Algorithms
Sorting Algorithms
Mohammed Hussein
 
durga python full.pdf
durga python full.pdf
ssuser476810
 
Heap tree
Heap tree
Shankar Bishnoi
 
DBMS - RAID
DBMS - RAID
MythiliMurugan3
 
Backtracking & branch and bound
Backtracking & branch and bound
Vipul Chauhan
 
OOSE
OOSE
shah zeb
 
Shell sorting
Shell sorting
TUC
 
Sorting algorithms
Sorting algorithms
Trupti Agrawal
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
Afzal Badshah
 
IRJET- Dynamic Implementation of Stack using Single Linked List
IRJET- Dynamic Implementation of Stack using Single Linked List
IRJET Journal
 
stacks and queues
stacks and queues
EktaVaswani2
 

More Related Content

What's hot (20)

Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
KristinaBorooah
 
trees in data structure
trees in data structure
shameen khan
 
Tree and Binary Search tree
Tree and Binary Search tree
Muhazzab Chouhadry
 
Hashing data
Hashing data
umair khan
 
Data structure - Graph
Data structure - Graph
Madhu Bala
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
Aakash deep Singhal
 
Arrays C#
Arrays C#
Raghuveer Guthikonda
 
Two-dimensional array in java
Two-dimensional array in java
Talha mahmood
 
Swapping | Computer Science
Swapping | Computer Science
Transweb Global Inc
 
Input and output in C++
Input and output in C++
Nilesh Dalvi
 
Stacks, Queues, Deques
Stacks, Queues, Deques
A-Tech and Software Development
 
Sorting Algorithms
Sorting Algorithms
Mohammed Hussein
 
durga python full.pdf
durga python full.pdf
ssuser476810
 
Heap tree
Heap tree
Shankar Bishnoi
 
DBMS - RAID
DBMS - RAID
MythiliMurugan3
 
Backtracking & branch and bound
Backtracking & branch and bound
Vipul Chauhan
 
OOSE
OOSE
shah zeb
 
Shell sorting
Shell sorting
TUC
 
Sorting algorithms
Sorting algorithms
Trupti Agrawal
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
Afzal Badshah
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
KristinaBorooah
 
trees in data structure
trees in data structure
shameen khan
 
Data structure - Graph
Data structure - Graph
Madhu Bala
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
Aakash deep Singhal
 
Two-dimensional array in java
Two-dimensional array in java
Talha mahmood
 
Input and output in C++
Input and output in C++
Nilesh Dalvi
 
durga python full.pdf
durga python full.pdf
ssuser476810
 
Backtracking & branch and bound
Backtracking & branch and bound
Vipul Chauhan
 
Shell sorting
Shell sorting
TUC
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
Afzal Badshah
 

Similar to Linked list implementation of Stack (20)

IRJET- Dynamic Implementation of Stack using Single Linked List
IRJET- Dynamic Implementation of Stack using Single Linked List
IRJET Journal
 
stacks and queues
stacks and queues
EktaVaswani2
 
Linked list
Linked list
RahulGandhi110
 
Stacks
Stacks
amitphadikar2012
 
Stack data structures with definition and code
Stack data structures with definition and code
bansidharj11
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
ssuser7319f8
 
Operations on linked list
Operations on linked list
Sumathi Kv
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
DSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUE
swathirajstar
 
unit 5 stack & queue.ppt
unit 5 stack & queue.ppt
SeethaDinesh
 
Data structure and algorithm list structures
Data structure and algorithm list structures
gangaresearch21
 
Linked list data structures and algorithms
Linked list data structures and algorithms
RaghavendraPrasad179187
 
Linked List - Insertion & Deletion
Linked List - Insertion & Deletion
Afaq Mansoor Khan
 
Unit ii(dsc++)
Unit ii(dsc++)
Durga Devi
 
Stacks in c++
Stacks in c++
Vineeta Garg
 
Circular linked list
Circular linked list
sajinis3
 
Stacks fundamentals
Stacks fundamentals
greatqadirgee4u
 
DSModule2.pptx
DSModule2.pptx
ChrisSosaJacob
 
Link list 2
Link list 2
sana younas
 
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
beshahashenafe20
 
IRJET- Dynamic Implementation of Stack using Single Linked List
IRJET- Dynamic Implementation of Stack using Single Linked List
IRJET Journal
 
Stack data structures with definition and code
Stack data structures with definition and code
bansidharj11
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
ssuser7319f8
 
Operations on linked list
Operations on linked list
Sumathi Kv
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
DSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUE
swathirajstar
 
unit 5 stack & queue.ppt
unit 5 stack & queue.ppt
SeethaDinesh
 
Data structure and algorithm list structures
Data structure and algorithm list structures
gangaresearch21
 
Linked list data structures and algorithms
Linked list data structures and algorithms
RaghavendraPrasad179187
 
Linked List - Insertion & Deletion
Linked List - Insertion & Deletion
Afaq Mansoor Khan
 
Unit ii(dsc++)
Unit ii(dsc++)
Durga Devi
 
Circular linked list
Circular linked list
sajinis3
 
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
beshahashenafe20
 
Ad

Recently uploaded (20)

David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
IJCNCJournal
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
Introduction to sensing and Week-1.pptx
Introduction to sensing and Week-1.pptx
KNaveenKumarECE
 
AI_Presentation (1). Artificial intelligence
AI_Presentation (1). Artificial intelligence
RoselynKaur8thD34
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
nnnnnnnnnnnn7777777777777777777777777777777.pptx
nnnnnnnnnnnn7777777777777777777777777777777.pptx
gayathri venkataramani
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Stay Safe Women Security Android App Project Report.pdf
Stay Safe Women Security Android App Project Report.pdf
Kamal Acharya
 
Mechanical Vibration_MIC 202_iit roorkee.pdf
Mechanical Vibration_MIC 202_iit roorkee.pdf
isahiliitr
 
Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
ElysiumPro Company Profile 2025-2026.pdf
ElysiumPro Company Profile 2025-2026.pdf
info751436
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
ijab2
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
IJCNCJournal
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
Introduction to sensing and Week-1.pptx
Introduction to sensing and Week-1.pptx
KNaveenKumarECE
 
AI_Presentation (1). Artificial intelligence
AI_Presentation (1). Artificial intelligence
RoselynKaur8thD34
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
nnnnnnnnnnnn7777777777777777777777777777777.pptx
nnnnnnnnnnnn7777777777777777777777777777777.pptx
gayathri venkataramani
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Stay Safe Women Security Android App Project Report.pdf
Stay Safe Women Security Android App Project Report.pdf
Kamal Acharya
 
Mechanical Vibration_MIC 202_iit roorkee.pdf
Mechanical Vibration_MIC 202_iit roorkee.pdf
isahiliitr
 
Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
ElysiumPro Company Profile 2025-2026.pdf
ElysiumPro Company Profile 2025-2026.pdf
info751436
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
ijab2
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
Ad

Linked list implementation of Stack

  • 1. LINKED LIST IMPLEMENTATION OF STACK MODULE 3 DR. SINDHIA LINGASWAMY, VIT 1
  • 3. • The drawback of using array method is fixed size, but in this method it’s removed. • The storage requirement of linked representation of the stack with n elements is O(n) and the typical time requirement for the operation is O(1). • In a linked stack, every node has two parts : one that stores data and another that stores the address of the next node. • The START pointer of the linked list is used as TOP. DR. SINDHIA LINGASWAMY, VIT 3
  • 4. OPERATIONS Push Operation:- •The push operation is used to insert an element into the stack. •The new element is added at the topmost position of the stack. LinkedStack DR. SINDHIA LINGASWAMY, VIT 4
  • 5. • To insert an element with value 8, we first check if TOP=NULL. • If this is the case, then we allocate memory for a new node, store the value in its DATA part and NULL in its NEXT part. • The new node will then be called TOP. • If TOP!=NULL, then we insert the new node at the beginning of the linked stack and name this new node as TOP. 8 Linkedstackafterinsertinga newnode DR. SINDHIA LINGASWAMY, VIT 5
  • 6. ALGORITHM TOPUSH AN ELEMENT INTO A LINKED STACK DR. SINDHIA LINGASWAMY, VIT 6
  • 7. • In Step 1, memory is allocated for the new node. • In Step 2, the DATA part of the new node is initialized with the value to be storedin the node. • In Step 3, we check if the new node is the first node of the linkedlist. • This is done by checking if TOP = NULL. In case the IF statement evaluates to true, then NULL is stored in the NEXT part of the node and the new node is called TOP. However, if the new node is not the first node in the list, then it is added before the first node of the list (that is, the TOP node) and termed as TOP. Step1: Allocatememory for the new node and name it as NEW_NODE Step2: SET NEW_NODE -> DATA= VAL Step3: IF TOP = NULL SET NEW_NODE -> NEXT = NULL SET TOP = NEW_NODE ELSE SET NEW_NODE -> NEXT = TOP SET TOP = NEW_NODE [END OF IF] Step4: END DR. SINDHIA LINGASWAMY, VIT 7
  • 8. POP OPERATION •The pop operation is used to delete an element into the stack. •The element is deleted at the topmost position of the stack. •However, before deleting the value, we must first check if TOP=NULL, because if this is the case, then it means that the stack is empty and no more deletions can be done DR. SINDHIA LINGASWAMY, VIT 8
  • 9. •If an attempt is made to delete a value from a stackthat is already empty, an UNDERFLOW message is printed. •In case TOP!=NULL, then we will delete the node pointed by TOP,and make TOP point to the second element of the linked stack. Thus, the updated stack becomes like this. Linkedstackafterdeletinga node TOP DR. SINDHIA LINGASWAMY, VIT 9
  • 10. ALGORITHM TOPOP AN ELEMENT INTO A LINKED STACK DR. SINDHIA LINGASWAMY, VIT 10
  • 11. Step 1: IF TOP=NULL PRINT “UNDERFLOW” Goto Step 5 [END OF IF] Step 2: SET PTR = TOP Step 3: SET TOP = TOP->NEXT Step 4: FREE PTR Step 5: END •In Step 1,we first check for the UNDERFLOW condition. TOP!=NULL •In Step 2,we use a pointer PTR that points to TOP. •In Step 3,TOP is made to point to the next node in sequence. •In Step 4,the memory occupied by PTR is given back to the free pool. DR. SINDHIA LINGASWAMY, VIT 11
  • 12. Peek Operation • The Peek operation is used to see the value of the topmost element of the stack without deleting it from the stack. • Peek operation first checks if the stack is empty, i.e., if TOP=NULL, then an appropriate message is printed, else the value is returned. DR. SINDHIA LINGASWAMY, VIT 12
  • 13. TO PEEK AN ELEMENT FROM A LINKED STACK • In step 1, if TOP • =NULL means that the stack is empty. • Else it will return the data on TOP, which will be our output or Peeped out Value. Step 1: IF TOP= NULL PRINT “UNDERFLOW” ELSE RETURN TOP->DATA [END OF IF] Step 2: END DR. SINDHIA LINGASWAMY, VIT 13