-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircularLinkedQueue.py
50 lines (50 loc) · 1.08 KB
/
CircularLinkedQueue.py
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
class CircularQueue:
class Node():
def __init__(self,e):
self.e=e
self.next=None
def __init__(self):
self.tail=None
self.size=0
def __len__(self):
return self.size
def first(self):
h=self.tail.next
return h.e
def enqueue(self,e):
t=self.Node(e)
if(self.size==0):
t.next=t
else:
t.next=self.tail.next
self.tail.next=t
self.tail=t
self.size+=1
def dequeue(self):
g=self.tail.next
if(self.size==1):
self.tail=0
else:
self.tail.next=g.next
self.size-=1
return g.e
def rotate(self):
if(self.size>0):
self.tail=self.tail.next
def __str__(self):
curr=self.tail.next
L=[]
while(curr!=self.tail):
L.append(str(curr.e))
curr=curr.next
L.append(str(self.tail.e))
return "".join(L)
F=CircularQueue()
F.enqueue(5)
F.enqueue(7)
F.enqueue(3)
print(F)
F.rotate()
print(F)
F.dequeue()
print(F)