0% found this document useful (0 votes)
46 views4 pages

05 01 2024 (Stack)

The document describes functions to perform operations on a stack data structure like push, pop, peek and display. It includes examples of pushing customer details and stationary item names into a stack based on certain criteria.

Uploaded by

lucifergamer996
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views4 pages

05 01 2024 (Stack)

The document describes functions to perform operations on a stack data structure like push, pop, peek and display. It includes examples of pushing customer details and stationary item names into a stack based on certain criteria.

Uploaded by

lucifergamer996
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

STACK

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

For example:
If the lists of customer details are:

[“Gurdas”, “99999999999”,”Goa”]
[“Julee”, “8888888888”,”Mumbai”] [“Murugan”,”77777777777”,”Cochin”]
[“Ashmit”, “1010101010”,”Goa”]

The stack should contain


[“Ashmit”,”1010101010”]
[“Gurdas”,”9999999999”]
The output should be:
[“Ashmit”,”1010101010”]
[“Gurdas”,”9999999999”]
Stack Empty

stack=[]
def push(stack,item):
# if len(stack)==5:
# print("stack is overflow")
# else:
stack.append(item)
def pop():
if len(stack)==0:
print("Stack is Underflow")
else:
print(stack.pop()," Pop from stack")
def peek():
if len(stack)==0:
print("Stack is Empty")
else:
print(stack[-1]," <-- is at top")
def display():
if len(stack) == 0:
print("Stack is Empty")
else:
print(stack[-1], " <-- is at top")
for i in range(len(stack)-2,-1,-1):
print(stack[i])
l=[["Gurdas", "99999999999","Goa"],
STACK

["Julee", "8888888888","Mumbai"],
["Murugan","77777777777","Cochin"],
["Ashmit", "1010101010","Goa"]]

while True:

x=int(input("1.Push\n2.Pop\n3.Peek\n4.Display\n5.Exit\n
Enter You Choice 1- 5: "))
if x==1:
for i in l:
if i[-1]=="Goa":
a=[i[0],i[1]]
push(stack,a)

elif x==2:
pop()
elif x==3:
peek()
elif x==4:
display()
elif x==5:
break

2. Write a function in Python, Push(SItem) where , SItem is a dictionary containing the


details of stationary items– {Sname:price}.

The function should push the names of those items in the stack who have price greater
than 75. Also display the count of elements pushed into the stack.

For example:
Ditem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}

The stack should contain


Notebook
Pen

The output should be:


The count of elements in the stack is 2

stack=[]
def push(stack,item):
STACK

# if len(stack)==5:
# print("stack is overflow")
# else:
stack.append(item)
def pop():
if len(stack)==0:
print("Stack is Underflow")
else:
print(stack.pop()," Pop from stack")
def peek():
if len(stack)==0:
print("Stack is Empty")
else:
print(stack[-1]," <-- is at top")
def display():
if len(stack) == 0:
print("Stack is Empty")
else:
print(stack[-1], " <-- is at top")
for i in range(len(stack)-2,-1,-1):
print(stack[i])
Ditem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}

while True:

x=int(input("1.Push\n2.Pop\n3.Peek\n4.Display\n5.Exit\n
Enter You Choice 1- 5: "))
if x==1:

for i in Ditem:
if Ditem[i]>75:
push(stack,i)

elif x==2:
pop()
elif x==3:
peek()
elif x==4:
display()
elif x==5:
break
STACK

OR

stack=[]
Ditem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
def push():
for i in Ditem:
if Ditem[i] > 75:
stack.append(i)
def pop():
if len(stack)==0:
print("Stack is Underflow")
else:
print(stack.pop()," Pop from stack")
push()
print("Total Elements in Stack = ",len(stack))

You might also like