Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

stack implementation in javascript using array

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
",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)
 */
Comment

stack in javascript

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; 
    } 

}
Comment

Stack Example In JavaScript

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);
Comment

How to create a stack in JavaScript?

/* 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
Comment

Stack Implementation in Javascript

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]
}
Comment

PREVIOUS NEXT
Code Example
::  
::  
::  
::  
Javascript ::  
::  
::  
::  
::  
::  
::  
::  
::  
::  
::  
::  
::  
::  
Javascript ::  
::  
::  
::  
Javascript ::  
::  
::  
::  
::  
::  
::  
::  
ADD CONTENT
Topic
Content
Source link
Name
2+4 =