Implementation
A stack adds and removes at one end: last in, first out. A queue adds at the back and removes at the front: first in, first out. Both can sit on an array (with a top index, or a head index that wraps round) or on a linked list. Every operation is O(1).
How to recognise it
- The problem asks you to build push, pop, top or enqueue and dequeue yourself.
- Things must come back out in reverse order (stack) or arrival order (queue).
- A recursive solution needs to hold values while it works on the rest: the call stack is a stack.
- Several stacks or queues must share one fixed block of memory.
The two shapes
| Adds at | Removes from | Order out | |
|---|---|---|---|
| Stack | top | top | newest first (LIFO) |
| Queue | back | front | oldest first (FIFO) |
A stack of plates and a line at a ticket counter are the usual pictures. A deque (double-ended queue) allows both ends, so it can act as either.
On an array
- Stack: keep
size. The top isdata[size - 1]. Push writesdata[size]and adds 1; pop subtracts 1. When the array is full, a growing stack copies into an array twice as big; a fixed stack reports overflow. - Queue: keep
headandsize. The front isdata[head]and the back slot after it is(head + size) mod capacity. Removing movesheadforward by one, wrapping to 0. This wrap is what makes it a circular queue: no slot is wasted and nothing is shifted.
On a linked list
- Stack: push puts a new node before the head; pop removes the head.
- Queue: keep
headandtail. Add after the tail, remove the head. When the last node leaves, set the tail to null as well.
Sharing one array
Two stacks can grow towards each other from the two ends; the array is full when the tops meet. For k stacks or
queues, give every slot a next index. Free slots form one linked list, and each stack (or queue) is its own linked
list through the same array.
The call stack is a stack
A recursive function that takes a value off, recurses, and puts it back is using the call stack to hold values. That is how you can insert at the bottom of a stack, reverse it or sort it with no second container.
Common mistakes
- Forgetting the empty check before pop or top.
- In an array queue, moving items left after every removal (O(n)) instead of moving
head. - In a list queue, leaving
tailpointing at a removed node once the queue is empty.
Problems
- Implement Stack using Arrayseasyguide ready
- Implement Queue using Arrayseasyguide ready
- Implement stack using Linkedlisteasyguide ready
- Implement queue using Linkedlisteasyguide ready
- Implement Stack from Scratcheasyguide ready
- Implement Queue from Scratcheasyguide ready
- Implement 2 stack in an arrayeasyguide ready
- find the middle element of a stackeasyguide ready
- Implement "N" stacks in an Arrayeasyguide ready
- Reverse a String using Stackeasyguide ready
- Implement a method to insert an element at its bottom without using any other data structureeasyguide ready
- Reverse a stack using recursioneasyguide ready
- Sort a Stack using recursioneasyguide ready
- Implement Stack using Dequeeasyguide ready
- Stack Permutations (Check if an array is stack permutation of other)easyguide ready
- Implement "n" queue in an arrayeasyguide ready
- Implement a Circular queueeasyguide ready
- Reverse a Queue using recursioneasyguide ready
- Reverse the first âKâ elements of a queueeasyguide ready
- Interleave the first half of the queue with second halfeasyguide ready
- Queue based approach or first non-repeating character in a streameasyguide ready
- Find the Celebritymediumguide ready