Computer Science
Write a program to insert or delete an element from a Queue depending upon the user's choice. The elements are not shifted after insertion or deletion.
Python
Python Queue
2 Likes
Answer
def enqueue(queue):
element = input("Enter the element to enqueue: ")
queue.append(element)
def dequeue(queue):
if queue:
removed_element = queue.pop(0)
print("Dequeued element:", removed_element)
else:
print("Queue is empty")
queue = []
while True:
print("QUEUE OPERATIONS")
print("1. Enqueue element")
print("2. Dequeue element")
print("3. Exit")
choice = input("Enter your choice (1-3): ")
if choice == '1':
enqueue(queue)
elif choice == '2':
dequeue(queue)
elif choice == '3':
print("Exiting program")
break
else:
print("Invalid choice. Please enter a valid option.")
Output
QUEUE OPERATIONS
1. Enqueue element
2. Dequeue element
3. Exit
Enter your choice (1-3): 1
Enter the element to enqueue: 4
QUEUE OPERATIONS
1. Enqueue element
2. Dequeue element
3. Exit
Enter your choice (1-3): 1
Enter the element to enqueue: 8
QUEUE OPERATIONS
1. Enqueue element
2. Dequeue element
3. Exit
Enter your choice (1-3): 1
Enter the element to enqueue: 12
QUEUE OPERATIONS
1. Enqueue element
2. Dequeue element
3. Exit
Enter your choice (1-3): 2
Dequeued element: 4
QUEUE OPERATIONS
1. Enqueue element
2. Dequeue element
3. Exit
Enter your choice (1-3): 3
Exiting program
Answered By
1 Like
Related Questions
Write a program that, depending upon the user's choice, either adds or removes an element from a Stack.
Write a program that, depending upon the user's choice, either pushes or pops an element in a Stack. The elements are shifted towards the right so that the top always remains at 0th (zeroth) index.
Two lists Lname and Lage contain name of person and age of person respectively. A list named Lnameage is empty. Write functions as per details given below:
- Push_na(): It will push the tuple containing pair of name and age from Lname and Lage whose age is above 50.
- Pop_na(): It will remove the last pair of name and age and also print name and age of the removed person. It should also print "underflow" if there is nothing to remove.
For example, the two lists have the following data:
Lname=['Narender', 'Jaya', 'Raju', 'Ramesh', 'Amit', 'Piyush' ]
Lage= [45, 23, 59, 34, 51, 43]After Push_na() the Lnameage Stack contains:
[('Raju', 59), ('Amit', 51)]
The output of first execution of Pop_na() is:
The name removed is Amit
The age of person is 51A dictionary stu contains rollno and marks of students. Two empty lists stack_roll and stack_mark will be used as Stack. Two functions Push_stu() and Pop_stu() are defined and perform the following operation:
- Push_stu(): It reads dictionary stu and adds keys into stack_roll and values into stack_marks for all students who secured more than 60 marks.
- Pop_stu(): It removes last rollno and marks from both list and prints "underflow" if there is nothing to remove.
For example,
stu={ 1 : 56, 2 : 45, 3 : 78, 4 : 65, 5 : 35, 6 : 90 }
Values of stack_roll and stack_mark after push_stu() function:
[3, 4, 6] and {78, 65, 90}