code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

02 Array Stack

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

class Stack {
  constructor() {
    this.data = new Array(10)
    this.size = 0
  }

  isEmpty() {
    return this.size === 0
  }

  length() {
    return this.size
  }

  pop() {
    if (!this.isEmpty()) {
      let result = this.data[this.size - 1]
      this.data[this.size - 1] = null
      this.size -= 1
      return result
    }
  }

  push(value) {
    this.data[this.size] = value
    this.size += 1
  }

  peek() {
    return this.data[this.size - 1]
  }
}

const log = console.log
let stack = new Stack()
stack.push(2)
stack.push(10)
log(stack.pop())
log(stack.peek())

// test('linked List Stack', () => {
//   expect(linkedListStack('Ashish')).toEqual('Ashish')
// });

Created 2019-11-30T02:13:21+05:30, updated 2019-11-30T02:28:17+05:30 · History · Edit