queue_stack_operations
Version:
Stack and Queue Operations
234 lines (202 loc) • 5.63 kB
JavaScript
// 1. Queue Implementation
class Queue {
constructor() {
this.items = [];
}
// Enqueue operation (add an element to the queue)
enqueue(element) {
this.items.push(element);
console.log(`${element} has been added to the queue.`);
}
// Dequeue operation (remove an element from the queue)
dequeue() {
if (this.isEmpty()) {
console.log('Queue is empty. Cannot dequeue.');
return;
}
const dequeuedElement = this.items.shift();
console.log(`${dequeuedElement} has been removed from the queue.`);
}
// Peek operation (view the front element of the queue)
peek() {
if (this.isEmpty()) {
console.log('Queue is empty. Nothing to peek.');
return;
}
console.log(`Front element in the queue: ${this.items[0]}`);
}
// Check if the queue is empty
isEmpty() {
return this.items.length === 0;
}
// Get the size of the queue
size() {
return this.items.length;
}
// Print all the elements in the queue
print() {
if (this.isEmpty()) {
console.log('Queue is empty.');
return;
}
console.log('Queue contents:', this.items.join(' <- '));
}
// Clear the queue
clear() {
this.items = [];
console.log('Queue has been cleared.');
}
// Reverse the queue (for demonstration)
reverse() {
this.items.reverse();
console.log('Queue has been reversed.');
}
// Check if the queue contains a specific element
contains(element) {
return this.items.includes(element);
}
// Merge another queue into this one
merge(otherQueue) {
this.items = this.items.concat(otherQueue.items);
console.log('Queues have been merged.');
}
// Remove all occurrences of an element from the queue
removeElement(element) {
this.items = this.items.filter(item => item !== element);
console.log(`All occurrences of ${element} have been removed from the queue.`);
}
// Rotate the queue (move the front element to the back)
rotate() {
if (!this.isEmpty()) {
const frontElement = this.items.shift();
this.items.push(frontElement);
console.log('Queue has been rotated.');
} else {
console.log('Queue is empty. Cannot rotate.');
}
}
// Get the first element in the queue without removing it
first() {
if (this.isEmpty()) {
console.log('Queue is empty.');
return null;
}
return this.items[0];
}
// Get the last element in the queue without removing it
last() {
if (this.isEmpty()) {
console.log('Queue is empty.');
return null;
}
return this.items[this.items.length - 1];
}
}
// 2. Stack Implementation
class Stack {
constructor() {
this.items = [];
}
// Push operation (add an element to the stack)
push(element) {
this.items.push(element);
console.log(`${element} has been added to the stack.`);
}
// Pop operation (remove an element from the stack)
pop() {
if (this.isEmpty()) {
console.log('Stack is empty. Cannot pop.');
return;
}
const poppedElement = this.items.pop();
console.log(`${poppedElement} has been removed from the stack.`);
}
// Peek operation (view the top element of the stack)
peek() {
if (this.isEmpty()) {
console.log('Stack is empty. Nothing to peek.');
return;
}
console.log(`Top element in the stack: ${this.items[this.items.length - 1]}`);
}
// Check if the stack is empty
isEmpty() {
return this.items.length === 0;
}
// Get the size of the stack
size() {
return this.items.length;
}
// Print all the elements in the stack
print() {
if (this.isEmpty()) {
console.log('Stack is empty.');
return;
}
console.log('Stack contents:', this.items.join(' -> '));
}
// Clear the stack
clear() {
this.items = [];
console.log('Stack has been cleared.');
}
// Reverse the stack (for demonstration)
reverse() {
this.items.reverse();
console.log('Stack has been reversed.');
}
// Check if the stack contains a specific element
contains(element) {
return this.items.includes(element);
}
// Merge another stack into this one
merge(otherStack) {
this.items = this.items.concat(otherStack.items);
console.log('Stacks have been merged.');
}
// Remove all occurrences of an element from the stack
removeElement(element) {
this.items = this.items.filter(item => item !== element);
console.log(`All occurrences of ${element} have been removed from the stack.`);
}
// Rotate the stack (move the top element to the bottom)
rotate() {
if (!this.isEmpty()) {
const topElement = this.items.pop();
this.items.unshift(topElement);
console.log('Stack has been rotated.');
} else {
console.log('Stack is empty. Cannot rotate.');
}
}
// Get the top element in the stack without removing it
top() {
if (this.isEmpty()) {
console.log('Stack is empty.');
return null;
}
return this.items[this.items.length - 1];
}
// Get the bottom element in the stack without removing it
bottom() {
if (this.isEmpty()) {
console.log('Stack is empty.');
return null;
}
return this.items[0];
}
// Peek a specific element in the stack (given its index)
peekAt(index) {
if (index < 0 || index >= this.items.length) {
console.log('Invalid index.');
return null;
}
console.log(`Element at index ${index}: ${this.items[index]}`);
return this.items[index];
}
}
// Exporting Queue and Stack classes for external use
module.exports = {
Queue,
Stack
};