-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram14.py
More file actions
69 lines (56 loc) · 1.57 KB
/
Program14.py
File metadata and controls
69 lines (56 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Initialize an empty list to represent the queue
queue = []
# Function to check if the queue is empty
def is_empty():
return len(queue) == 0
# Function to add an element to the end of the queue
def enqueue(item):
queue.append(item)
print(f"Enqueued {item} to the queue")
# Function to remove and return the front element of the queue
def dequeue():
if is_empty():
return "Queue is empty"
return queue.pop(0)
# Function to peek at the front element of the queue
def peek():
if is_empty():
return "Queue is empty"
return queue[0]
# Function to display the current elements in the queue
def display():
if is_empty():
print("Queue is empty")
else:
print("Queue contents:", queue)
# User interaction loop
while True:
print("\n**** QUEUE DEMONSTRATION ******")
print("1. ENQUEUE")
print("2. DEQUEUE")
print("3. PEEK")
print("4. SHOW QUEUE")
print("0. EXIT")
choice = int(input("Enter your choice: "))
if choice == 1:
val = int(input("Enter Item to Enqueue: "))
enqueue(val)
elif choice == 2:
val = dequeue()
if val == "Queue is empty":
print(val)
else:
print("\nDequeued Item was:", val)
elif choice == 3:
val = peek()
if val == "Queue is empty":
print(val)
else:
print("Front Item:", val)
elif choice == 4:
display()
elif choice == 0:
print("Goodbye!")
break
else:
print("Invalid Choice, please try again.")