Lecture Note-6: Stack Operations 2015
By Rajesh K Shukla, HOD, Department of CSE, SIRT Bhopal
Downloaded from www.RajeshkS...
of 1

Pop operation

Stack operation: Push and POP
Published on: Mar 4, 2016
Published in: Education      

Transcripts - Pop operation

  • 1. Lecture Note-6: Stack Operations 2015 By Rajesh K Shukla, HOD, Department of CSE, SIRT Bhopal Downloaded from www.RajeshkShukla.com Stack Operations There are two basic operations that are usually performed with stacks. 1. PUSH 2. POP Let us discuss POP Operation in detail. POP Operation POP Operation is basically used to remove the elements from the stack. This is the process used to delete the elements from the stack. So when we say pop an element we mean delete an element from the stack. Suppose we want to remove all the five elements from the stack shown in figure 3.6, the graphical representation of such removal from the Stack is shown here in figure 3.7 The most recently inserted element i.e. E is at the Top so it is deleted first from the stack. The second to top element i.e. D is deleted next because the previous top element E is removed so the next older item D in the stack becomes the new Top element. The value of Top is changed after the deletion in Stack takes place. In this way the element inserted first i.e. A is deleted last from the stack. When the last item in the stack is deleted the stack is set to empty state. The elements from the stack are always retrieved in the reverser order as we can see from figure 3.7 the elements are removed in the reverse order (E, D, C, B, A) in which they were pushed (A, B, C, D, E) into the stack as shown in figure 3.6. Removal of an element depends upon its position in the stack; you have to first remove all the elements above it. Therefore you cannot delete B unless all the elements above it i.e. C, D and E are removed. We can see in the figure 3.7 that after every pop operation the stack Top is decremented by one. The most frequently accessible element in the stack is the topmost element whereas the least accessible element is the bottom of the stack. When all the elements from the stack are removed mean stack is empty then we cannot delete anything from the stack. This situation is called Underflow. If the pop operation is performed in the empty stack then the stack underflow condition occurs so one must test whether there is an element in the stack to be deleted. We can perform the pop operation only when the stack is not empty. The algorithm for POP operation is given below as ALGORITHM-2. Let STACK is an array of MAX size and we want to delete the ELEMENT from the STACK ALGORITHM-2: POP (STACK, MAX, TOP, ITEM) Step1. Check for Underflow If Top=-1 then Print “Underflow” and Return Else a. ELEMENT =STACK[Top] // Delete an ELEMENT from the STACK b. Top=Top-1 // Decrement the Top by One End if Step2. Return Fig 3.7: Graphical representation of Stack POP Operation 4 3 2 1 0 E D C B A T o 4 3 2 1 0 3 2 1 0 D C B A T o 2 1 0 C B A T o 4 3 2 1 0 E D C B A T o 4 3 2 1 0 E D C B A T o Top=4(stack is given) Top=3(E is Removed) Top=2(D is Removed) Top=1(C is Removed) Top=0(B is Removed) Top=-1(A is Removed)

Related Documents