STACK
STACK
What is Stack
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.
Working of Stack
Stack Operations
push(): When we insert an element in a stack then the operation is known as a push. If the
stack is full then the overflow condition occurs.
pop(): When we delete an element from the stack, the operation is known as a pop. If the
stack is empty means that no element exists in the stack, this state is known as an
underflow state.
isEmpty(): It determines whether the stack is empty or not.
isFull(): It determines whether the stack is full or not.
peek(): It returns the element at the given position.
count(): It returns the total number of elements available in a stack.
change(): It changes the element at the given position.
display(): It prints all the elements available in the stack
Stack Push Operation
Before deleting the element from the stack, we check whether the stack is empty.
If we try to delete the element from the empty stack, then the underflow condition
occurs.
If the stack is not empty, we first access the element which is pointed by the top
Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1.
Stack Pop Operation
Push Algorithm
begin
if top = n then stack full
top = top + 1
stack (top) : = item;
end
Array Implementation of Push Operation
begin
if top = 0 then stack empty;
item := stack(top);
top = top - 1;
end;
Array Implementation of Pop Operation
int pop ()
{
if(top == -1)
{
printf("Underflow");
return 0;
}
else
{
return stack[top - - ];
}
}
Application of Stack
String reversal
Functions
UNDO/REDO
Recursion
DFS(Depth First Search)
Backtracking
Expression conversion
#include<stdlib.h> void push()
#include<stdio.h> {
int stk[5]; int n;
int top= -1; if (top==4)
void push(); {
void pop(); printf("Stack is Full");
void display(); }
void main() else
{
{
printf("enter the element in the stack");
int choice; scanf("%d", & n);
while(2>1) top++;
{ stk[top]=n;
}
printf("\n 1. The Push Operation");
}
printf("\n 2. The Pop Operation"); void pop()
printf("\n 3. Display"); {
printf("\n 4. Exit"); int x;
if (top==-1)
printf("\n Enter Number for specific choice"); {
scanf("%d",& choice); printf("\n stack is empty");
switch(choice) }
else
{
{
case 1: x=stk[top];
push(); top--;
break; printf("Deleted Element is %d”, %d",&x);
}
case 2:
}
pop(); void display()
break; {
int i;
case 3:
if (top==-1)
display(); {
break; printf("\n stack is empty");
case 4: }
else
exit(0);
for(i=top;i>=0;i++)
Default: {
printf(“Invalid option”); printf("%d", stk[i]);
}
}
}