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

More Related Content

What's hot

Data Structure (Stack)
Data Structure (Stack)Data Structure (Stack)
Data Structure (Stack)
Adam Mukharil Bachtiar
 
Continuity of a function module02
Continuity of a function module02Continuity of a function module02
Continuity of a function module02
REYEMMANUELILUMBA
 
Pop operation
Pop operationPop operation
Pop operation
Rajesh K Shukla
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)
Neil Soliven
 
Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applicationsSaqib Saeed
 
Stack project
Stack projectStack project
Stack project
Amr Aboelgood
 
Stack data structure
Stack data structureStack data structure
Stack data structure
rogineojerio020496
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
kulachihansraj
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
Circular linked list
Circular linked list Circular linked list
Circular linked list
sajinis3
 
Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!
Mahesh Tibrewal
 
data structure, stack, stack data structure
data structure, stack, stack data structuredata structure, stack, stack data structure
data structure, stack, stack data structurepcnmtutorials
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
University of Science and Technology Chitttagong
 
Unit 3 stack
Unit 3   stackUnit 3   stack
Unit 3 stack
kalyanineve
 
Sql group functions(2)
Sql group functions(2)Sql group functions(2)
Sql group functions(2)
Sumit Tambe
 
Sql group functions
Sql group functionsSql group functions
Sql group functions
Sumit Tambe
 
Stacks in Data Structure
Stacks in Data StructureStacks in Data Structure
Stacks in Data Structure
Lovely Professional University
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
Sheikh Monirul Hasan
 

What's hot (20)

Data Structure (Stack)
Data Structure (Stack)Data Structure (Stack)
Data Structure (Stack)
 
Continuity of a function module02
Continuity of a function module02Continuity of a function module02
Continuity of a function module02
 
Pop operation
Pop operationPop operation
Pop operation
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)
 
Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applications
 
Stack project
Stack projectStack project
Stack project
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Circular linked list
Circular linked list Circular linked list
Circular linked list
 
Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!
 
data structure, stack, stack data structure
data structure, stack, stack data structuredata structure, stack, stack data structure
data structure, stack, stack data structure
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Stack and heap
Stack and heapStack and heap
Stack and heap
 
Unit 3 stack
Unit 3   stackUnit 3   stack
Unit 3 stack
 
Sql group functions(2)
Sql group functions(2)Sql group functions(2)
Sql group functions(2)
 
Sql group functions
Sql group functionsSql group functions
Sql group functions
 
Stacks in Data Structure
Stacks in Data StructureStacks in Data Structure
Stacks in Data Structure
 
Lect 11-12 Zaheer Abbas
Lect 11-12 Zaheer AbbasLect 11-12 Zaheer Abbas
Lect 11-12 Zaheer Abbas
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
 

Similar to Linked list implementation of Stack

DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
ChrisSosaJacob
 
IRJET- Dynamic Implementation of Stack using Single Linked List
IRJET- Dynamic Implementation of Stack using Single Linked ListIRJET- Dynamic Implementation of Stack using Single Linked List
IRJET- Dynamic Implementation of Stack using Single Linked List
IRJET Journal
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
NirmalPandey23
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
SonaPathak5
 
Linked list
Linked listLinked list
Linked list
RahulGandhi110
 
Stacks
StacksStacks
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
MuhammadUmerIhtisham
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
Amar Jukuntla
 
cp264_lecture13_14_linkedlist.ppt
cp264_lecture13_14_linkedlist.pptcp264_lecture13_14_linkedlist.ppt
cp264_lecture13_14_linkedlist.ppt
ssuser9dd05f
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
skilljiolms
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptx
skilljiolms
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
EktaVaswani2
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
Afaq Mansoor Khan
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
Introduction Linked Lists - Singly Linked List,
Introduction Linked Lists - Singly Linked List,Introduction Linked Lists - Singly Linked List,
Introduction Linked Lists - Singly Linked List,
JayaKamal
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
LegesseSamuel
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
Micheal Ogundero
 
DSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEDSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUE
swathirajstar
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
SeethaDinesh
 

Similar to Linked list implementation of Stack (20)

DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
 
IRJET- Dynamic Implementation of Stack using Single Linked List
IRJET- Dynamic Implementation of Stack using Single Linked ListIRJET- Dynamic Implementation of Stack using Single Linked List
IRJET- Dynamic Implementation of Stack using Single Linked List
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
Linked list
Linked listLinked list
Linked list
 
Stacks
StacksStacks
Stacks
 
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
 
cp264_lecture13_14_linkedlist.ppt
cp264_lecture13_14_linkedlist.pptcp264_lecture13_14_linkedlist.ppt
cp264_lecture13_14_linkedlist.ppt
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptx
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
 
Introduction Linked Lists - Singly Linked List,
Introduction Linked Lists - Singly Linked List,Introduction Linked Lists - Singly Linked List,
Introduction Linked Lists - Singly Linked List,
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
 
DSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEDSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUE
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
 

Recently uploaded

Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
Kamal Acharya
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
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.
PrashantGoswami42
 
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
wendy cai
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
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...
Amil baba
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
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
Kamal Acharya
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
Explosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdfExplosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdf
884710SadaqatAli
 
Antenna efficency lecture course chapter 3.pdf
Antenna  efficency lecture course chapter 3.pdfAntenna  efficency lecture course chapter 3.pdf
Antenna efficency lecture course chapter 3.pdf
AbrahamGadissa
 
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
 
Introduction to Casting Processes in Manufacturing
Introduction to Casting Processes in ManufacturingIntroduction to Casting Processes in Manufacturing
Introduction to Casting Processes in Manufacturing
ssuser0811ec
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
shape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxshape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptx
VishalDeshpande27
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 

Recently uploaded (20)

Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
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.
 
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
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
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...
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
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
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Explosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdfExplosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdf
 
Antenna efficency lecture course chapter 3.pdf
Antenna  efficency lecture course chapter 3.pdfAntenna  efficency lecture course chapter 3.pdf
Antenna efficency lecture course chapter 3.pdf
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
 
Introduction to Casting Processes in Manufacturing
Introduction to Casting Processes in ManufacturingIntroduction to Casting Processes in Manufacturing
Introduction to Casting Processes in Manufacturing
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
shape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxshape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptx
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 

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