Back

Stack Pseudocode

Stacks can be implemented using an array data structure, with each element of the array representing an item in the stack. New items are placed on the stack using push and items are removed using pop. A variable ‘top’ holds the position of the topmost item in the stack, and size holds the maximum number of items the stack can hold.

S is the stack being operated on and x is the item to add to the stack.

  • Push
  •         Push(S,x)
            IF top == size THEN
              Stack is full
            ELSE
            BEGIN
              top++
              S[top] = x
            END 
  • Pop
  •     Pop(S) 
        IF top == 0 THEN 
          Stack is empty 
        ELSE 
        BEGIN 
          x = S[top] 
          top-- 
          return x 
        END