|
| 1 | +--- |
| 2 | +title: Stack |
| 3 | +description: 'Introduction to Data Structures and Algorithms' |
| 4 | +--- |
| 5 | + |
| 6 | +A stack is a linear data structure that operates on a Last In, First Out (LIFO) basis, where the last element added is the first one to be removed. It's similar to a stack of plates—only the top plate can be removed at a time. The stack supports two main operations: |
| 7 | + |
| 8 | +1. **Push**: Adds an element to the top of the stack. |
| 9 | +2. **Pop**: Removes the element from the top of the stack. |
| 10 | + |
| 11 | +### Key Properties of Stack |
| 12 | + |
| 13 | +* **LIFO (Last In, First Out)**: The last element added is the first one to be removed. |
| 14 | +* **Top**: A pointer or reference to the element at the top of the stack. |
| 15 | +* **Size**: The number of elements in the stack. |
| 16 | + |
| 17 | +### Basic Operations |
| 18 | + |
| 19 | +* **Push(x)**: Insert element at the top of the stack. |
| 20 | +* **Pop()**: Remove and return the element from the top of the stack. |
| 21 | +* **Peek()** or **Top()**: Return the element at the top without removing it. |
| 22 | +* **isEmpty()**: Check if the stack has any elements. |
| 23 | +* **isFull()**: Check if the stack has reached its maximum capacity (for fixed-size stacks). |
| 24 | + |
| 25 | +### Implementation |
| 26 | + |
| 27 | +Stacks can be implemented using arrays or linked lists. |
| 28 | + |
| 29 | +::code-group |
| 30 | + |
| 31 | +```python [Array] |
| 32 | +# In an array-based stack, elements are added to the endof an array. |
| 33 | +# Here's a simple code example in Python: |
| 34 | + |
| 35 | +class Stack: |
| 36 | + def __init__(self, capacity): |
| 37 | + self.stack = [] |
| 38 | + self.capacity = capacity |
| 39 | + |
| 40 | + def push(self, item): |
| 41 | + if len(self.stack) < self.capacity: |
| 42 | + self.stack.append(item) |
| 43 | + else: |
| 44 | + print("Stack Overflow") |
| 45 | + |
| 46 | + def pop(self): |
| 47 | + if not self.is_empty(): |
| 48 | + return self.stack.pop() |
| 49 | + else: |
| 50 | + print("Stack Underflow") |
| 51 | + |
| 52 | + def peek(self): |
| 53 | + if not self.is_empty(): |
| 54 | + return self.stack[-1] |
| 55 | + return None |
| 56 | + |
| 57 | + def is_empty(self): |
| 58 | + return len(self.stack) == 0 |
| 59 | + |
| 60 | +# Usage example |
| 61 | +s = Stack(5) |
| 62 | +s.push(10) |
| 63 | +s.push(20) |
| 64 | +print(s.pop()) # Output: 20 |
| 65 | +``` |
| 66 | + |
| 67 | +```python [Linked List] |
| 68 | +# In a linked list-based stack, each element points to the next one, |
| 69 | +# allowing flexible resizing. |
| 70 | + |
| 71 | +class Node: |
| 72 | + def __init__(self, data): |
| 73 | + self.data = data |
| 74 | + self.next = None |
| 75 | + |
| 76 | +class Stack: |
| 77 | + def __init__(self): |
| 78 | + self.top = None |
| 79 | + |
| 80 | + def push(self, item): |
| 81 | + new_node = Node(item) |
| 82 | + new_node.next = self.top |
| 83 | + self.top = new_node |
| 84 | + |
| 85 | + def pop(self): |
| 86 | + if self.is_empty(): |
| 87 | + print("Stack Underflow") |
| 88 | + return None |
| 89 | + popped_data = self.top.data |
| 90 | + self.top = self.top.next |
| 91 | + return popped_data |
| 92 | + |
| 93 | + def peek(self): |
| 94 | + return self.top.data if self.top else None |
| 95 | + |
| 96 | + def is_empty(self): |
| 97 | + return self.top is None |
| 98 | + |
| 99 | +# Usage example |
| 100 | +s = Stack() |
| 101 | +s.push(10) |
| 102 | +s.push(20) |
| 103 | +print(s.pop()) # Output: 20 |
| 104 | +``` |
| 105 | + |
| 106 | +:: |
| 107 | + |
| 108 | +### Applications of Stack |
| 109 | + |
| 110 | +* **Expression Evaluation**: Evaluating postfix or prefix expressions. |
| 111 | +* **Syntax Parsing**: Parsing expressions in compilers and interpreters. |
| 112 | +* **Backtracking**: Like undo operations in text editors. |
| 113 | +* **Function Calls**: Keeping track of function calls in recursion. |
| 114 | + |
| 115 | +Stacks are simple but powerful, forming the backbone of many computer science algorithms and operations. |
| 116 | + |
| 117 | +### Resources |
| 118 | + |
| 119 | +* [AlgoMonster](https://algo.monster/problems/stack_intro) |
| 120 | +* [Programiz](https://www.programiz.com/dsa/stack) |
0 commit comments