STACK PROGRAMS
STACK PROGRAMS
1. Write the functions in python, PUSH(L, data), POP(L), DISPLAY(L) to add a new integer,
delete an integer from a List of numbers L and display the element of the stack, considering them
to act as push and pop operations of the Stack data
structure.
Program:
2. Write a function PUSH(L) and POP(Stk), where L is List of integers and Stk is a stack. In
PUSH function create two stack StkO and StkE, then read the list L and push all the even element
in StkE and push all the odd element in StkO. At last return both the stacks. In POP function pop
the number from the stack and display.
PROGRAM:
def PUSH(L):
StkO = []
StkE = []
for i in L:
if i%2 == 0:
StkE.append(i)
else:
StkO.append(i)
return StkO, StkE
def POP(Stk):
if len(Stk) == 0
print(“Stack is Empty”)
else:
while len(Stk) != 0:
print(Stk.pop())
3. A list, NList contains following record as list elements: [City, Country, distance from Delhi] Each of
these records are nested together to form a nested list. Write the following user defined functions in
Python to perform the specified operations on the stack named travel. (i) Push_element(NList): It
takes the nested list as an argument and pushes a list object containing name of the city and
country, which are not in India and distance is less than 3500 km from Delhi. (ii) Pop_element(): It
pops the objects from the stack and displays them. Also, the function should display “Stack Empty”
when there are no elements in the stack.
PROGRAM:
4. A list contains following record of a customer: [Customer_name, Phone_number, City] Write the
following user defined functions to perform given operations on the stack named ‘status’: (i)
Push_element() - To Push an object containing name and Phone number of customers who live in
Goa to the stack (ii) Pop_element() - To Pop the objects from the stack and display them. Also,
display “Stack Empty” when there are no elements in the stack.
PROGRAM:
5. Sakshi has created a dictionary D, containing names and salary as key value pairs of 5
employees. Write two separate functions to perform the following operations:
● PUSH(HS, D), where HS is the stack and D is the dictionary which containing names and
salaries. Push the keys (name of the employee) of the dictionary into a stack, where the
corresponding value (salary) is greater than ₹75,000.
● POP(HS), where HS is the stack. Pop and display the content of the stack.
PROGRAM:
6. Write the functions in python, PUSH(Stk, L) and POP(Stk), where Stk is the stack and L is a
list of strings. In PUSH function read the list and push those string into the stack which start with
a vowel. And in POP function display the
stack.
PROGRAM:
while len(Stk) != 0:
x = Stk.pop()
print(x)