Skip to content

Updated index.md and added stacks.md under the issue #349 #693

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

Merged
merged 6 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions contrib/ds-algorithms/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
- [Greedy Algorithms](greedy-algorithms.md)
- [Dynamic Programming](dynamic-programming.md)
- [Linked list](linked-list.md)
- [Stacks in Python](stacks.md)
- [Sliding Window Technique](sliding-window.md)
- [Trie](trie.md)
116 changes: 116 additions & 0 deletions contrib/ds-algorithms/stacks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Stacks in Python

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.

## Points to be Remebered

- A stack is a collection of data items that can be accessed at only one end, called **TOP**.
- Items can be inserted and deleted in a stack only at the TOP.
- The last item inserted in a stack is the first one to be deleted.
- Therefore, a stack is called a **Last-In-First-Out (LIFO)** data structure.

## Real Life Examples of Stacks

- **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.

- **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.

- **BANGLES IN A HAND** - When a person wears bangles, the last bangle worn is the first one to be removed.

## Applications of Stacks

Stacks are widely used in Computer Science:

- Function call management
- Maintaining the UNDO list for the application
- Web browser *history management*
- Evaluating expressions
- Checking the nesting of parentheses in an expression
- Backtracking algorithms (Recursion)

Understanding these applications is essential for Software Development.

## Operations on a Stack

Key operations on a stack include:

- **PUSH** - It is the process of inserting a new element on the top of a stack.
- **OVERFLOW** - A situation when we are pushing an item in a stack that is full.
- **POP** - It is the process of deleting an element from the top of a stack.
- **UNDERFLOW** - A situation when we are popping item from an empty stack.
- **PEEK** - It is the process of getting the most recent value of stack *(i.e. the value at the top of the stack)*
- **isEMPTY** - It is the function which return true if stack is empty else false.
- **SHOW** -Displaying stack items.

## Implementing Stacks in Python

```python
def isEmpty(S):
if len(S) == 0:
return True
else:
return False

def Push(S, item):
S.append(item)

def Pop(S):
if isEmpty(S):
return "Underflow"
else:
val = S.pop()
return val

def Peek(S):
if isEmpty(S):
return "Underflow"
else:
top = len(S) - 1
return S[top]

def Show(S):
if isEmpty(S):
print("Sorry, No items in Stack")
else:
print("(Top)", end=' ')
t = len(S) - 1
while t >= 0:
print(S[t], "<", end=' ')
t -= 1
print()

stack = [] # initially stack is empty

Push(stack, 5)
Push(stack, 10)
Push(stack, 15)

print("Stack after Push operations:")
Show(stack)
print("Peek operation:", Peek(stack))
print("Pop operation:", Pop(stack))
print("Stack after Pop operation:")
Show(stack)
```

## Output

```markdown
Stack after Push operations:

(Top) 15 < 10 < 5 <

Peek operation: 15

Pop operation: 15

Stack after Pop operation:

(Top) 10 < 5 <
```

## Complexity Analysis

- **Worst case**: `O(n)` This occurs when the stack is full, it is dominated by the usage of Show operation.
- **Best case**: `O(1)` When the operations like isEmpty, Push, Pop and Peek are used, they have a constant time complexity of O(1).
- **Average case**: `O(n)` The average complexity is likely to be lower than O(n), as the stack is not always full.