Open In App

How to Create a Stack in TypeScript using an Array ?

Last Updated : 10 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Stack is a linear Data Structure that is based on the LIFO concept (last in first out).

There are 5 primary operations in the stack as follows:

  • push method(): This method adds element x to the stack.
  • pop() Method: This method removes the last element of the stack.
  • peek() Method: This method returns the last element of the stack
  • empty() Method: This method returns whether the stack is empty or not.
  • size() Method: This method returns the size of the stack

Time Complexity: It is of O(1) for all operations of the stack.

Program to create a stack in TypeScript using an array

To create a stack in TypeScript using an array we first define a generic class Stack<T> that implements a stack data structure using an array. The class includes methods to manipulate the stack, such as push to add elements, pop to remove and return the top element, peek to view the top element without removing it, isEmpty to check if the stack is empty, size to get the number of elements in the stack, clear to remove all elements, and print to display the stack contents. Each method operates on the private items array property, ensuring encapsulation.

Example: In this example we demonstrated how to create a stack, perform operations like pushing and popping elements, and query the stack’s size and emptiness using array

JavaScript
class Stack<T> {
    private items: T[];
    // Private array to store stack elements

    constructor() {
        this.items = [];
        // Initialize the array as empty 
        //when a new stack is created
    }

    // Method to push an 
    // element onto the stack
    push(element: T): void {
        this
            .items.push(element);
    }

    // Method to pop an 
    // element from the stack
    pop(): T | undefined {
        return this
            .items.pop();
    }

    // Method to peek the top element
    // of the stack without removing it
    peek(): T | undefined {
        return this
            .items[this.items.length - 1];
    }

    // Method to check
    // if the stack is empty
    isEmpty(): boolean {
        return this
            .items.length === 0;
    }

    // Method to get 
    // the size of the stack
    size(): number {
        return this
            .items.length;
    }

    // Method to
    // clear the stack
    clear(): void {
        this.items = [];
    }

    // Method to print 
    // the elements of the stack
    print(): void {
        console.log(this.items);
    }
}

// Example usage
const stack = new Stack < number > ();
stack.push(1);
stack.push(2);
stack.push(3);
stack.print();

// Output: [1, 2, 3]
console.log("Top element:", stack.peek());

// Output: 3
console.log("Stack size:", stack.size());

// Output: 3
console.log("Is stack empty?", stack.isEmpty());

// Output: false
console.log("Popped element:", stack.pop());

// Output: 3
stack.print();

// Output: [1, 2]
console.log("Is stack empty?", stack.isEmpty());

// Output: False
// Clearing the stack 
// using stack.clear
stack.clear();
console.log("Is stack empty?", stack.isEmpty());
// Output: true

Output:

[ 1, 2, 3 ]
Top element: 3
Stack size: 3
Is stack empty? false
Popped element: 3
[ 1, 2 ]
Is stack empty? false
Is stack empty? true


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads