-
Notifications
You must be signed in to change notification settings - Fork 1
/
CircularQueueClass.py
50 lines (41 loc) · 1014 Bytes
/
CircularQueueClass.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:
"""Queue implementation using circularly linked list for storage"""
class _Node:
__slots__ = '_element', '_next'
def __init__(self.element, next):
self._element = element
self._next = next
def __init__(self):
self._tail = None
self._size = 0
def __len__(self):
return self._size
def is_empty():
return self._size==0
def first(self):
if self.is_empty():
raise Empty('Queue is empty')
head = self._tail._next
return head._element
def dequeue(self):
if self.is_empty():
raise Empty('Queue is empty')
oldhead = self._tail._next
if self._size == 1:
self._tail = None
else:
self._tail._next = oldhead._next
self._size -= 1
return oldhead._element
def enqueque(self, e):
newest = self._Node(e, None)
if self.is_empty():
newest._next = newest
else:
newest._next = self._tail._next
self._tail._next = newest
self._tail = newest
self._size += 1
def rotate(self):
if self._size > 0:
self._tail = self._tail._next