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

sudiptasamanta86493 24 views 7 slides Aug 02, 2024
Slide 1
Slide 1 of 7
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7

About This Presentation

Ppt of stack


Slide Content

` 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 : Push() Pop() Peek() isFull () 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 T he 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 T he 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 !