0% found this document useful (0 votes)
9 views

STACK

Uploaded by

Shivam Maurya
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)
9 views

STACK

Uploaded by

Shivam Maurya
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/ 16

STACK

What is Stack

 Stack is a collection of similar data items in which both insertion and


deletion operation are performed based on LIFO principle.
 Stack is an ordered list of the same type of elements.
 In a stack , when an element is added , it goes to the top of the stack.
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.
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 inserting an element in a stack, we check whether the stack is full.


 If we try to insert the element in a stack, and the stack is full, then
the overflow condition occurs.
 When we initialize a stack, we set the value of top as -1 to check that the stack is
empty.
 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.
 The elements will be inserted until we reach the max size of the stack.
Stack Push Operation
Stack Pop 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

 void push (int val,int n) //n is size of the stack


 {
 if (top == n )
 printf("\n Overflow");
 else
 {
 top = top +1;
 stack[top] = val;
 }
 }
Pop Algorithm

 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]);
}
}
}

You might also like