Sunday, 25 September 2011

Stack pop operations

(i) top=2 when element A is on the top of the stack
(ii) top is reduced by one when data A is deleted from the stack and current top is 1
(iii) top is again reduced by one when data C is deleted from the stack and current top is 0
(iv) Finally top= -1 when data C is deleted from the top of the stack

Note: Please note that top= -1 represents an empty stack.


POP ALGORITHM FOR STACKS
algorithm pop()
{
  If top = -1 // if the stack is empty we cannot delete anything from the stack.
 then return -1 to the function call //-1 stands for "Stack is empty or underflow";
 Endif
 Else
 x = stack[top] //x is the local variable which stores the deleted data
 top=top-1
 End
}


//Write a pop() function logic for stack program
//pop()
template<class T>
T stack<T>::pop()
{
 T x;
 if(isempty())
  return(-1);
 else
 {
  x=s[top];
  top--;
  return(x);
 }
}


Stacks


Basic stack operation

No comments:

Post a Comment