`
Stack
Sudipta Samanta
27500123050
Data Structure & Algorithm
PCC-CS301
2024-2025
What is a stack ?
 Stack is a linear data structure in which an element is inserted or deleted only
from the Top position.
Stack is based on last in first out (LIFO) property . it means the element inserted
first will be the last one to delete.
Basic operations of stack :
1. Push()
2. Pop()
3. Peek()
4. isFull()
5. isEmpty()
Implement of Push
operation:
void push()
{
If(top==maxsize-1)
{
printf(“Stack is full”);
return 0;
}
int num;
printf(“enter the element to be pushed”)
}
Scanf(“%d”,& num);
Top=top+1;
Stack[top]=num;
}
Push operation
The process of putting a new data element onto stack is known as push operation.
Algorithm for push operation:
Step 1- Check if the stack is full.
Step 2- If the stack is full, produces
an error and exit.
Step 3- If the stack is not full,
increments top to point next empty
space.
Step 4- Adds data element to the
stack location, where top is
pointing.
Step 5- Returns success.
Implement of Pop
operation:
void pop()
{
If(top== -1)
{
printf(“Stack is empty”);
return -1;
}
int num;
num=stack[top];
printf(“Element poped from stack: %d”,num);
top=top-1;
}
Pop operation
The process of deleting an element from the top of the stack is called pop operation.
After every pop operation, the top of stack is decremented by 1.
Algorithm for pop operation:
Step 1- Check if the stack is empty.
Step 2- If the stack is empty,
produces an error and exit.
Step 3- If the stack is not empty,
accesses the data element at which
top is pointing.
Step 4- Decreases the value of top
by 1.
Step 5- Returns success.
 Peek Operation: Returns the value of the top element without
removing it from the stack.
int peek()
{
return stack[top];
}
 isFull() Operation: Returns true if the stack is full.
bool isFull()
{
if(top==Maxsize)
return true;
else{
return false;
}
 isEmpty() Operation: Returns true if the stack is empty
and false if it’s not.
bool isEmpty()
{
if(top==-1)
return true;
else{
return false;
}
Stack Errors
Stack Overflow:
when stack is completely full (i.e Top=maxsize-1) and we try to insert
more elements onto stack then this condition is called overflow condition.
Stack underflow:
when stack is empty(i.e Top= -1) and we try to delete more element from
it then this condition is called underflow condition.
Thank You !

stack_ppt_DSA(sudipta samanta).pptx push,pop,peek operation

  • 1.
  • 2.
    What is astack ?  Stack is a linear data structure in which an element is inserted or deleted only from the Top position. Stack is based on last in first out (LIFO) property . it means the element inserted first will be the last one to delete. Basic operations of stack : 1. Push() 2. Pop() 3. Peek() 4. isFull() 5. isEmpty()
  • 3.
    Implement of Push operation: voidpush() { If(top==maxsize-1) { printf(“Stack is full”); return 0; } int num; printf(“enter the element to be pushed”) } Scanf(“%d”,& num); Top=top+1; Stack[top]=num; } Push operation The process of putting a new data element onto stack is known as push operation. Algorithm for push operation: Step 1- Check if the stack is full. Step 2- If the stack is full, produces an error and exit. Step 3- If the stack is not full, increments top to point next empty space. Step 4- Adds data element to the stack location, where top is pointing. Step 5- Returns success.
  • 4.
    Implement of Pop operation: voidpop() { If(top== -1) { printf(“Stack is empty”); return -1; } int num; num=stack[top]; printf(“Element poped from stack: %d”,num); top=top-1; } Pop operation The process of deleting an element from the top of the stack is called pop operation. After every pop operation, the top of stack is decremented by 1. Algorithm for pop operation: Step 1- Check if the stack is empty. Step 2- If the stack is empty, produces an error and exit. Step 3- If the stack is not empty, accesses the data element at which top is pointing. Step 4- Decreases the value of top by 1. Step 5- Returns success.
  • 5.
     Peek Operation:Returns the value of the top element without removing it from the stack. int peek() { return stack[top]; }  isFull() Operation: Returns true if the stack is full. bool isFull() { if(top==Maxsize) return true; else{ return false; }  isEmpty() Operation: Returns true if the stack is empty and false if it’s not. bool isEmpty() { if(top==-1) return true; else{ return false; }
  • 6.
    Stack Errors Stack Overflow: whenstack is completely full (i.e Top=maxsize-1) and we try to insert more elements onto stack then this condition is called overflow condition. Stack underflow: when stack is empty(i.e Top= -1) and we try to delete more element from it then this condition is called underflow condition.
  • 7.