Skip to content

Latest commit

 

History

History
39 lines (29 loc) · 551 Bytes

Data-Structures.md

File metadata and controls

39 lines (29 loc) · 551 Bytes

Data Structures

Stack

class Stack {
  constructor() {
    this.elements = []; 
  }

  peek() {
    return this.elements[this.elements.length - 1]; 
  }

  isEmpty() {
    return this.elements.length === 0; 
  }

  push(element) {
    this.elements.push(element); 
  }

  pop() {
    if (this.elements.length === 0) {
      return 'empty'
    }
    return this.elements.pop(); 
  }
}

let stack = new Stack(); 

console.log(stack.isEmpty()); // true 

stack.push('Alex');
stack.push(6.3);

console.log(stack.peek()); // 6.3