Read: Class 10 Stacks & Queues - 401-advanced-javascript-muna/amman-javascript-401d1 GitHub Wiki

Stacks & Queues

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.

Stacks follow these concepts:

  • FILO (First In Last Out )

  • LIFO ( Last In First Out)

Stack Visualization

  • When you push something to the stack, it becomes the new top.
  • When you pop something from the stack, you pop the current top and set the next top as top.next.

Push O(1)

  1. You should have the Node that you want to add. Here is an example of a Node that we want to add to the stack. Push to Stack 01
  2. Next, you need to assign the next property of Node 5 to reference the same Node that top is referencing: Node 4
  3. Technically at this point, your new Node is added to your stack, but there is no indication that it is the first Node in the stack. To make this happen, you have to re-assign our reference top to the newly added Node, Node 5.
  4. Congratulations! You completed a successful push of Node 5 onto the stack

Pop O(1)

  1. The first step of removing Node 5 from the stack is to create a reference named temp that points to the same Node that top points to. Pop from Stack 02
  2. Once you have created the new reference type, you now need to re-assign top to the value that the next property is referencing. In our visual, we can see that the next property is pointing to Node 4. We will re-assign top to be Node 4.
  3. We can now remove Node 5 safely without it affecting the rest of the stack. Before we do that though you may want to make sure that you clear out the next property in your current temp reference. This will ensure that no further references to Node 4 are floating around the heap. This will allow our garbage collector to cleanly and safely dispose of the Nodes correctly.
  4. Finally, we return the temp Node that was just popped off.

Peek O(1) When conducting a peek, you will only be viewing the top Node of the stack. Traditionally, you always want to peek before conducting a pop. This will ensure that you do not receive a NullExceptionError on your pop action.

What is a Queue

  • 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

  • LILO Last In Last Out