Stacks

David Medina
2 min readApr 26, 2021

Stacks are a kind of linear data structure, a collection of nodes that adds data to the top of the stack. A stack can be thought of like a stack of books, where you can only pick up the top book, and add a new book to the top.

Stacks are a data structure that contains an ordered set of data, where it provides three methods for interaction:

  • Push — adds data to the top or head of the stack.
  • Pop — returns and removes data from the top or head of the stack.
  • Peek — returns data from the top or head of the stack without removing it.

A Stack can mimic a set of gym weights. Each plate has a weight amount (the data). The first plate you add or push, onto the floor is both the bottom and top of the stack, each weight added becomes the new top or head of the stack. The only weight you can remove, or pop, from the stack, is the top one. You can also peek and read the top weight without removing it from the stack.

A less frequently used term is First In, Last Out, or FILO.

Implementation

Depending on the implementation, the top or head of the stack is equivalent to the head node of a linked list and the bottom of the stack is equivalent to the tail node.

Stacks can be implemented using a linked list as the underlying data structure because it’s more efficient than a list or array.

On the other hand, a constraint that may be placed on a stack is its size. Attempting to push data onto an already full stack will result in a stack overflow. Similarly, if you attempt to pop data from an empty stack, it will result in a stack underflow.

--

--