The Stack Data Structure

The Stack Data Structure Featured Image

Welcome back to yet another tutorial on data structures. In this 4th installment of the series, we’ll take a look at our first complex data structure – the stack. Don’t worry! It’s not actually complicated. It just builds on our knowledge of linked lists. So if you’re not familiar with linked lists, now is probably a good time to revisit them.

Table of Contents

What is a Stack?

If we think back to our lesson on methods, we’ll remember that Java stores method calls on the stack. But what exactly is a stack? Like its name implies, a stack is a data structure which resembles a stack of items. We can think about it like a pile of papers or a stack of pancakes. That’s because a stack only allows us to add or remove items from the top. As a result, stacks really only support two operations: pop and push.

As we can probably imagine, the push operation involves placing a new item on top of the stack. Meanwhile, the pop operation performs the opposite. These two functionalities alone are all we need to create a stack. However some stacks have additional features like peek which allows us to check the top node without having to pop it off the top.

Properties of Stacks

Stacks are basically just special case linked lists (though they can be implemented using arrays). In other words, we keep the same structure of a linked list, but we only expose pop and push. As a result, we can’t traverse a stack. Instead, we interact with the data structure exclusively through the top element.

Restricting a linked list gives us some pretty interesting new properties. For one, a stack is a last-in first-out (LIFO) data structure. This structure allows us to keep track of the state of a system in a recursive manner. For example, Java uses braces to define code blocks. However, sometimes we nest several levels before we close a block. If we want to track these braces, we might intuitively think that we can just count the number of open and close braces. If the numbers match, we’re good. Unfortunately, that sort of method would fail. After all, starting a program with a close brace is a compilation error.

Instead, we can track the braces using a stack. Every time we encounter an open brace, we pop it on top. If we encounter a close brace, we peek at the top for any open braces. If one exists, we pop it off and continue. Otherwise, we know we have a compilation error! 🙂

Applications of Stacks

Due to the recursive nature of the stack, we have quite a few fun applications for them. The classic example is reversing a string. We would simply push each letter onto the stuck and then pop them back out. That reversing behavior actually comes in handy if we ever want to reverse the state of a system. Think undo. In fact, the universal undo command (ctrl + z) is likely implemented using a stack.

We can actually use a stack to implement backtracking as well. Think about maze traversal. In a maze, we start by storing each move on a stack. Eventually, we’ll run out of moves. If the maze is not solved by that point, we would then backtrack until we hit a location where we had some unused moves. This process would continue until we solved the maze.

Of course, compilers make heavy use of stacks for matching braces. This is especially applicable for languages like Java and C/C++ which use braces to signify code blocks.

Java Stack Syntax

Like linked lists, Java also has support for the Stack data structure. Stacks come in two primary forms: static and dynamic. We can really think about these two implementations as arrays vs. linked lists. For the purposes of this tutorial, our stack will be implemented as a linked list (aka dynamic). For more information on static stacks, feel free to check out this implementation of an ArrayStackOpens in a new tab. in Java.

Class Definition

Because a stack is really a linked list in disguise, the class definition is going to look quite similar.

public class Stack {
  private Node top;
}

And that’s pretty much it. Notice that we actually hide the top node. We only want to expose push and pop for the purposes of encapsulating the underlying linked list structure.

The Node class we’re using is the same node from the last lesson. It’s only going to work with integers as generic types are still a bit out of scope. However, we’ll get around to those soon enough!

Insertion

As stated before, there’s really only one way to perform an insertion on a stack. It’s called a push. A push is clearly a constant time operation. We simply insert a node at the front of the list and reset the head.

public void push(int value) {
  this.top = new Node(value, this.top);
}

With this method, we can push values on top of the stack forever. When we want to start clearing it, we’ll need an implementation of pop.

Deletion

Since stacks are so simple, it makes sense to show off at least one more piece of syntax: deletion. Like insertion, deletion is a fixed cost operation which we call pop.

public int pop() {
  if (this.top != null) {
    Node toRemove = this.top;
    this.top = this.top.getNext();
    return toRemove.getPayload();
  } else {
    throw new NoSuchElementException();
  }
}

In the case that the stack is empty, we throw an exception. Otherwise, we remove the node and return the element to the user.

Summary

That’s about all we have for stacks. As always, here’s the breakdown of the typical data structure operations and their Big O estimates. Like linked lists, stacks perform great with insertion and deletion. However, they don’t really support access and search. It’s possible to implement both of those features, but then we’d essentially have a linked list.

AlgorithmRunning Time
AccessO(N)
InsertO(1)
DeleteO(1)
SearchO(N)

Thanks for sticking around for another tutorial on data structures. Up next we’ll cover the queue!

Data Structures in Java (6 Articles)—Series Navigation

The Data Structures in Java series is a beginner friendly tutorial series which covers topics such as arrays, linked lists, stacks, and queues. By the end of the series, students should feel confident enough to choose the right data structure for their needs.

The inspiration for this series came from an intern who was looking to learn a little more about data structures. In addition to teaching them, I put together these articles. Now everyone can learn a little bit about data structures.

As always, if there’s a data structure you’d like to learn about, don’t be afraid to ask. There will be plenty of people who will be glad you did. Also, don’t forget to share this series with your friends. It helps the website grow.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts