Skip to content

Commit

Permalink
Merge pull request #15 from Charul00/feature/stack-implementation
Browse files Browse the repository at this point in the history
Add stack implementation with stack.py and readme.me
  • Loading branch information
TheChaoticor authored Oct 2, 2024
2 parents b9b3ce5 + 0d07b8e commit c5be6d4
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
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.
69 changes: 69 additions & 0 deletions Algorithms_and_Data_Structures/Stack/stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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__":
# 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

# 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

# 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



# 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

0 comments on commit c5be6d4

Please sign in to comment.