LIFO : Last In, First Out
class Stack {
constructor(){
this.top = -1;
this.storage = [];
}
push (element) {
this.top ++;
this.storage[this.top] = element;
}
pop () {
if(top < 0){
return;
}else{
let popped = this.storage[this.top]
delete this.storage[this.top]
this.top --;
return popped;
}
}
peek () {
return this.storage[this.top]
}
}
FIFO : First In, First Out
class Queue {
constructor () {
this.storage = [];
this.front = 0;
this.rear = -1;
}
enqueue (element) {
this.rear ++;
this.storage[this.rear] = element;
}
dequeue () {
if(this.front - this.rear > 0) {
return;
}else{
let dequeued = this.storage[this.front];
delete this.storage[this.front];
this.front ++;
return dequeued;
}
}
peek () {
return this.storage[this.front];
}
}