Stacks and Queues
What is a Stack?
Stack is a data structure that follows the property of First In Last Out (FILO). So the first element inserted into a stack will be the last element deleted from the stack. You can think of a stack as a stack of dishes. The first dish that goes into the stack will be the last one to be used.
Stack has two main operations:
push() - insert an element into a stack
pop() - delete the most recently inserted element
Stack is usually implemented as an array or linked list.
My implementation of a stack as an array is as follows:
What is a Queue?
Queue is a data structure that follows the property of First In First Out (FIFO). So the first element inserted into a queue will be the first element deleted from the queue. Imagine a group of people waiting in line. The first person to go in the line will be the first person to go out of the line.
Queue has two main operations:
enqueue() - insert an element into a queue
dequeue() - delete the oldest element in a queue
Queue is usually implemented as an array or linked list.
My implementation of a queue as a linked list is as follows: