Stacks

A stack is a linear data structure which you access only at one end.

Picture yourself in line at a cafeteria. The plates are stacked up in a big stainless steel housing, which is spring-loaded. Only one plate is visible, but when you pick it up, the next one pops up. You can always get the top plate (until you run out of plates), but you can't reach down and get the third one from the top.

A stack of data elements works the same way. You can add an element at the top, or you can remove one from the top. You can't reach one at the bottom, or in between, except by removing the ones above it first.

Stack Operations

Classically a stack allows only two operations: By combining these two operations, the application can do whatever it needs to do to the stack. However, other operations may be useful as well.

For example, the application may need to examine the top of the stack, without removing it. It could do so by popping and then immediately pushing. However, a separate top operation would be more efficient and easy to implement.

Implementing a Stack

There are two main ways to implement a stack: Arrays are usually the technique of choice: they are more efficient and easier to code. When you don't know how big an array would need to be, use linked lists.

Uses for Stacks


[home]COBOL Home [crane]Data Structures