-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCircularQ.py
More file actions
116 lines (110 loc) · 2.08 KB
/
CircularQ.py
File metadata and controls
116 lines (110 loc) · 2.08 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
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
##QSize = 3
##Front = 0
##End = 0
##NoOfItems = 0
##Q = [""] * QSize
##
##def InitializeQ():
## global Front
## global End
## global NoOfItems
## Front = -1
## End = -1
## NoOfItems = 0
##
##def join(n):
## global Front
## global End
## global NoOfItems
## global Q
## global QSize
## if NoOfItems < QSize:
## End +=1
## if End > QSize-1:
## End = -1
## Q[End] = n
## NoOfItems += 1
## else:
## print('Q is full')
##
##def leave():
## global Front
## global End
## global NoOfItems
## global Q
## global QSize
## Item = ""
## if NoOfItems > 0:
## Item = Q[Front]
## NoOfItems -= 1
## if NoOfItems==0:
## InitializeQ()
## else:
## Front = Front +1
## if Front>QSize-1:
## Front = -1
## else:
## print('Q is empty')
## return Item
##
##join('A')
##join('B')
##join('C')
##join('D')
###join ('E')
##print(leave())
##print(leave())
##print(leave())
Q_SIZE = 3
front = 0
end = 0
no_of_items = 0
q = [""] * Q_SIZE
def initialize_q():
global front
global end
global no_of_items
front = 0
end = 0
no_of_items = 0
def join(n):
global front
global end
global no_of_items
global q
global Q_SIZE
if no_of_items < Q_SIZE:
end += 1
if end == Q_SIZE:
end = 0
q[end] = n
no_of_items += 1
else:
print('Q is full')
def leave():
global front
global end
global no_of_items
global q
global Q_SIZE
item = ""
if no_of_items > 0:
item = q[front]
no_of_items -= 1
if no_of_items == 0:
initialize_q()
else:
front += 1
if front == Q_SIZE:
front = 0
else:
print('Q is empty')
return item
join('A')
join('B')
join('C')
join('D')
print(leave())
print(leave())
print(leave())
print(leave())