-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.py
More file actions
66 lines (62 loc) · 2 KB
/
queue.py
File metadata and controls
66 lines (62 loc) · 2 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
import time
queue = ["Pineapple", "apple", "mango", "watermelon", "grape", "pear"]
fPointer = 0
rPointer = 5
carryOn = True
element = 0
def enqueue(rear,item,element,front):
if rear == 5 and "." in queue:
added = False
while added == False:
if queue[element] == ".":
queue[element] = item
rear = element
element = 0
added = True
return rear
else:
element += 1
elif front-rear == 1 or rear-front == 1 or rear == 5:
print("Error: queue is full...")
return rear
else:
rear += 1
queue[rear] = item
return rear
def dequeue(front, rear):
if front > 5:
front = 0
if rear < 0:
print("Error: queue is empty...")
return front, 0
else:
dequeuedItem = queue[front]
queue[front] = "."
front += 1
return front, dequeuedItem
#main program -------------------------------------------------------------------------------------------------
while carryOn == True:
z=0
print(" ___________")
for item in queue:
if z == fPointer:
print(f"f-->| {queue[z]}")
elif z == rPointer:
print(f"r-->| {queue[z]}")
else:
print(f" | {queue[z]}")
z+=1
time.sleep(1)
print("Enter:\nEnqueue if you want to add an item to the queue(1)\nDequeue if you want to remove the item from the front of the queue(2)\nEnd if you want to end the program(3)")
choice = int(input("\nEnter your decision: ").title())
if choice == 2:
fPointer, dequeuedItem = dequeue(fPointer, rPointer)
elif choice == 1:
item = input("Enter item you want to enqueue: ")
rPointer = enqueue(rPointer,item,element,fPointer)
elif choice == 3:
carryOn = False
print("\nHave a good day")
else:
print("Please try again.")
print("-------------------------------------------------------------------------")