Stack Using Typescript

04 Aug 2017

Stack is a last in first out (LIFO) data structure . The last item to be pushed into the stack will be the first item to be retrieved. It resembles a pile of books . The common operations in a stack are push and pop.


class Stack {

  data: any[];
  count: number = 0;
  size: number = 10;

  constructor(){
   this.data = new Array(this.size);
  }
  
  constructor(size: number) {
    this.size = size;
    this.data = new Array(size);
  }

  push(item: any) {
    if (this.count < this.size) {
      this.data[this.count] = item;
      this.count++;
    } else {
      console.log("The stack is full");
    }
  }

  pop() {
    if (!this.isEmpty()) {
      const index = this.count - 1;
      const value = this.data[index];
      this.count--;
      return value;
    }
  }

  sizeOfStack() {
    return this.count;
  }

  isEmpty() {
    return this.count == 0;
  }

  print() {
    this.data.map(item => {
      console.log(item);
    })
  }

}