Thursday, June 27, 2013

Write the algorithm to pop an item from the stack.



Let S[N] be the stack with size N of type T. ‘top’ is an integer variable that indicate the top of the stack. Then the algorithm to pop the item from the stack is as follows:
1.   Start
2.   Check stack underflow
if(top==-1) THEN
print “Stack is empty” and exit, else
3.   Store the top element in the temporary variable ‘temp’
temp= S[top];
4.   Decrement ‘top’ by 1
top--;
5.   Display the pop item, ‘temp’.
6.   Exit.

No comments:

Post a Comment