Stack
A stack is a fundamental data structure that follows the Last In, First Out (LIFO) principle, meaning the last item added
is the first one to be removed. Think of it like a stack of plates - when you add a new plate, it goes on top, and when
you take one away, you remove the topmost one first.
Key Operations of a Stack:
• Push: Adds an item to the top of the stack
• Pop: Removes the top item from the stack
• Top: place where push (insertion) and pop (deletion) take place
Push operation on stack
10 is pushed into an empty stack 20 is pushed into a stack already 30 is pushed into a stack already
containing 10 containing 10, 20
10 top 20 30 top
10 top 20
Or, 10 top
10 20 top Or,
10 20 30
Pop operation on stack
Topmost value is popped from a stack Topmost value is popped from a stack
containing 10, 20, 30 (30 is popped) containing 10, 20 (20 is popped)
20 top 10 top
10
Or,
10 20 top
In Python, stack is generally implemented using a list. When implementing a stack using a list, remember the following
points:
• Push operation on stack is implemented using list method append() or +=[values] if one value is to be inserted
• Push operation on stack is implemented using list method extend() or +=[value] if two or more values are to be
inserted
Using Python list methods append() or extend() or +=[value(s)], number of values present in the stack gets
automatically incremented. Unlike other programming languages, no need to increment top through coding.
• Pop operation on stack (one value is deleted) is implemented using list method pop(). Using Python list method
pop(), number values present in the stack gets automatically decremented. Unlike other programming languages,
no need to decrement top through coding.
Using Python list to implement a stack, stack's top is just a concept.
• Pop all values from stack: Using a while / for loop to delete all the values present in a stack
• Display the stack using a while / for loop without deleting the values present in the stack
Python functions based on stack using Python list:
• Write a Python function to create a stack to contain integers divisible by 5 obtained from a list of n (user input) 2-
digit random integers
• Write a Python function to display the stack
• Write a Python function to delete the topmost value from the stack and return the value
• Write a Python function to delete and display all the elements from a stack and at the end display 'Stack is Empty!'
• Write a Python function to check that the stack is empty; function returns True if stack is empty or returns False
otherwise
from random import randint
stack=[]
def stackpush(stack):
n=int(input('Input n? '))
for x in range(n):
val=randint(10,99)
if val%5==0: stack.append(val)
#if val%5==0: stack+=[val]
'''
OR,
def stackpush(stack):
n=int(input('Input n? '))
nlist=[randint(10, 99) for x in range(n)]
for val in nlist:
if val%5==0: stack.append(val)
#if val%5==0: stack+=[val]
'''
def stackshow(stack):
if stack==[]: print('Stack is Empty!')
#if len(stack)==0: print('Stack is Empty!')
else:
for x in range(len(stack)-1, -1, -1): print(stack[x])
#for val in stack[::-1]: print(val)
def stackpop(stack):
if stack==[]: print('Stack is Empty!')
#if len(stack)==0: print('Stack is Empty!')
else:
return stack.pop()
def stackpopall(stack):
while stack:
print(stack.pop())
print('Stack is Empty!')
'''
OR,
def stackpopall(stack):
for x in range(len(stack)):
print(stack.pop())
print('Stack is Empty!')
'''
def isempty(stack):
return stack==[]
#return len(stack)==0