mirror of
https://git.adityakumar.xyz/data-structures.git
synced 2024-11-09 13:39:43 +00:00
add stack.py
This commit is contained in:
commit
c050979213
1 changed files with 28 additions and 0 deletions
28
stack/stack.py
Normal file
28
stack/stack.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
# Create a stack
|
||||
def create_stack():
|
||||
stack = []
|
||||
return stack
|
||||
|
||||
# Create an empty stack
|
||||
def check_empty(stack):
|
||||
return len(stack) == 0
|
||||
|
||||
# Add items to stack
|
||||
def push(stack, item):
|
||||
stack.append(item)
|
||||
print(f'Pushed {item}')
|
||||
|
||||
# Remove an element from stack
|
||||
def pop(stack):
|
||||
if (check_empty(stack)):
|
||||
return "stack empty"
|
||||
return stack.pop()
|
||||
|
||||
# Driver code
|
||||
stack = create_stack()
|
||||
push(stack, str(1))
|
||||
push(stack, str(2))
|
||||
push(stack, str(3))
|
||||
push(stack, str(4))
|
||||
print(f'Popped: {pop(stack)}')
|
||||
print(f'Stack: {str(stack)}')
|
Loading…
Reference in a new issue