code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

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:01:21+05:18, updated 2019-11-30T02:16:17+05:18 · History · Edit