1. Whta is Stack
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.
2. LIFO Principle of Stack
In programming terms, putting an item on top of the stack is called push and removing an item is called pop.

3. Basic Operations of Stack
- Push : Add an element to the top of a stack
- Pop : Remoe an element from the top of a stack
- IsEmpty : Check if the stack is empty
- IsFull : Check if the stack is full
- Peek : Get the value of the top element without removing it
4. Working of Stack Data Structure
- A pointer called TOP is used to keep track of the top element in the stack.
- When initializing the stack, we set its value to -1 so that we can check if the stack is empty by comparing TOP == -1.
- On pushing an element, we increase the value of TOP and place the new element in the position pointed to by TOP.
- On popping an element, we return the element pointed to by TOP and reduce its value
- Before pushing, we check if the stack is already full.
- Before popping, we check if the stack is already empty.
5. Stack Implementation
class Stack:
def __init__(self):
self.stack = []
def is_empty(self, stack):
return len(self.stack) == 0
def push(self, item):
self.stack.append(item)
print(f"Pushed item: {item}")
def pop(self):
if self.is_empty(self.stack):
print(f"Stack is empty")
return self.stack.pop()
6. Source
https://www.programiz.com/dsa/stack
'Course > [Progrmiz] Data Structure Algorithm' 카테고리의 다른 글
| [DS1] Types of Queue (0) | 2022.08.16 |
|---|---|
| [DS1] Queue (0) | 2022.08.10 |
| [Introduction] Divide and Conquer Algorithm (0) | 2022.08.09 |
| [Introduction] Asymptotic Notations (0) | 2022.08.08 |
| [Introduction] Why Learn DSA? (0) | 2022.08.08 |