Data Structures in Typescript – Stack

A stack uses LIFO (last-in-first-out) ordering, the most recent item added is the first item to be removed, just like a real stack.
Some uses of this data structure are expressions evaluations and conversion (prefix, postfix, and infix), backtracking, …


This content originally appeared on DEV Community and was authored by Ricardo Borges

A stack uses LIFO (last-in-first-out) ordering, the most recent item added is the first item to be removed, just like a real stack.
Some uses of this data structure are expressions evaluations and conversion (prefix, postfix, and infix), backtracking, and memory management.

stack

Representation

A stack can be implemented using an array or a linked list, can be either fixed or dynamic size.

Basic operations

  • Push - Add an item to the top of the stack
  • Pop - Remove the top item from the stack
  • Peek - Return the top of the stack, without removing it.
  • isEmpty - Return true if the stack is empty.
  • isFull - Return true if the stack is full, used when the stack is fixed size.

Here's an implementation of a stack using an array, in Typescript an array doesn't have a fixed length, so the operation isFull is not required, however you can implement a stack with a fixed length and use that operation.

class Stack<T> {
  private array: T[] = [];

  pop(): T | undefined {
    if (this.isEmpty()) throw new EmptyStackException();

    return this.array.pop();
  }

  push(data: T): void {
    this.array.push(data);
  }

  peek(): T {
    if (this.isEmpty()) throw new EmptyStackException();

    return this.array[this.array.length - 1];
  }

  isEmpty(): boolean {
    return this.array.length === 0;
  }
}


This content originally appeared on DEV Community and was authored by Ricardo Borges


Print Share Comment Cite Upload Translate Updates
APA

Ricardo Borges | Sciencx (2021-06-17T22:58:26+00:00) Data Structures in Typescript – Stack. Retrieved from https://www.scien.cx/2021/06/17/data-structures-in-typescript-stack/

MLA
" » Data Structures in Typescript – Stack." Ricardo Borges | Sciencx - Thursday June 17, 2021, https://www.scien.cx/2021/06/17/data-structures-in-typescript-stack/
HARVARD
Ricardo Borges | Sciencx Thursday June 17, 2021 » Data Structures in Typescript – Stack., viewed ,<https://www.scien.cx/2021/06/17/data-structures-in-typescript-stack/>
VANCOUVER
Ricardo Borges | Sciencx - » Data Structures in Typescript – Stack. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/17/data-structures-in-typescript-stack/
CHICAGO
" » Data Structures in Typescript – Stack." Ricardo Borges | Sciencx - Accessed . https://www.scien.cx/2021/06/17/data-structures-in-typescript-stack/
IEEE
" » Data Structures in Typescript – Stack." Ricardo Borges | Sciencx [Online]. Available: https://www.scien.cx/2021/06/17/data-structures-in-typescript-stack/. [Accessed: ]
rf:citation
» Data Structures in Typescript – Stack | Ricardo Borges | Sciencx | https://www.scien.cx/2021/06/17/data-structures-in-typescript-stack/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.