xxxxxxxxxx
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]
}
xxxxxxxxxx
class Stack{
constructor(){
this.array = [];
this.count = 0;
}
isEmpty(){
return this.count == 0;
}
size(){
return this.count;
}
peekTop() {
if(this.count == 0){
return "Stack is Empty";
}
return this.array[this.count-1];
}
peek(pos){
var top =this.count-1;
if(top-pos+1<0)
return "invalid index";
else
return this.array[top-pos+1];
}
push(data){
this.count++;
this.array.push(data);
}
pop(){
if(this.array.length == 0)
return "Stack is Underflow";
this.count--;
return this.array.pop();
}
print(){
if(this.count == 0){
return "Unable to Print,Stack is Empty :-(";
}
var str = "";
for (var i = this.array.length-1; i >= 0; i--)
str += this.array[i] + " ";
return str;
}
}
const arrStack = new Stack();
arrStack.push("10");
arrStack.push("20");
arrStack.push("30");
arrStack.push("40");
console.log("Peek Top element is ",arrStack.peekTop());
arrStack.push("50");
console.log("Poped element in stack ",arrStack.pop());
console.log("Peeked element at position 2",arrStack.peek(2));
console.log("element in stack\n",arrStack.print());
console.log("is Stack empty? ",arrStack.isEmpty());
console.log("Size of stack is ",arrStack.size());
/**
* Time Complexity -> O(1) (all opeartion take constant time)
* Space Complexity -> O(1)
*/
xxxxxxxxxx
class Stack {
constructor(size) {
// make properties protected usually prefixed with an underscore _
this._size = size
this._stack = []
}
isFull() {
return (this._stack.length === this._size)
}
isEmpty() {
return (this._stack.length === 0)
}
push(item) {
if (!this.isFull()) {
this._stack.push(item)
return true
}
return false
}
pop() {
if (!this.isEmpty()) {
const index = this._stack.indexOf(this._stack[this._stack.length - 1])
if (index > -1) {
this._stack.splice(index, 1)
}
return true
}
return false
}
peek() {
if (!this.isEmpty()) {
return this._stack[this._stack.length - 1]
}
return 'empty'
}
getStack() {
return this._stack
}
}
let stack = new Stack(3)
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
console.log('stack: ', stack.getStack())
console.log('peek: ', stack.peek())
console.log('is full: ', stack.isFull())
console.log('is empty:', stack.isEmpty(), '\n')
stack.pop()
stack.pop()
stack.pop()
console.log('stack: ', stack.getStack())
console.log('peek: ', stack.peek())
console.log('is full: ', stack.isFull())
console.log('is empty:', stack.isEmpty())
// [Log]:
stack: [ 1, 2, 3 ]
peek: 3
is full: true
is empty: false
stack: []
peek: empty
is full: false
is empty: true
xxxxxxxxxx
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
xxxxxxxxxx
class Stack{
constructor()
{
this.items = [];
}
push(element)
{
// push element into the items
this.items.push(element);
}
pop()
{
if (this.items.length == 0)
return "Underflow";
return this.items.pop();
}
peek()
{
return this.items[this.items.length - 1];
}
printStack()
{
var str = "";
for (var i = 0; i < this.items.length; i++)
str += this.items[i] + " ";
return str;
}
}
xxxxxxxxxx
class Stack {
constructor() {
this.items = [];
}
push(item) {
this.items.unshift(item);
}
pop() {
return this.items.shift();
}
peek() {
return this.items[0];
}
isEmpty() {
return this.items.length === 0;
}
}
let s = new Stack();
s.push("one");
s.push("two");
s.push("three");
s.pop("two");
console.log(s);
xxxxxxxxxx
class Node{
constructor(value){
this.value = value;
this.next = null;
}
}
class Stack{
constructor(){
this.top = null;
this.bottom = null;
this.length = 0;
};
peek(){
return this.top;
};
push(value){
const newNode = new Node(value);
newNode.next = this.top
this.top = newNode;
if(this.length === 0) this.bottom = newNode;
this.length++;
}
pop(){
if(!this.top) return;
if(this.top === this.bottom){
this.top = null;
this.bottom = null;
this.length--;
return
}
const temp = this.top
this.top = temp.next
this.length--;
}
}
const a = new Stack();