26 - JavaScript - Stack & Queue

26 - JavaScript - Stack & Queue

Stack

A Stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. It means that the element which is inserted last will be the first one to be removed. A Stack is similar to a stack of plates, where you can only access the top plate, and you can only add or remove plates from the top.

A Stack has two main operations, namely, Push and Pop. The Push operation inserts an element at the top of the Stack, and the Pop operation removes an element from the top of the Stack. There is also another operation called Peek, which allows you to access the top element without removing it.

Here is an example of a Stack with the elements 5, 8, 2, 4, and 1:

   +---+
5  | 1 |
   +---+
4  | 4 |
   +---+
3  | 2 |
   +---+
2  | 8 |
   +---+
1  | 5 |
   +---+
     T

In this example, 5 was the first element pushed into the Stack, followed by 8, then 2, and so on. When we Pop an element from the Stack, we always remove the top element, which in this case is 1.

Push Operation

To illustrate the Push operation, let's add the number 9 to the Stack:

   +---+
6  | 9 |
   +---+
5  | 1 |
   +---+
4  | 4 |
   +---+
3  | 2 |
   +---+
2  | 8 |
   +---+
1  | 5 |
   +---+
     T

As you can see, the number 9 is added to the top of the Stack, and it becomes the first element that will be removed if we Pop an element from the Stack.

Pop Operation

To illustrate the Pop operation, let's remove the top element, which is 9:

   +---+
5  | 1 |
   +---+
4  | 4 |
   +---+
3  | 2 |
   +---+
2  | 8 |
   +---+
1  | 5 |
   +---+
     T

As you can see, the number 9 is removed from the Stack, and the number 1 becomes the new top element.

Peek Operation

To illustrate the Peek operation, let's see what the top element of the Stack is without removing it:

   +---+
5  | 1 |
   +---+
4  | 4 |
   +---+
3  | 2 |
   +---+
2  | 8 |
   +---+
1  | 5 |
   +---+
     T

As you can see, the top element of the Stack is 1, and it remains unchanged.

Queue

A Queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. It means that the element which is inserted first will be the first one to be removed. A Queue is similar to a queue of people waiting in line, where the first person who enters the line is the first one to leave, and new people can only enter the queue at the end.

A Queue has two main operations, namely, Enqueue and Dequeue. The Enqueue operation inserts an element at the end of the Queue, and the Dequeue operation removes an element from the front of the Queue. There is also another operation called Peek, which allows you to access the front element without removing it.

Here is an example of a Queue with the elements 5, 8, 2, 4, and 1:

     +---+---+---+---+---+
  T  | 1 | 4 | 2 | 8 | 5 |  H
     +---+---+---+---+---+

In this example, 5 was the first element Enqueued into the Queue, followed by 8, then 2, and so on. When we Dequeue an element from the Queue, we always remove the front element, which in this case is 1.

Enqueue Operation

To illustrate the Enqueue operation, let's add the number 9 to the Queue:

     +---+---+---+---+---+---+
  T  | 1 | 4 | 2 | 8 | 5 | 9 |  H
     +---+---+---+---+---+---+

As you can see, the number 9 is added to the end of the Queue, and it becomes the last element that will be removed if we Dequeue an element from the Queue.

Dequeue Operation

To illustrate the Dequeue operation, let's remove the front element, which is 1:

     +---+---+---+---+---+
  T  | 4 | 2 | 8 | 5 | 9 |  H
     +---+---+---+---+---+

As you can see, the number 1 is removed from the front of the Queue, and the number 4 becomes the new front element.

Peek Operation

To illustrate the Peek operation, let's see what the front element of the Queue is without removing it:

     +---+---+---+---+---+
  T  | 4 | 2 | 8 | 5 | 9 |  H
     +---+---+---+---+---+

As you can see, the front element of the Queue is 4, and it remains unchanged.

Summarizing Up

Stack and Queue are both linear data structures used in computer science for storing and organizing data. A Stack follows the Last-In-First-Out (LIFO) principle, where the most recently added element is the first one to be removed. A Queue, on the other hand, follows the First-In-First-Out (FIFO) principle, where the first element added is the first one to be removed. Both data structures have fundamental operations like push/pop for Stack and enqueue/dequeue for Queue, as well as additional operations like peek, isFull, and isEmpty. Stacks and Queues are essential building blocks of many algorithms and data structures used in programming, from web browsers to operating systems.

Did you find this article valuable?

Support Bosonique ITEdTech by becoming a sponsor. Any amount is appreciated!