From 01fd3ad52b1e9bb32b1978d42fb4f94d07f6b495 Mon Sep 17 00:00:00 2001 From: Charul00 Date: Wed, 2 Oct 2024 12:12:45 +0530 Subject: [PATCH 1/2] Add stack implementation with stack.py and readme.me --- .../Stack/README.md | 27 +++++++++++ Algorithms_and_Data_Structures/Stack/stack.py | 47 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 Algorithms_and_Data_Structures/Stack/README.md create mode 100644 Algorithms_and_Data_Structures/Stack/stack.py diff --git a/Algorithms_and_Data_Structures/Stack/README.md b/Algorithms_and_Data_Structures/Stack/README.md new file mode 100644 index 00000000..0d4f63ef --- /dev/null +++ b/Algorithms_and_Data_Structures/Stack/README.md @@ -0,0 +1,27 @@ +# Stacks + +## What is a Stack? + +A **Stack** is a linear data structure that follows a specific order for operations. It can be understood as a collection of elements that allows for adding and removing elements in a specific manner. The two primary order types are: + +- **LIFO (Last In First Out)**: The most recently added element is the first to be removed. +- **FILO (First In Last Out)**: The first element added is the last one to be removed. + +### Key Characteristics + +- **Top**: The most recently added element that can be removed next. +- **Push**: The operation of adding an element to the stack. +- **Pop**: The operation of removing the top element from the stack. +- **Peek/Top**: The operation to view the top element without removing it. +- **IsEmpty**: A check to see if the stack has no elements. + +## Operations + +1. **Push**: Add an item to the top of the stack. +2. **Pop**: Remove the item from the top of the stack. +3. **Peek**: Retrieve the top item without removing it. +4. **IsEmpty**: Check if the stack is empty. + +## Conclusion + +Stacks are fundamental data structures that are widely used in various applications, such as expression parsing, backtracking algorithms, and memory management in programming languages. Understanding how to implement and manipulate stacks is essential for many computer science concepts. diff --git a/Algorithms_and_Data_Structures/Stack/stack.py b/Algorithms_and_Data_Structures/Stack/stack.py new file mode 100644 index 00000000..e48d7e37 --- /dev/null +++ b/Algorithms_and_Data_Structures/Stack/stack.py @@ -0,0 +1,47 @@ +class Stack: + def __init__(self): + self.items = [] + + def is_empty(self): + """Check if the stack is empty.""" + return len(self.items) == 0 + + def push(self, item): + """Add an item to the top of the stack.""" + self.items.append(item) + + def pop(self): + """Remove and return the top item from the stack.""" + if not self.is_empty(): + return self.items.pop() + raise IndexError("Pop from an empty stack") + + def peek(self): + """Return the top item from the stack without removing it.""" + if not self.is_empty(): + return self.items[-1] + raise IndexError("Peek from an empty stack") + + def size(self): + """Return the number of items in the stack.""" + return len(self.items) + +# Example usage +if __name__ == "__main__": + stack = Stack() + stack.push(10) + stack.push(20) + stack.push(30) + + print("Top element is:", stack.peek()) # Output: Top element is: 30 + print("Stack size is:", stack.size()) # Output: Stack size is: 3 + + print("Popped element:", stack.pop()) # Output: Popped element: 30 + print("Stack size after pop:", stack.size()) # Output: Stack size after pop: 2 + + print("Is stack empty?", stack.is_empty()) # Output: Is stack empty? False + + stack.pop() # Pops 20 + stack.pop() # Pops 10 + + print("Is stack empty after popping all elements?", stack.is_empty()) # Output: Is stack empty after popping all elements? True From 0d07b8e1f0fff48997ba227ddc375baaaa592748 Mon Sep 17 00:00:00 2001 From: Charul00 Date: Wed, 2 Oct 2024 13:29:08 +0530 Subject: [PATCH 2/2] Updated stack.py with examples and comments --- Algorithms_and_Data_Structures/Stack/stack.py | 48 ++++++++++++++----- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/Algorithms_and_Data_Structures/Stack/stack.py b/Algorithms_and_Data_Structures/Stack/stack.py index e48d7e37..bab89472 100644 --- a/Algorithms_and_Data_Structures/Stack/stack.py +++ b/Algorithms_and_Data_Structures/Stack/stack.py @@ -28,20 +28,42 @@ def size(self): # Example usage if __name__ == "__main__": - stack = Stack() - stack.push(10) - stack.push(20) - stack.push(30) - - print("Top element is:", stack.peek()) # Output: Top element is: 30 - print("Stack size is:", stack.size()) # Output: Stack size is: 3 + # Example with integers + int_stack = Stack() + int_stack.push(10) + int_stack.push(20) + int_stack.push(30) + print("Top element is:", int_stack.peek()) # Output: Top element is: 30 + print("Stack size is:", int_stack.size()) # Output: Stack size is: 3 + print("Popped element:", int_stack.pop()) # Output: Popped element: 30 + print("Stack size after pop:", int_stack.size()) # Output: Stack size after pop: 2 + print("Is stack empty?", int_stack.is_empty()) # Output: Is stack empty? False - print("Popped element:", stack.pop()) # Output: Popped element: 30 - print("Stack size after pop:", stack.size()) # Output: Stack size after pop: 2 + # Pop remaining elements + int_stack.pop() # Pops 20 + int_stack.pop() # Pops 10 + print("Is stack empty after popping all elements?", int_stack.is_empty()) # Output: Is stack empty after popping all elements? True - print("Is stack empty?", stack.is_empty()) # Output: Is stack empty? False + # Example with strings + string_stack = Stack() + string_stack.push("Hello") + string_stack.push("World") + print("Top element is:", string_stack.peek()) # Output: Top element is: World + print("Stack size is:", string_stack.size()) # Output: Stack size is: 2 + print("Popped element:", string_stack.pop()) # Output: Popped element: World + print("Stack size after pop:", string_stack.size()) # Output: Stack size after pop: 1 + print("Is stack empty?", string_stack.is_empty()) # Output: Is stack empty? False - stack.pop() # Pops 20 - stack.pop() # Pops 10 - print("Is stack empty after popping all elements?", stack.is_empty()) # Output: Is stack empty after popping all elements? True + +# Top element is: 30 +# Stack size is: 3 +# Popped element: 30 +# Stack size after pop: 2 +# Is stack empty? False +# Is stack empty after popping all elements? True +# Top element is: World +# Stack size is: 2 +# Popped element: World +# Stack size after pop: 1 +# Is stack empty? False \ No newline at end of file