forked from PriyankaKhire/ProgrammingPracticePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicQ.py
More file actions
66 lines (59 loc) · 1.56 KB
/
BasicQ.py
File metadata and controls
66 lines (59 loc) · 1.56 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#Circular queue
class circularQ:
#Add element to q from back and remove from front
def __init__(self, capacity):
self.capacity = capacity
self.q = [None] * capacity
self.back = 0
self.front = 0
self.elementCount = 0
def isEmpty(self):
if(self.elementCount == self.capacity):
return False
return True
def push(self, value):
#Check if q is full
if(not self.isEmpty()):
print "Q is full"
return
#Add the element
print "Added element "+str(value)
self.q[self.back] = value
self.back = (self.back % self.capacity)+1
if(self.back == self.capacity):
self.back = 0
self.elementCount = self.elementCount + 1
def pop(self):
#Check if q is empty
if(self.isEmpty()):
print "Q is empty"
return
#Remove element
print "Removed element "+str(self.q[self.front])
self.q[self.front] = None
self.front = (self.front%self.capacity) + 1
if(self.front == self.capacity):
self.front = 0
self.elementCount = self.elementCount - 1
def display(self):
print self.q
#Main program
obj1 = circularQ(5)
print obj1.capacity
print obj1.q
obj1.push(1)
obj1.push(2)
obj1.push(3)
obj1.push(4)
obj1.push(5)
obj1.display()
obj1.pop()
obj1.push(6)
obj1.display()
obj1.pop()
obj1.push(7)
obj1.pop()
obj1.push(8)
obj1.pop()
obj1.push(9)
obj1.display()