Skip to content

Commit 5735304

Browse files
authored
Create design-circular-queue.py
1 parent 7b7be79 commit 5735304

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

Python/design-circular-queue.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Time: O(1)
2+
# Space: O(k)
3+
4+
# Design your implementation of the circular queue.
5+
# The circular queue is a linear data structure in which
6+
# the operations are performed based on FIFO (First In First Out)
7+
# principle and the last position is connected back to
8+
# the first position to make a circle. It is also called ‘Ring Buffer’.
9+
# One of the Benefits of the circular queue is that
10+
# we can make use of the spaces in front of the queue.
11+
# In a normal queue, once the queue becomes full,
12+
# we can not insert the next element even if there is a space in front of the queue.
13+
# But using the circular queue, we can use the space to store new values.
14+
# Your implementation should support following operations:
15+
#
16+
# MyCircularQueue(k): Constructor, set the size of the queue to be k.
17+
# Front: Get the front item from the queue. If the queue is empty, return -1.
18+
# Rear: Get the last item from the queue. If the queue is empty, return -1.
19+
# enQueue(value): Insert an element into the circular queue. Return true if the operation is successful.
20+
# deQueue(): Delete an element from the circular queue. Return true if the operation is successful.
21+
# isEmpty(): Checks whether the circular queue is empty or not.
22+
# isFull(): Checks whether the circular queue is full or not.
23+
# Example:
24+
#
25+
# MyCircularQueue circularQueue = new MycircularQueue(3); // set the size to be 3
26+
# circularQueue.enQueue(1); // return true
27+
# circularQueue.enQueue(2); // return true
28+
# circularQueue.enQueue(3); // return true
29+
# circularQueue.enQueue(4); // return false, the queue is full
30+
# circularQueue.Rear(); // return 3
31+
# circularQueue.isFull(); // return true
32+
# circularQueue.deQueue(); // return true
33+
# circularQueue.enQueue(4); // return true
34+
# circularQueue.Rear(); // return 4
35+
#
36+
# Note:
37+
# - All values will be in the range of [1, 1000].
38+
# - The number of operations will be in the range of [1, 1000].
39+
# - Please do not use the built-in Queue library.
40+
41+
class MyCircularQueue(object):
42+
43+
def __init__(self, k):
44+
"""
45+
Initialize your data structure here. Set the size of the queue to be k.
46+
:type k: int
47+
"""
48+
self.__start = 0
49+
self.__size = 0
50+
self.__buffer = [0] * k
51+
52+
def enQueue(self, value):
53+
"""
54+
Insert an element into the circular queue. Return true if the operation is successful.
55+
:type value: int
56+
:rtype: bool
57+
"""
58+
if self.isFull():
59+
return False
60+
self.__buffer[(self.__start+self.__size) % len(self.__buffer)] = value
61+
self.__size += 1
62+
return True
63+
64+
def deQueue(self):
65+
"""
66+
Delete an element from the circular queue. Return true if the operation is successful.
67+
:rtype: bool
68+
"""
69+
if self.isEmpty():
70+
return False
71+
self.__start = (self.__start+1) % len(self.__buffer)
72+
self.__size -= 1
73+
return True
74+
75+
def Front(self):
76+
"""
77+
Get the front item from the queue.
78+
:rtype: int
79+
"""
80+
return -1 if self.isEmpty() else self.__buffer[self.__start]
81+
82+
def Rear(self):
83+
"""
84+
Get the last item from the queue.
85+
:rtype: int
86+
"""
87+
return -1 if self.isEmpty() else self.__buffer[(self.__start+self.__size-1) % len(self.__buffer)]
88+
89+
def isEmpty(self):
90+
"""
91+
Checks whether the circular queue is empty or not.
92+
:rtype: bool
93+
"""
94+
return self.__size == 0
95+
96+
def isFull(self):
97+
"""
98+
Checks whether the circular queue is full or not.
99+
:rtype: bool
100+
"""
101+
return self.__size == len(self.__buffer)
102+
103+
104+
# Your MyCircularQueue object will be instantiated and called as such:
105+
# obj = MyCircularQueue(k)
106+
# param_1 = obj.enQueue(value)
107+
# param_2 = obj.deQueue()
108+
# param_3 = obj.Front()
109+
# param_4 = obj.Rear()
110+
# param_5 = obj.isEmpty()
111+
# param_6 = obj.isFull()

0 commit comments

Comments
 (0)