-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircular Queue
74 lines (68 loc) · 1.91 KB
/
Circular Queue
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
70
71
72
#Circular Queue:
class QueueException(Exception):
print()
class CircularQueue:
def __init__(self,size=5):
self.item=[None]*size
self.front=0
self.count=0
def is_empty(self):
return self.count==0
def size(self):
return self.count
def enqueue(self,data):
if self.count==len(self.item):
self.resize(2*len(self.item))
i=(self.front+self.count)%len(self.item)
self.item[i]=data
self.count+=1
def dequeque(self):
if self.is_empty():
raise QueueException("Queue is empty.")
x=self.item[self.front]
self.item[self.front]=None
self.front=(self.front+1)%len(self.item)
self.count-=1
def peek(self):
if self.is_empty():
raise QueueException("Queue is empty.")
return self.item[self.front]
def display(self):
print(self.item)
def resize(self,n):
old=self.item
self.item=[None]*n
i=self.front
for j in range(self.count):
self.item[j]=old[i]
i=(1+i)%len(old)
self.front=0
if __name__=="__main__":
queue=CircularQueue()
while True:
print("1.Enqueue")
print("2.Dequeue")
print("3.Size")
print("4.Peek")
print("5.Display")
print("6.Quit")
choice=int(input("Enter a choice: "))
if(choice==1):
data=int(input("Enter the element: "))
queue.enqueue(data)
elif(choice==2):
popped=queue.dequeque()
print("Delete elements is: ",popped)
elif(choice==3):
s=queue.size()
print("Size is: ",s)
elif(choice==4):
p=queue.peek()
print("Front is at: ",p)
elif(choice==5):
queue.display()
elif(choice==6):
break
else:
print("Invalid Choice!!")
print()