Linked Implementation of Stack

  • Implement the core operations of stack efficiently (linked base).

A singly linked list referenced by a head pointer naturally lends itself to the implementation of the Stack ADT.

Exercise Think about how to implement a stack using a singly linked list such that the core operations are constant time.

OperationHow?Time
push$\Omicron(1)$
pop$\Omicron(1)$
top$\Omicron(1)$
empty$\Omicron(1)$
Solution

The top of the stack would be the HEAD node of the singly linked list. This works because every time we want to push an additional value to the stack, we would create a new node and set it as the new HEAD node (with its .next pointing to the original HEAD node, or NULL if the stack was empty). Then when we want to pop a value from the top of the stack, we set the HEAD to the current HEAD node's .next. As such, the HEAD node will always be at the top of the stack, and calling top would return the value of the HEAD node.

OperationHow?Time
pushprepend the list and update the head$\Omicron(1)$
popdelete from front: head = head.next$\Omicron(1)$
topreturn head.data$\Omicron(1)$
emptycheck if head is null$\Omicron(1)$