Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stack implementation with stack.py and readme.me #15

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Algorithms_and_Data_Structures/Stack/README.md
Original file line number Diff line number Diff line change
@@ -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.
47 changes: 47 additions & 0 deletions Algorithms_and_Data_Structures/Stack/stack.py
Original file line number Diff line number Diff line change
@@ -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