# Class 38: Stacks

Back to Introdution to Linear Structures. On to Stack lab.

Held Tuesday, April 13

Summary

• Implementing stacks

Contents

Handouts

Notes

## Implementation

• So, how do we implement all of these structures?
• We could start with an existing structure, such as a list.
• However, we should not extend any of the lists, because these are definitely not lists. (For example, some would argue that you should never iterate a stack, queue, or such.)
• We would start again from scratch, using arrays, nodes and links, or some other structure.
• Over the next few days, we'll visit some alternatives.

## Implementing Stacks

• The implementation we choose may depend on our knowledge of the use of the stack (e.g., are we going to have a limited or unlimited size?) and the limitations of our programming language (e.g., does it support dynamic data allocation).
• The two most basic ways to implement stacks are with vectors and lists.

### List-Based Stacks

• A list-based implementation is relatively straightforward, as many list implementations include all the standard stack operations.
• `push(o)` is just another name for `insertAtFront(o)`
• Of course, you need `insertAtFront`
• `pop()` is just another name for `removeFromFront()`
• Of course, you need `removeFromFront`
• `peek()` is just another name for `front()`
• You could also reverse this:
• `push(o)` is just another name for `addToEnd(o)`
• `pop()` is just another name for `deleteLast()`

### Vector-Based Stacks

• Array-based (or vector-based) implementations are nearly as easy. We simply add elements to the end of the array, and remove elements from the end of the array.
• We may need to keep track of the position of the last element, but we can also use the `size()` or `length` to determine that.
• When representing stacks, the bottom (front) of the stack is always at position 0, since it's the first element added and the last element removed.

### Node-Based Stacks

• We can also implement stacks using nodes like those used in linked lists. We'll explore those today.
• What is a node? At the simplest level, it's simply a pair of contents and next.
• Some design decisions for nodes:
• What constructors should we support? Clearly, we should support one that takes both contents and next. Should we also support one that takes only contents and gives a ``default'' next node? If so, what would that default be?
• Should we support modifiers? While it is standard practice to include setXXX methods, perhaps we don't want to allow users to change nodes once they're created. (After all, a client could also create a new node ....)
• Once we have nodes, we can think about the stack operations.
• A node-based stack will contain two fields:
• The node at the top of the stack. This will be `null` for empty stacks.
• The size of the stack. (Not necessary, but might be helpful.)
• The constructor will create a new, empty, stack. It need not take any parameters.
• There are three standard accessors: `isFull`, `isEmpty`, and `peek`.
• Since we're using a dynamic implementation, the stack is never full.
• We've decided to make the top `null` when the stack is empty.
• We can peek at the top of the stack by looking at the contents of the top node.
• There are two standard modifiers: `add` and `remove`
• We can add an element to the top of the stack by
• Creating a new node to hold that element.
• Making the next node the old top of the stack
• Making the new node the top of the stack
• We can remove an element from the top of the stack by
• Getting the top element
• Setting top the next element
• Node that we could write add as
```  public void add(Object elt) {
Node newNode = new Node(elt,top);
top = newNode;
}
```
or more succinctly as
```  public void add(Object elt) {
top = new Node(elt,top);
}
```
• And that's it
• Should we also support standard methods?
• A `toString` method?
• An `equals` method?
• A `clone` method?

History

• Created Monday, January 11, 1999.
• Added short summary on Friday, January 22, 1999.
• Filled in many of the details on Tuesday, April 13, 1999.
• On Wednesday, April 14, 1999, moved some uncovered materials to the next outline.

Back to Introdution to Linear Structures. On to Stack lab.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.