Stacks

Introduction

One of the simplest and most commonly-used data structures is the Stack. In a stack, data is added and removed from one end only (typically called the top). Logically, the top item is the only one that can even be seen. Real-life examples of stacks can include:

The fundamental operations of a stack are:

See the StackInterface.java.

A Stack organizes data by Last In First Out, or LIFO (or FILO - First In Last Out). This access, although simple, is useful for a variety of problems. Let's look at a few applications before we discuss the implementation:

Implementation

A Stack can easily be implemented using either an array or a linked list:

In the Java Collections Framework, the Stack class extends the Vector class, defining the stack operations appropriately. Unfortunately, all Vector operations are still available, allowing the user to violate stack restrictions. Later, we will see that the Queue ADT in Java is (more appropriately) an interface. The difference between the stack implementation and the queue implementation in Java is due to history and backward compatibility. What do you think the tradeoffs are between Java's stack implementation and their queue implementation.

<< Previous Notes Daily Schedule Next Notes >>