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.
Stack
Push Operation
The Push() operation adds an element to the top of the Stack. Here's an example of how to implement the Push() operation in JavaScript:
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element);
}
}
let stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack.items);
Output: [1, 2, 3]
Explanation:
We create a new Stack class with an empty array as the initial items.
We define the push() method that takes an element as input and adds it to the end of the items array using the push() method of the array.
We create a new instance of the Stack class called stack.
We call the push() method on the stack object with values 1, 2, and 3 respectively.
We print the items array of the stack object using console.log().
Pop Operation
The Pop() operation removes the top element from the Stack. Here's an example of how to implement the Pop() operation in JavaScript:
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element);
}
pop() {
if (this.items.length == 0)
return "Underflow";
return this.items.pop();
}
}
let stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack.pop());
console.log(stack.items);
Output:
3
[1, 2]
Explanation:
We define the pop() method that removes the last element of the items array using the pop() method of the array.
We add an if statement to check if the length of the items array is 0, in which case we return "Underflow" to indicate that the stack is empty and the pop operation cannot be performed.
We create a new instance of the Stack class called stack.
We call the push() method on the stack object with values 1, 2, and 3 respectively.
We call the pop() method on the stack object, which removes the top element (3) and returns it.
We print the items array of the stack object using console.log().
Peek Operation
The Peek() operation returns the top element of the Stack without removing it. Here's an example of how to implement the Peek() operation in JavaScript:
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element);
}
pop() {
if (this.items.length == 0)
return "Underflow";
return this.items.pop();
}
peek() {
return this.items[this.items.length - 1];
}
}
let stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack.peek());
console.log(stack.items);
Output:
3
[1, 2, 3]
Explanation:
We define the peek() method that returns the last element of the items array without removing it.
We create a new instance of the Stack class called stack.
We call the push() method on the stack object with values 1, 2, and 3 respectively.
We call the peek() method on the stack object, which returns the top element (3).
We print the items array of the stack object using console.log().
Queue
Enqueue Operation
The Enqueue() operation adds an element to the end of the Queue. Here's an example of how to implement the Enqueue() operation in JavaScript:
class Queue {
constructor() {
this.items = [];
}
enqueue(element) {
this.items.push(element);
}
}
let queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
console.log(queue.items);
Output: [1, 2, 3]
Explanation:
We create a new Queue class with an empty array as the initial items.
We define the enqueue() method that takes an element as input and adds it to the end of the items array using the push() method of the array.
We create a new instance of the Queue class called queue.
We call the enqueue() method on the queue object with values 1, 2, and 3 respectively.
We print the items array of the queue object using console.log().
Dequeue Operation
The Dequeue() operation removes the first element from the Queue. Here's an example of how to implement the Dequeue() operation in JavaScript:
class Queue {
constructor() {
this.items = [];
}
enqueue(element) {
this.items.push(element);
}
dequeue() {
if (this.isEmpty())
return "Underflow";
return this.items.shift();
}
isEmpty() {
return this.items.length == 0;
}
}
let queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
console.log(queue.dequeue());
console.log(queue.items);
Output:
1
[2, 3]
Explanation:
We define the dequeue() method that removes the first element of the items array using the shift() method of the array.
We add an if statement to check if the Queue is empty, in which case we return "Underflow" to indicate that the Queue is empty and the dequeue operation cannot be performed.
We define the isEmpty() method that returns true if the length of the items array is 0, indicating that the Queue is empty.
We create a new instance of the Queue class called queue.
We call the enqueue() method on the queue object with values 1, 2, and 3 respectively.
We call the dequeue() method on the queue object, which removes the first element (1) and returns it.
We print the items array of the queue object using console.log().
Peek Operation
The Peek() operation returns the first element of the Queue without removing it. Here's an example of how to implement the Peek() operation in JavaScript:
class Queue {
constructor() {
this.items = [];
}
enqueue(element) {
this.items.push(element);
}
dequeue() {
if (this.isEmpty())
return "Underflow";
return this.items.shift();
}
peek() {
if (this.isEmpty())
return "Underflow";
return this.items[0];
}
isEmpty() {
return this.items.length == 0;
}
}
let queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
console.log(queue.peek());
console.log(queue.items);
Output:
1
[1, 2, 3]
Explanation:
We define the peek() method that returns the first element of the items array without removing it.
We add an if statement to check if the Queue is empty, in which case we return "Underflow" to indicate that the Queue is empty and the peek operation cannot be performed.
We define the isEmpty() method that returns true if the length of the items array is 0, indicating that the Queue is empty.
We create a new instance of the Queue class called queue.
We call the enqueue() method on the queue object with values 1, 2, and 3 respectively.
We call the peek() method on the queue object, which returns the first element (1) without removing it.
We print the items array of the queue object using console.log().
Summarizing Up
Stack and Queue are two fundamental data structures used in computer programming. A Stack is a linear data structure that follows the Last In First Out (LIFO) principle, where the last element added is the first to be removed. The basic operations in a Stack include push (add element), pop (remove element), and peek (look at the top element without removing it). On the other hand, a Queue is a linear data structure that follows the First In First Out (FIFO) principle, where the first element added is the first to be removed. The basic operations in a Queue include enqueue (add element), dequeue (remove element), and peek (look at the first element without removing it).