A Stack is an abstract data type for data storage. It follows the LIFO or last-in-first-out order for retrieving data. Elements that are added at the end are removed first.
The illustration below shows how a stack works:
Step 1: The stack is initially empty.
Step 2: A push operation adds an item to the top of the stack. The top shifts one place above its previous position.
Step 3: Each subsequent push adds an item to the top of the stack.
Step 4: A pop command removes an item from the top-1 position of the stack. The top shifts one place below its previous position.
A stack implemented using a linked list does not have an upper bound on the number of items that can be added. An item is added to the head of the linked list using the push operation. Therefore, with each addition, the head pointer changes. Similarly, the head of the linked list is removed using the pop operation. The head pointer then points to the second node in the linked list.
The illustration below shows how a linked list-based stack works:
A linked list basedstack implementation can be broken down into several functions. The purpose of each function is discussed below:
| Function | Purpose |
|---|---|
Size |
Returns the number of items in the stack |
Push |
Adds a data item to the stack |
Pop |
Removes a data item from the stack |
IsEmpty |
Returns true if the stack is empty. Else, returns false |
Peek |
Returns the value at the top of the linked list but does not remove it |
Print |
Displays all elements of the stack |
The functions used to implement an linked list based stack are defined as follows:
func (s *StackLinkedList) Size() int {}
func (s *StackLinkedList) IsEmpty() bool {}
func (s *StackLinkedList) Peek() (int, bool) {}
func (s *StackLinkedList) Push(value int) {}
func (s *StackLinkedList) Pop() (int, bool) {}
func (s *StackLinkedList) Print() {}
The code below shows an linked list based implementation of the stack in Golang:
package mainimport "fmt"type Node struct {value intnext *Node}type StackLinkedList struct {head *Nodesize int}// Size() functionfunc (s *StackLinkedList) Size() int {return s.size}// IsEmpty() functionfunc (s *StackLinkedList) IsEmpty() bool {return s.size == 0}// Peek() functionfunc (s *StackLinkedList) Peek() (int) {if s.IsEmpty() {fmt.Println("StackEmptyException")return 0}return s.head.value}// Push() functionfunc (s *StackLinkedList) Push(value int) {s.head = &Node{value, s.head}s.size++}// Pop() functionfunc (s *StackLinkedList) Pop() (int, bool) {if s.IsEmpty() {fmt.Println("StackEmptyException")return 0, false}value := s.head.values.head = s.head.nexts.size--return value, true}// Print() functionfunc (s *StackLinkedList) Print() {temp := s.headfmt.Print("Values stored in stack are: ")for temp != nil {fmt.Print(temp.value, " ")temp = temp.next}fmt.Println()}func main() {var stack StackLinkedList // create a stack variable of type StackLinkedList/* Adding items to stack */stack.Push(1)stack.Push(2)stack.Push(3)stack.Print() // Print the stackfmt.Println("Checking length of stack:")fmt.Println(stack.Size()) // Get Lengthfmt.Println("Removing an Item:")stack.Pop() // Remove an itemstack.Print()fmt.Println("Getting Item at Top of Linked List")fmt.Println(stack.Peek()) // Get Top-most item}
StackLinkedList is created named stack. It refers to a head node and has an int variable to keep track of the size of the stack.push function creates a new head node. It assigns it the int value that is passed in the parameter. In our case, we push three values separately into the stack.length function returns the number of elements in the stack.pop function first checks if the stack is empty. It raises an exception in that case. Otherwise, it makes the second node in the linked list the head node and deletes the first node. It also decrements the size variable by one.peek function returns the value of the head node of the linked list.print function iterates over each node and prints its value.Let us examine the time complexity for each operation in a linked list-based stack:
| Operation | Time Complexity |
|---|---|
IsEmpty |
|
Peek |
|
Size |
|
Push |
|
Pop |
|
Print |
Free Resources