Stacks

Tarun Luthra

Agenda

What you will learn?

  • Basics of Stacks

  • Implementation

  • Examples

Stacks

A stackΒ is a linear data structure that follows the principle of Last In First Out (LIFO). This means the last element inserted inside the stack is removed first.

Operations on Stacks

  1. push(x)

Operations on Stacks

Operations on Stacks

  1. push(x)

: insert x at the top

Operations on Stacks

  1. push(x)
  2. pop()

: insert x at the top

Operations on Stacks

  1. push(x)
  2. pop()

: insert x at the top

: delete the top element

Operations on Stacks

  1. push(x)
  2. pop()
  3. top()

: insert x at the top

: delete the top element

Operations on Stacks

  1. push(x)
  2. pop()
  3. top()

: insert x at the top

: delete the top element

: return the top element

Operations on Stacks

  1. push(x)
  2. pop()
  3. top()
  4. size()

: insert x at the top

: delete the top element

: return the top element

: return no of elements

Can we iterate over a stack ? 🧐

Not without removing the data.

Can we iterate over a stack ? 🧐

Example πŸ‘¨β€πŸ’»Β 

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

8

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

8

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

8

9

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

8

9

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

8

9

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

8

9

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

8

9

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

8

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

8

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

8

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

8

Peek at the top

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

8

Peek at the top

Read 8

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

8

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

8

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

8

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

20

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

Peek at the top

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

Peek at the top

Read 40

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

25

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

25

  1. push(40)
  2. push(20)
  3. push(8)
  4. push(9)
  5. pop()
  6. top()
  7. pop()
  8. pop()
  9. top()
  10. push(25)

Perform these operations

40

25

What would happen if you use pop() or top() on an empty stack ? 🧐

What would happen if you use pop() or top() on an empty stack ? 🧐

What would happen if you use pop() or top() on an empty stack ? 🧐

Accessing pop() or top() method on an empty stack will lead to an

Error

What would happen if you use pop() or top() on an empty stack ? 🧐

Accessing pop() or top() method on an empty stack will lead to an

Stack Underflow Error

Implementation πŸ‘¨β€πŸ’»Β 

Stack Implementation using Dynamic Arrays πŸ‘¨β€πŸ’»Β 

Stack Implementation using Dynamic Arrays πŸ‘¨β€πŸ’»Β 

class Stack {
    ArrayList<Integer> l = new ArrayList<>();

    void push(int x) {
        l.add(x);
    }

    void pop() {
        l.remove(l.size() - 1);
    }

    int top() {
        return l.get(l.size() - 1);
    }

    int size() {
        return l.size();
    }
}
class Stack:
    l = []

    def push(self, x):
        self.l.append(x)

    def pop(self):
        self.l.pop()

    def top(self):
        return self.l[-1]

    def size(self):
        return len(self.l)

Stack Implementation using Dynamic Arrays πŸ‘¨β€πŸ’»Β 

class Stack {
    ArrayList<Integer> l = new ArrayList<>();

    void push(int x) {
        l.add(x);
    }

    void pop() {
        if (l.size() == 0) {
            System.out.println("Stack is empty");
        }
        l.remove(l.size() - 1);
    }

    int top() {
        if (l.size() == 0) {
            System.out.println("Stack is empty");
            return -1;
        }
        return l.get(l.size() - 1);
    }

    int size() {
        return l.size();
    }
}
class Stack:
    l = []

    def push(self, x):
        self.l.append(x)

    def pop(self):
        if len(self.l) == 0:
            print("Stack is empty")
        self.l.pop()

    def top(self):
        if len(self.l) == 0:
            print("Stack is empty")
        return self.l[-1]

    def size(self):
        return len(self.l)

Stack Implementation using Dynamic Arrays πŸ‘¨β€πŸ’»Β 

class Stack {
    ArrayList<Integer> l = new ArrayList<>();

    void push(int x) {
        l.add(x);
    }

    void pop() throws Exception {
        if (l.size() == 0) {
            throw new Exception("Stack underflow");
        }
        l.remove(l.size() - 1);
    }

    int top() throws Exception {
        if (l.size() == 0) {
            throw new Exception("Stack underflow");
        }
        return l.get(l.size() - 1);
    }

    int size() {
        return l.size();
    }
}
class Stack:
    l = []

    def push(self, x):
        self.l.append(x)

    def pop(self):
        if len(self.l) == 0:
            raise Exception("Stack underflow")
        self.l.pop()

    def top(self):
        if len(self.l) == 0:
            raise Exception("Stack underflow")
        return self.l[-1]

    def size(self):
        return len(self.l)

Stack Implementation using Linked Lists πŸ‘¨β€πŸ’»Β 

Stack Implementation using Linked Lists πŸ‘¨β€πŸ’»Β 

Stack Implementation using Linked Lists πŸ‘¨β€πŸ’»Β 

class Node {
    int data;
    Node next;

    Node(int x) {
        data = x;
        next = null;
    }
}
class Node:
    data = 0
    next = None

    def __init__(self, x):
        self.data = x
        self.next = None
class Stack {
    Node head = null;
    int s = 0;

    void push(int x) {
        Node n = new Node(x);
        n.next = head;
        head = n;
        s++;
    }

    void pop() throws Exception {
        if (head == null) {
            throw new Exception("Stack underflow error");
        }
        head = head.next;
        s--;
    }

    int top() throws Exception {
        if (head == null) {
            throw new Exception("Stack underflow error");
        }
        return head.data;
    }

    int size() {
        return s;
    }
}
class Stack:
    head = None
    s = 0

    def push(self, x):
        n = Node(x)
        n.next = self.head
        self.head = n
        self.s += 1

    def pop(self):
        if self.head is None:
            raise Exception("Stack underflow error")

        self.head = self.head.next
        self.s -= 1

    def top(self):
        if self.head is None:
            raise Exception("Stack underflow error")
        return self.head.data

    def size(self):
        return self.s

Stack Libraries

There are ready-made implementations of Stack in most language's libraries/modules. You can find them here :

  1. Stacks in Python - https://www.scaler.com/topics/stack-in-python/
  2. Stacks in Java - https://www.scaler.com/topics/stack-class-in-java/

  3. Stacks in C++ - https://www.scaler.com/topics/cpp/stack-in-cpp/

Visualisers

  1. Stack Visualiser - Array Implementation -https://www.cs.usfca.edu/~galles/visualization/StackArray.html
  2. Stack Visualiser - Linked List Implementation -
    https://visualgo.net/en/list?slide=3-5

Codes

  1. Java LL Implementation :
    https://ide.codingminutes.com/?id=ayg
  2. Python LL Implementation :
    https://ide.codingminutes.com/?id=ayu
  3. Java Stack ArrayList Implementation :
    https://ide.codingminutes.com/?id=ayk
  4. Python Stack List Implementation :
    https://ide.codingminutes.com/?id=ayf

Thank you! πŸ‘¨β€πŸ’»Β 

Made with Slides.com