Read: Stacks & Queues - 401-advanced-javascript-dania/amman-javascript-401d1 GitHub Wiki
What is a Stack A stack is a data structure that consists of Nodes. Each Node references the next Node in the stack, but does not reference its previous.
Common terminology for a stack is
- Push - Nodes or items that are put into the stack are pushed
- Pop - Nodes or items that are removed from the stack are popped. When you attempt to pop an empty stack, you will receive a NullReferenceException
- Top - This is the top of the stack.
- Peek - When you peek you will view the top Node in the stack. FILO First In Last Out
This means that the first item added in the stack will be the last item popped out of the stack.
LIFO Last In First Out
This means that the last item added to the stack will be the first item popped out of the stack.
Push O(1)
Pushing a Node onto a stack will always be an O(1) operation. This is because it takes the same amount of time no matter how many Nodes (n) you have in the stack.
When adding a Node, you push it into the stack by assigning it as the new top, with its next property equal to the original top.
Pop O(1)
Popping a Node off a stack is the action of removing a Node from the top. When conducting a pop, the top Node will be re-assigned to the Node that lives below and the top Node is returned to the user.
What is a Queue Common terminology for a queue is
- Enqueue - Nodes or items that are added to the queue.
- Dequeue - Nodes or items that are removed from the queue.
- Front - This is the front/first Node of the queue.
- Rear - This is the rear/last Node of the queue.
- Peek - When you peek you will view the top Node in the queue. If the queue is empty, and you don’t peek, you will receive a NullReferenceException. Queues follow these concepts:
FIFO First In First Out
This means that the first item in the queue will be the first item out of the queue.
LILO Last In Last Out
This means that the last item in the queue will be the last item out of the queue.
Prerequisites
You should include a requirement in both your Stack and Queue class to guarantee that you have at least one Node starting out. Don’t forget to assign the proper properties to the correct locations in both the stack and the queue.