Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript stack

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

javascript stack

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();
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 :: react-drag-drop-files open twice 
Javascript :: javascript json 
Javascript :: how to use ejs with client side ejs 
Javascript :: knex insert multiple rows 
Javascript :: js test library 
Javascript :: comming soon page in react 
Javascript :: js opposite of includes 
Javascript :: how to make a syntax highlighter in javascript 
Javascript :: ant design form validation in the modal 
Javascript :: Changes not staged for commit: modified: ../package.json 
Javascript :: quote 
Javascript :: how to set option value in fstdropdown using ajax 
Javascript :: how to create a variable in javascript 
Javascript :: instantiate js 
Javascript :: what is process.env.NODE_ENV 
Javascript :: how use multi things in in switch case in js 
Javascript :: access css and js files inside resources folder in laravel 
Javascript :: else if in javascript 
Javascript :: instanceof 
Javascript :: canvas js in react 
Javascript :: add new element by index js 
Javascript :: Array of indexOf 
Javascript :: map duplicate keys JS 
Javascript :: react autocomplete 
Javascript :: search in javascript 
Javascript :: async vs await javascript 
Javascript :: jq cheat sheet 
Javascript :: update column with find sequelize 
Javascript :: decode jwt token 
Javascript :: express middleware status code always 200 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =