CBSE Class 12 Computer Science Question 7 of 26

Data Structures in Python — Question 8

Back to all questions
8
Question

Question 8

Write an interactive menu-driven program to implement Stack using list. The list contains the names of students.

Solution
def push(student):
    h = input("Enter a name: ")
    student.append(h)

def pop(student):
    if len(student) == 0:
        print("No name to delete")
    else:
        print("Deleted name is:", student.pop())

def display(student):
    print(student)

student = []
while True:
    print("1. Add name")
    print("2. Delete name")
    print("3. Display names")
    print("4. Exit")
    op = int(input("Enter the Choice: "))
    if op == 1:
        push(student)
    elif op == 2:
        pop(student)
    elif op == 3:
        display(student)
    elif op == 4:
        print("Exiting program.")
        break
    else:
        print("Invalid choice. Please enter a number between 1 and 4.")
Output
1. Add name
2. Delete name
3. Display names
4. Exit
Enter the Choice: 1
Enter a name: Prajwal
1. Add name
2. Delete name
3. Display names
4. Exit
Enter the Choice: 1
Enter a name: Amrita
1. Add name
2. Delete name
3. Display names
4. Exit
Enter the Choice: 1
Enter a name: Kavya
1. Add name
2. Delete name
3. Display names
4. Exit
Enter the Choice: 1
Enter a name: Drishti
1. Add name
2. Delete name
3. Display names
4. Exit
Enter the Choice: 3
['Prajwal', 'Amrita', 'Kavya', 'Drishti']
1. Add name
2. Delete name
3. Display names
4. Exit
Enter the Choice: 2
Deleted name is: Drishti
1. Add name
2. Delete name
3. Display names
4. Exit
Enter the Choice: 3
['Prajwal', 'Amrita', 'Kavya']
1. Add name
2. Delete name
3. Display names
4. Exit
Enter the Choice: 4
Exiting program.
Answer

def
push
(
student
):
h
=
input
(
"Enter a name: "
)
student
.
append
(
h
)
def
pop
(
student
):
if
len
(
student
)
==
0
:
print
(
"No name to delete"
)
else
:
print
(
"Deleted name is:"
,
student
.
pop
())
def
display
(
student
):
print
(
student
)
student
=
[]
while
True
:
print
(
"1. Add name"
)
print
(
"2. Delete name"
)
print
(
"3. Display names"
)
print
(
"4. Exit"
)
op
=
int
(
input
(
"Enter the Choice: "
))
if
op
==
1
:
push
(
student
)
elif
op
==
2
:
pop
(
student
)
elif
op
==
3
:
display
(
student
)
elif
op
==
4
:
print
(
"Exiting program."
)
break
else
:
print
(
"Invalid choice. Please enter a number between 1 and 4."
)
Output
1. Add name
2. Delete name
3. Display names
4. Exit
Enter the Choice: 1
Enter a name: Prajwal
1. Add name
2. Delete name
3. Display names
4. Exit
Enter the Choice: 1
Enter a name: Amrita
1. Add name
2. Delete name
3. Display names
4. Exit
Enter the Choice: 1
Enter a name: Kavya
1. Add name
2. Delete name
3. Display names
4. Exit
Enter the Choice: 1
Enter a name: Drishti
1. Add name
2. Delete name
3. Display names
4. Exit
Enter the Choice: 3
['Prajwal', 'Amrita', 'Kavya', 'Drishti']
1. Add name
2. Delete name
3. Display names
4. Exit
Enter the Choice: 2
Deleted name is: Drishti
1. Add name
2. Delete name
3. Display names
4. Exit
Enter the Choice: 3
['Prajwal', 'Amrita', 'Kavya']
1. Add name
2. Delete name
3. Display names
4. Exit
Enter the Choice: 4
Exiting program.