You are on page 1of 2

#Q1: Write a program to insert, delete, display and find the peak value in a

stack
#Ans:

#functions

def push(a,val):
a.append(val)
print("Value Inserted successfully")

def popitem(a):
x=a.pop()
print(x,"Popped Successfully")

def peak(a):
i=len(a)-1
print("Peak element",a[i])

def display(a):
for i in range(len(a)-1,-1,-1):
print(a[i])

#main

a=[]
while True:
ch=int(input("1 to push\n2 to Pop\n3 to Peak \n4 to Display all \n5 to Exit
\nEnter your choice: "))
if ch==1:
val=int(input("Enter element to push: "))
push(a,val)
elif ch==2:
if len(a)==0:
print("stack underflow")
else:
popitem(a)
elif ch==3:
if len(a)==0:
print("stack underflow")
else:
peak(a)
elif ch==4:
if len(a)==0:
print("stack underflow")
else:
display(a)
else:
break

#Q2: Write a program to display unique vowels present in the given word using
stack.
#Ans:
vowels=['a','e','i','o','u']
word=input ("Enter the word to search for vowels: ")
stack=[]
for letter in word:
if letter in vowels:
if letter not in stack:
stack.append(letter)
print(stack)
print("The number of different vowels present in", word, "is", len(stack))

#Q3: Write a program to create a Stack call Employee, to perform the basic
operations on stack using list.
#Ans:

employee=[]
c="y"
while (c=="y"):
print("1, Push")
print("2, Pop")
print("3, Display")
choice=int(input("Enter your choice : "))
if (choice==1):
e_id=input("Enter employee no. :")
ename=input("Enter employee name :")
emp=(e_id,ename)
employee.append(emp)
elif (choice==2):
if(employee==[]):
print("stack empty")
else:
e_id,ename=employee.pop()
print("Deleted element is :",e_id,ename)
elif (choice==3):
i=len(employee)
while i>0: #To display elements from last element to first'
print(employee[i-1])
i=i-1
else:
print("Wrong input")
c=input("Do you want to continue or not ? :")

You might also like