Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

STACK OPERATIONS

A Stack is a linear data structure that follows the LIFO (Last-


In-First-Out) principle. Stack has one end, whereas the Queue
has two ends (front and rear). It contains only one pointer top
pointer pointing to the topmost element of the stack. Whenever
an element is added in the stack, it is added on the top of the
stack, and the element can be deleted only from the stack. In
other words, a stack can be defined as a container in which
insertion and deletion can be done from the one end known as
the top of the stack.
Example Piles of Books

Working of Stack Data Structure


The operations work as follows:
1. A pointer called TOP is used to keep track of the top element in
the stack.
2. When initializing the stack, we set its value to -1 so that we can
check if the stack is empty by comparing TOP == -1.
3. On pushing an element, we increase the value of TOP and place
the new element in the position pointed to by TOP.
4. On popping an element, we return the element pointed to
by TOP and reduce its value.
5. Before pushing, we check if the stack is already full
6. Before popping, we check if the stack is already empty
Basic Operations of Stack
There are some basic operations that allow us to perform different actions on
a stack.

 Push: Add an element to the top of a stack


 Pop: Remove an element from the top of a stack
 IsEmpty: Check if the stack is empty
 IsFull: Check if the stack is full
 Peek: Get the value of the top element without removing it
PUSH operation
The steps involved in the PUSH operation is given below:
o Before inserting an element in a stack, we check whether the
stack is full.
o If we try to insert the element in a stack, and the stack is full,
then the overflow condition occurs.
o When we initialize a stack, we set the value of top as -1 to
check that the stack is empty.
o When the new element is pushed in a stack, first, the value
of the top gets incremented, i.e., top=top+1, and the element
will be placed at the new position of the top.
o The elements will be inserted until we reach the max size of
the stack.
POP operation
The steps involved in the POP operation is given below:
o Before deleting the element from the stack, we check
whether the stack is empty.
o If we try to delete the element from the empty stack, then
the underflow condition occurs.
o If the stack is not empty, we first access the element which
is pointed by the top
o Once the pop operation is performed, the top is decremented
by 1, i.e., top=top-1.

Advantages (Pros)
* Easy to get started
* Low Hardware Requirement

* Cross- Platform

* Anyone with access can edit the program

Disadvantages (Cons)

* Inflexible

* Lack of scalability

* Unable to Copy & Paste

Study stack program in c++ For exam point of you

You might also like