-
Notifications
You must be signed in to change notification settings - Fork 17
/
design-circular-queue.py
133 lines (110 loc) · 3.91 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
import unittest
class MyCircularQueue:
def __init__(self, k):
"""
Initialize your data structure here. Set the size of the queue to be k.
:type k: int
"""
self._length = k
self._queue = [None] * self._length
self._head = None
self._tail = None
def enQueue(self, value):
"""
Insert an element into the circular queue. Return true if the operation is successful.
:type value: int
:rtype: bool
"""
if self.isFull():
return False
if self.isEmpty():
self._tail = 0
self._head = 0
else:
self._tail = (self._tail + 1) % self._length
self._queue[self._tail] = value
return True
def deQueue(self):
"""
Delete an element from the circular queue. Return true if the operation is successful.
:rtype: bool
"""
if self.isEmpty():
return False
if self._tail == self._head:
self._tail = None
self._head = None
else:
self._head = (self._head + 1) % self._length
return True
def Front(self):
"""
Get the front item from the queue.
:rtype: int
"""
return -1 if self.isEmpty() else self._queue[self._head]
def Rear(self):
"""
Get the last item from the queue.
:rtype: int
"""
return -1 if self.isEmpty() else self._queue[self._tail]
def isEmpty(self):
"""
Checks whether the circular queue is empty or not.
:rtype: bool
"""
return self._head is None and self._tail is None
def isFull(self):
"""
Checks whether the circular queue is full or not.
:rtype: bool
"""
return False \
if self.isEmpty() else self._head == (self._tail + 1) % self._length
class TestMyCircularQueue(unittest.TestCase):
def setUp(self):
self.queue = MyCircularQueue(3)
def test_empty(self):
self.assertFalse(self.queue.deQueue())
self.assertEqual(self.queue.Front(), -1)
self.assertEqual(self.queue.Rear(), -1)
self.assertFalse(self.queue.isFull())
self.assertTrue(self.queue.isEmpty())
def test_1(self):
self.assertTrue(self.queue.enQueue(1))
self.assertEqual(self.queue.Rear(), 1)
self.assertEqual(self.queue.Front(), 1)
self.assertFalse(self.queue.isFull())
self.assertFalse(self.queue.isEmpty())
self.assertTrue(self.queue.enQueue(2))
self.assertEqual(self.queue.Front(), 1)
self.assertEqual(self.queue.Rear(), 2)
self.assertFalse(self.queue.isFull())
self.assertFalse(self.queue.isEmpty())
self.assertTrue(self.queue.enQueue(3))
self.assertEqual(self.queue.Front(), 1)
self.assertEqual(self.queue.Rear(), 3)
self.assertTrue(self.queue.isFull())
self.assertFalse(self.queue.isEmpty())
self.assertFalse(self.queue.enQueue(4))
self.assertEqual(self.queue.Front(), 1)
self.assertEqual(self.queue.Rear(), 3)
self.assertTrue(self.queue.isFull())
self.assertFalse(self.queue.isEmpty())
self.assertTrue(self.queue.deQueue())
self.assertEqual(self.queue.Front(), 2)
self.assertEqual(self.queue.Rear(), 3)
self.assertFalse(self.queue.isFull())
self.assertFalse(self.queue.isEmpty())
self.assertTrue(self.queue.deQueue())
self.assertEqual(self.queue.Front(), 3)
self.assertEqual(self.queue.Rear(), 3)
self.assertFalse(self.queue.isFull())
self.assertFalse(self.queue.isEmpty())
self.assertTrue(self.queue.deQueue())
self.assertEqual(self.queue.Front(), -1)
self.assertEqual(self.queue.Rear(), -1)
self.assertFalse(self.queue.isFull())
self.assertTrue(self.queue.isEmpty())
unittest.main()