DSA: Stacks

Stacks

Stacks are linear data structures like array. They support:

  1. Last In First Out (LIFO) or First In Last Out (FILO).
  2. Fixed sizing and dynamic sizing

Push: This is how we add an element to the stack. The element is always added to the top.

Pop: This is the removal of an element from the stack. The element removed is always from the top.

Peek or Top: This operation allows us to see the element on the top of the stack without removing it.

IsEmpty: This operation checks if the stack is empty.

Stacks

Real world usage:
Web Browser History: Every time you visit a new webpage, it's added to the top of your history stack. When you hit the back button, you're "popping" pages off the top of the stack.

Undo Function in Software Applications: The undo function in software applications uses a stack to remember actions.

Stack Overflow

Stack Underflow

for a fixed sized array, when you try to add elements more than size of stack

when you try to remove elements from an empty stack

Implementing Stacks Using Array

class Stack {
    // Constructor to initialize the stack with a specified size.
    constructor(size) {
        // Create a new array to represent the stack with the given size.
        this.stack = new Array(size);
        // Initialize the top pointer to -1, indicating an empty stack.
        this.top = -1;
    }
}

const myStack = new Stack(10)

Implementing Stacks Using Array

class Stack {
    // Constructor to initialize the stack with a specified size.
    constructor(size) {
        // Create a new array to represent the stack with the given size.
        this.stack = new Array(size);
        // Initialize the top pointer to -1, indicating an empty stack.
        this.top = -1;
    }
    
    
    // Method to add an element to the top of the stack.
    push(data) {
        // Check if the stack is already full.
        if (this.top === this.stack.length - 1) {
            throw new Error("Stack is full");
        }
        // Increment the top pointer and add the data to the stack.
        this.stack[++this.top] = data;
    }
}

const myStack = new Stack(10)
myStack.push("Yash")
myStack

Implementing Stacks Using Array

class Stack {
    // Constructor to initialize the stack with a specified size.
    constructor(size) {
        // Create a new array to represent the stack with the given size.
        this.stack = new Array(size);
        // Initialize the top pointer to -1, indicating an empty stack.
        this.top = -1;
    }
    // Method to add an element to the top of the stack.
    push(data) {
        // Check if the stack is already full.
        if (this.top === this.stack.length - 1) {
            throw new Error("Stack is full");
        }
        // Increment the top pointer and add the data to the stack.
        this.stack[++this.top] = data;
    }    
    // Method to remove and return the element from the top of the stack.
    pop() {
        // Check if the stack is empty.
        if (this.isEmpty()) {
            throw new Error("Stack is empty");
        }
        // Retrieve the data from the top of the stack.
        let data = this.stack[this.top];
        // Clear the top element by setting it to null, and decrement the top pointer.
        this.stack[this.top--] = null;
        // Return the removed data.
        return data;
    }
}
const myStack = new Stack(10)
myStack.push("Yash")
myStack

Implementing Stacks Using Array

class Stack {
    // Constructor to initialize the stack with a specified size.
    constructor(size) {
        // Create a new array to represent the stack with the given size.
        this.stack = new Array(size);
        // Initialize the top pointer to -1, indicating an empty stack.
        this.top = -1;
    }
    .
    .
    .
    // Method to peek at the element on the top of the stack without removing it.
    peek() {
        // Check if the stack is empty.
        if (this.isEmpty()) {
            throw new Error("Stack is empty");
        }
        // Return the element at the top of the stack.
        return this.stack[this.top];
    }

    // Method to check if the stack is empty.
    isEmpty() {
        // The stack is empty if the top pointer is -1.
        return this.top === -1;
    }

}

const myStack = new Stack(10)
const stackTopItem = myStack.peek()
const isStackEmpty = myStack.isEmpty()

DSA: Stacks

By Yash Priyam

DSA: Stacks

  • 131