Skip to content

Instantly share code, notes, and snippets.

@Deshan555
Created August 10, 2023 18:06
Show Gist options
  • Select an option

  • Save Deshan555/9f8c9565c5f85a756979ab17e3e17c67 to your computer and use it in GitHub Desktop.

Select an option

Save Deshan555/9f8c9565c5f85a756979ab17e3e17c67 to your computer and use it in GitHub Desktop.
stack Implementation of Javascript
/*
Stacks Follow Princiles of : LIFO
Push: Add an element to the top of a stack
Pop: Remove an element from the top of a stack
IsEmpty: Check if the stack is empty
IsFull: Check if the stack is full
Peek: Get the value of the top element without removing it
*/
class Stacks{
constructor(){
this.stack = [];
this.size = 20;
}
push(data){
if(!this.stack.length <= 20){
this.stack.push(data);
return this.stack;
}
return 'stack full';
}
pop(){
this.stack.pop();
return this.stack;
}
get length_stack(){
return this.stack.length;
}
get peek(){
return this.stack[(this.stack.length)-1];
}
get print(){
return this.stack;
}
}
// stack operataions
const stack = new Stacks();
console.log(stack.print);
for (var i = 0; i < 10; i++) {
stack.push(i);
}
console.log(stack.print);
console.log(stack.pop());
console.log(stack.print);
console.log(stack.length_stack);
console.log(stack.peek);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment