-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign_circular_queue.py
184 lines (140 loc) · 5.39 KB
/
design_circular_queue.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'''
Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".
One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.
Implementation the MyCircularQueue class:
MyCircularQueue(k) Initializes the object with the size of the queue to be k.
int Front() Gets the front item from the queue. If the queue is empty, return -1.
int Rear() Gets the last item from the queue. If the queue is empty, return -1.
boolean enQueue(int value) Inserts an element into the circular queue. Return true if the operation is successful.
boolean deQueue() Deletes an element from the circular queue. Return true if the operation is successful.
boolean isEmpty() Checks whether the circular queue is empty or not.
boolean isFull() Checks whether the circular queue is full or not.
You must solve the problem without using the built-in queue data structure in your programming language.
'''
class Node:
def __init__(self, val, prev, next):
self.val = val
self.prev = prev
self.next = next
class MyCircularQueue:
def __init__(self, k: int):
self.head = None
self.end = None
self.size = 0
self.k = k
def enQueue(self, value: int) -> bool:
if self.size == 0:
newnode = Node(value, None, None)
self.head = newnode
self.end = newnode
self.end.next = self.head
self.head.prev = self.end
self.size += 1
return True
elif self.size != 0 and self.size < self.k:
newnode = Node(value, None, None)
self.end.next = newnode
self.end = self.end.next
self.end.next = self.head
self.head.prev = self.end
self.size += 1
return True
elif self.size >= self.k:
return False
def deQueue(self) -> bool:
if self.size != 0 and self.size <= self.k:
self.head = self.head.next
self.head.prev = self.end
self.end.next = self.head
self.size -= 1
return True
else:
return False
def Front(self):
if self.size == 0:
return -1
return self.head.val
def Rear(self) -> int:
if self.size == 0:
return -1
return self.end.val
def isEmpty(self):
if self.size == 0:
return True
return False
def isFull(self):
if self.size >= self.k:
return True
return False
-------------------------------------------------------------------------------------
class ListNode:
def __init__(self, value: int):
self.value = value
self.next = None
class MyCircularQueue:
def __init__(self, k: int):
self.head = self.tail = None
self.len = 0
self.limit = k
def enQueue(self, value: int) -> bool:
if (self.isFull()):
return False
if (self.head is None):
self.tail = self.head = ListNode(value)
self.len += 1
return True
self.tail.next = ListNode(value)
self.tail = self.tail.next
self.len += 1
return True
def deQueue(self) -> bool:
if (self.isEmpty()):
return False
self.head = self.head.next
self.len -= 1
return True
def Front(self) -> int:
if (self.isEmpty()):
return -1
return self.head.value
def Rear(self) -> int:
if (self.isEmpty()):
return -1
return self.tail.value
def isEmpty(self) -> bool:
return self.len == 0
def isFull(self) -> bool:
return self.len == self.limit
-----------------------------------------------------------------------
class MyCircularQueue:
def __init__(self, k: int):
self.k = k
self.rear, self.front = 0, 0
self.arr = [None] * self.k
def inc(self, i: int) -> int:
return 0 if i >= self.k-1 else i + 1
def dec(self, i: int) -> int:
return self.k-1 if i == 0 else i - 1
def enQueue(self, value: int) -> bool:
if self.arr[self.rear] == None:
self.arr[self.rear] = value
self.rear = self.inc(self.rear)
return True
return False
def deQueue(self) -> bool:
if self.arr[self.front] == None: return False
self.arr[self.front] = None
self.front = self.inc(self.front)
return True
def Front(self) -> int:
if self.arr[self.front] == None: return -1
return self.arr[self.front]
def Rear(self) -> int:
if self.arr[self.dec(self.rear)] == None: return -1
return self.arr[self.dec(self.rear)]
def isEmpty(self) -> bool:
if self.arr[self.front] is None and self.arr[self.rear] is None: return True
return False
def isFull(self) -> bool:
if self.arr[self.front] and self.arr[self.rear]: return True
return False