let stack = []
stack.push(2) // stack is now [ 2 ]
stack.push(5) // stack is now [ 2, 5 ]
let lastElement = stack[stack.length - 1] // the las element is 5
let i = stack.pop() // stack is now [ 2 ]
console.log(i) // displays 5
let queue = []
queue.push(2) // queue is now [ 2 ]
queue.push(5) // queue is now [ 2, 5 ]
let i = queue.shift() // queue is now [ 5 ]
console.log(i) // displays 2
/* Errors to be thrown when
trying to get elements from
an empty stack. */
class StackError extends Error {
constructor(message) {
super(message);
this.name = "Stack Error";
}
}
// Last In First Out stack data structure
class Stack {
constructor() {
this.items = []; // container holding elements
}
// Getter methods
get size() {
return this.items.length;
}
get isEmpty() {
return this.size === 0;
}
// push: push an element onto
// top of the stack
push(item) {
this.items.push(item);
}
// pop: pop top element from stack
pop() {
if (this.isEmpty) {
throw new StackError("Stack is empty!");
}
return this.items.pop();
}
// peek: return top element of stack
peek() {
if (this.isEmpty) {
throw new StackError("Stack is empty!");
}
return this.items[this.items.length - 1];
}
}
const stack = new Stack();
try {
stack.peek(); // Stack is empty => error
} catch (err) {
console.log(err.message); // Stack is empty!
}
// Add elements 1, 2, and 3 to stack
for (let idx = 1; idx <= 3; idx++) {
stack.push(idx);
}
console.log("Size:", stack.size); // Size: 3
const stackContent = [];
while (!stack.isEmpty) {
stackContent.push(stack.pop() + " ");
}
console.log("Stack:", stackContent.join("")); // Stack: 3 2 1
array = []
top = -1
function push(data){
array[++top] = data
}
function pop(){
if (top == -1) return "Empty Stack"
ans = array[top]
top--
return ans
}
function peek(){
if (top == -1) return "Empty Stack"
return array[top]
}