-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathQueue_.py
More file actions
42 lines (35 loc) · 1.06 KB
/
Queue_.py
File metadata and controls
42 lines (35 loc) · 1.06 KB
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
class Queue_():
def __init__(self, length=10):
self.head = 0
self.tail = 1
self.length = length
self.Q = [0 for i in range(self.length)]
# Actually there are only length-1 spaces in Q.
# And we need to leave one space to determine whether fullStack or emptyStack.
# So the available space is length - 2
def QueueEmpty(self):
if (self.head + 1) % self.length == self.tail:
return True
return False
def QueueFull(self):
if (self.tail + 1) % self.length == self.head:
return True
return False
def Enqueue(self, x):
if self.QueueFull():
return False
else:
self.Q[self.tail] = x
self.tail = (self.tail + 1) % self.length
return True
def Dequeue(self):
if self.QueueEmpty():
return None
else:
self.head += 1
return self.Q[self.head]
Q = Queue_()
for i in range(0, 10):
Q.Enqueue(i)
while not Q.QueueEmpty():
print Q.Dequeue()