eWebProgrammer eweb  

Perl Variables  «Prev 

Using an array as a stack in Perl

Stack push

Diagram of push to the stack
Diagram of push to the stack
When you push a new element onto the stack, it goes on at the top, as the new highest-numbered element.

A stack is much like a queue except that you remove the most recently added element rather than the least recently added. The FIFO order has been changed to LIFO (last-in first-out). A typical example (which is why the data structure has this name) is a stack of plates in a cafeteria:
Diners take the top plate from the stack, but when a new plate has been washed, it is put on top of the stack and will be used next.
Stacks are frequently used when operations need to be broken down into suboperations to be executed in sequence. When such a compound operation is encountered, the operation is popped off, and the suboperations are pushed ontocontinue