STACK DATA STRUCTURE
STACK DATA STRUCTURE
STRUCTURE
STACK DATA STRUCTURE
A stack is a linear data structure that follows
the principle of Last In First Out (LIFO). This
means the last element inserted inside the
stack is removed first.
Here, you can:
Put a new plate on top
Remove the top plate
LIFO PRINCIPLE OF STACK
In programming terms, putting an item on top of the stack is called push
and removing an item is called pop.
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
WORKING OF STACK DATA STRUCTURE
TYPES OF STACK DATA STRUCTURE
Fixed Size Stack : As the name suggests, a fixed size stack has a fixed
size and cannot grow or shrink dynamically. If the stack is full and an
attempt is made to add an element to it, an overflow error occurs. If the
stack is empty and an attempt is made to remove an element from it,
an underflow error occurs.
Dynamic Size Stack : A dynamic size stack can grow or shrink
dynamically. When the stack is full, it automatically increases its size to
accommodate the new element, and when the stack is empty, it
decreases its size. This type of stack is implemented using a linked list,
as it allows for easy resizing of the stack.
PUSH OPERATION IN STACK DATA STRUCTURE
Adds an item to the stack. If the stack is full, then it is said to be an Overflow condition.
Implementation of twoStacks should use only one array, i.e., both stacks should use the
pop1() –> pops an element from first stack and return the popped element
pop2() –> pops an element from second stack and return the popped element
IMPLEMENT TWO STACKS IN AN ARRAY BY DIVIDING
THE SPACE INTO TWO HALVES:
Stack 1-----> use arr[0] to arr[n/2]
size of array =n
First Approach
IMPLEMENT TWO STACKS IN AN ARRAY BY DIVIDING
THE SPACE INTO TWO HALVES:
Stack 1-----> will start from 0, extends to the right direction
Stack 2-----> will start from n-1, extends to the left direction
Second Approach
INFIX TO POSTFIX CONVERSION
(a+(b*c)/(d-e))
A+(B*C-(D/E^F)*G)*H
K + L - M*N + (O^P) * W/U/V * T + Q
( ( A + B ) * ( C – D ) + E ) / (F + G)
a+b*(c^d-e)^(f+g*h)-i