0% found this document useful (0 votes)
23 views18 pages

Stack

A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle, where elements are added and removed from the top. The three basic operations of a stack are push (to add an element), pop (to remove the top element), and peek (to view the top element without removing it). Stacks can be implemented using arrays or linked lists, with specific procedures for handling overflow and underflow conditions.

Uploaded by

jrtadlip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views18 pages

Stack

A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle, where elements are added and removed from the top. The three basic operations of a stack are push (to add an element), pop (to remove the top element), and peek (to view the top element without removing it). Stacks can be implemented using arrays or linked lists, with specific procedures for handling overflow and underflow conditions.

Uploaded by

jrtadlip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Stacks

Data Structures and Algortihm


● Stack is an important data structure which stores its elements in an ordered manner.

● We will explain the concept of stacks using an analogy. You must have seen a pile of plates
where one plate is placed on top of another as shown in Fig.
● Now, when you want to remove a plate, you remove the topmost plate first.
● Hence, you can add and remove an element (i.e., a plate) only at/from one position which is the
topmost position.

● A stack is a linear data structure which uses the same


principle.
● the elements in a stack are added and removed only from
one end, which is called the TOP.
● A stack is called a LIFO (Last-In-First-Out) data structure
When A calls B, A is pushed on top When B calls C, B is pushed on top
of the system stack. When the of the system stack. When the
execution of B is complete, the execution of C is complete, the
system control will remove A from system control will remove B from
the stack and continue with its the stack and continue with its
execution. execution.

When C calls D, C is pushed on top When D calls E, D is pushed on top


of the system stack. When the of the system stack. When the
execution of D is complete, the execution of E is complete, the
system control will remove C from system control will remove D from
the stack and continue with its the stack and continue with its
execution. execution.
OPERATIONS ON A STACK
● A stack supports three basic operations: push, pop, and peek.
● The push operation adds an element to the top of the stack and the pop operation
removes the element from the top of the stack.
● The peek operation returns the value of the topmost element of the stack.

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.

before inserting the value, we must first check if TOP=MAX–1.

To insert an element with value 6, we first check if TOP=MAX–1. If the condition is false,
then we
increment the value of TOP and store the new element at the position given by stack[TOP].
Step 1, we first check for the OVERFLOW condition.
In Step 2, TOP is incremented so that it points to the next location in
the array. In
Step 3, the value is stored in the stack at the location pointed by TOP.

Pop Operation
● The pop operation is used to delete the topmost element from
the stack.
● To delete the topmost element, we first check if TOP=NULL.
● If the condition is false, then we decrement the value pointed by
TOP.

Step 1, we first check for the UNDERFLOW condition.


Step 2, the value of the location in the stack pointed by TOP is stored
Peek Operation
● Peek is an operation that returns the value of the topmost
● element of the stack without deleting it from the stack.
Linked List Operations on Stacks
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.
● All insertions and deletions are done at the node pointed by
TOP.
● If TOP = NULL, then it indicates that the stack is empty.
OPERATIONS ON A LINKED STACK
A linked stack supports all the three stack operations, that is,
push, pop, and peek.
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.

● To insert an element with value 9, 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.
● However, if TOP!=NULL, then we insert the new node at the
beginning of the linked stack and name this new node as TOP.
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 stored in the node.
In Step 3, we check if the new node is the first
node of the linked list.

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.
Pop Operation
● The pop operation is used to delete the topmost element from a
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.
● If an attempt is made to delete a value from a stack that 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.
Step 1, we first check for the UNDERFLOW condition.
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.

You might also like