|
| 1 | +# Stacks in Python |
| 2 | + |
| 3 | +In Data Structures and Algorithms, a stack is a linear data structure that complies with the Last In, First Out (LIFO) rule. It works by use of two fundamental techniques: **PUSH** which inserts an element on top of the stack and **POP** which takes out the topmost element.This concept is similar to a stack of plates in a cafeteria. Stacks are usually used for handling function calls, expression evaluation, and parsing in programming. Indeed, they are efficient in managing memory as well as tracking program state. |
| 4 | + |
| 5 | +## Points to be Remebered |
| 6 | + |
| 7 | +- A stack is a collection of data items that can be accessed at only one end, called **TOP**. |
| 8 | +- Items can be inserted and deleted in a stack only at the TOP. |
| 9 | +- The last item inserted in a stack is the first one to be deleted. |
| 10 | +- Therefore, a stack is called a **Last-In-First-Out (LIFO)** data structure. |
| 11 | + |
| 12 | +## Real Life Examples of Stacks |
| 13 | + |
| 14 | +- **PILE OF BOOKS** - Suppose a set of books are placed one over the other in a pile. When you remove books from the pile, the topmost book will be removed first. Similarly, when you have to add a book to the pile, the book will be placed at the top of the file. |
| 15 | + |
| 16 | +- **PILE OF PLATES** - The first plate begins the pile. The second plate is placed on the top of the first plate and the third plate is placed on the top of the second plate, and so on. In general, if you want to add a plate to the pile, you can keep it on the top of the pile. Similarly, if you want to remove a plate, you can remove the plate from the top of the pile. |
| 17 | + |
| 18 | +- **BANGLES IN A HAND** - When a person wears bangles, the last bangle worn is the first one to be removed. |
| 19 | + |
| 20 | +## Applications of Stacks |
| 21 | + |
| 22 | +Stacks are widely used in Computer Science: |
| 23 | + |
| 24 | +- Function call management |
| 25 | +- Maintaining the UNDO list for the application |
| 26 | +- Web browser *history management* |
| 27 | +- Evaluating expressions |
| 28 | +- Checking the nesting of parentheses in an expression |
| 29 | +- Backtracking algorithms (Recursion) |
| 30 | + |
| 31 | +Understanding these applications is essential for Software Development. |
| 32 | + |
| 33 | +## Operations on a Stack |
| 34 | + |
| 35 | +Key operations on a stack include: |
| 36 | + |
| 37 | +- **PUSH** - It is the process of inserting a new element on the top of a stack. |
| 38 | +- **OVERFLOW** - A situation when we are pushing an item in a stack that is full. |
| 39 | +- **POP** - It is the process of deleting an element from the top of a stack. |
| 40 | +- **UNDERFLOW** - A situation when we are popping item from an empty stack. |
| 41 | +- **PEEK** - It is the process of getting the most recent value of stack *(i.e. the value at the top of the stack)* |
| 42 | +- **isEMPTY** - It is the function which return true if stack is empty else false. |
| 43 | +- **SHOW** -Displaying stack items. |
| 44 | + |
| 45 | +## Implementing Stacks in Python |
| 46 | + |
| 47 | +```python |
| 48 | +def isEmpty(S): |
| 49 | + if len(S) == 0: |
| 50 | + return True |
| 51 | + else: |
| 52 | + return False |
| 53 | + |
| 54 | +def Push(S, item): |
| 55 | + S.append(item) |
| 56 | + |
| 57 | +def Pop(S): |
| 58 | + if isEmpty(S): |
| 59 | + return "Underflow" |
| 60 | + else: |
| 61 | + val = S.pop() |
| 62 | + return val |
| 63 | + |
| 64 | +def Peek(S): |
| 65 | + if isEmpty(S): |
| 66 | + return "Underflow" |
| 67 | + else: |
| 68 | + top = len(S) - 1 |
| 69 | + return S[top] |
| 70 | + |
| 71 | +def Show(S): |
| 72 | + if isEmpty(S): |
| 73 | + print("Sorry, No items in Stack") |
| 74 | + else: |
| 75 | + print("(Top)", end=' ') |
| 76 | + t = len(S) - 1 |
| 77 | + while t >= 0: |
| 78 | + print(S[t], "<", end=' ') |
| 79 | + t -= 1 |
| 80 | + print() |
| 81 | + |
| 82 | +stack = [] # initially stack is empty |
| 83 | + |
| 84 | +Push(stack, 5) |
| 85 | +Push(stack, 10) |
| 86 | +Push(stack, 15) |
| 87 | + |
| 88 | +print("Stack after Push operations:") |
| 89 | +Show(stack) |
| 90 | +print("Peek operation:", Peek(stack)) |
| 91 | +print("Pop operation:", Pop(stack)) |
| 92 | +print("Stack after Pop operation:") |
| 93 | +Show(stack) |
| 94 | +``` |
| 95 | + |
| 96 | +## Output |
| 97 | + |
| 98 | +```markdown |
| 99 | +Stack after Push operations: |
| 100 | + |
| 101 | +(Top) 15 < 10 < 5 < |
| 102 | + |
| 103 | +Peek operation: 15 |
| 104 | + |
| 105 | +Pop operation: 15 |
| 106 | + |
| 107 | +Stack after Pop operation: |
| 108 | + |
| 109 | +(Top) 10 < 5 < |
| 110 | +``` |
| 111 | + |
| 112 | +## Complexity Analysis |
| 113 | + |
| 114 | +- **Worst case**: `O(n)` This occurs when the stack is full, it is dominated by the usage of Show operation. |
| 115 | +- **Best case**: `O(1)` When the operations like isEmpty, Push, Pop and Peek are used, they have a constant time complexity of O(1). |
| 116 | +- **Average case**: `O(n)` The average complexity is likely to be lower than O(n), as the stack is not always full. |
0 commit comments