forked from PriyankaKhire/ProgrammingPracticePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularQ.py
More file actions
52 lines (49 loc) · 1.11 KB
/
CircularQ.py
File metadata and controls
52 lines (49 loc) · 1.11 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
43
44
45
46
47
48
49
50
51
52
#Circular Q
class CircularQ(object):
def __init__(self, size):
self.size = size
self.array = [None for i in range(size)]
self.numElements = 0
# delete from head
self.head = 0
# add from tail
self.tail = 0
def push(self, element):
if(self.numElements == self.size):
print "Q full"
return
self.array[self.tail] = element
self.tail = (self.tail+1)%self.size
self.numElements = self.numElements + 1
print self.array
def pop(self):
if(self.numElements == 0):
print "Q empty"
return
print self.array[self.head]
self.array[self.head] = None
self.head = (self.head+1)%self.size
self.numElements = self.numElements - 1
print self.array
# Main
obj = CircularQ(5)
obj.push(1)
obj.push(2)
obj.push(3)
obj.push(4)
obj.push(5)
obj.push(6)
obj.pop()
obj.push(6)
obj.push(7)
obj.pop()
obj.push(7)
obj.pop()
obj.pop()
obj.pop()
obj.pop()
obj.pop()
obj.pop()
obj.push(8)
obj.pop()
obj.push(9)