-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathImplementationUsingLists.py
70 lines (60 loc) · 1.78 KB
/
ImplementationUsingLists.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
# Python3 program for array implementation of queue
# Class Queue to represent a queue
class Queue:
# __init__ function
def __init__(self, capacity):
self.front = self.size = 0
self.rear = capacity - 1
self.Q = [None]*capacity # empty Queue
self.capacity = capacity
# Queue is full when size becomes
# equal to the capacity
def isFull(self):
return self.size == self.capacity
# Queue is empty when size is 0
def isEmpty(self):
return self.size == 0
# Function to add an item to the queue.
# It changes rear and size
def EnQueue(self, item):
if self.isFull():
return
self.rear = (self.rear + 1) % (self.capacity)
self.Q[self.rear] = item
self.size = self.size + 1
# Function to remove an item from queue.
# It changes front and size
def DeQueue(self):
if self.isEmpty():
print("-1",end=" ")
return
print(str(self.Q[self.front]),end=" ")
self.front = (self.front + 1) % (self.capacity)
self.size = self.size -1
# Function to get front of queue
def que_front(self):
if self.isEmpty():
print("Queue is empty")
print("Front item is", self.Q[self.front])
# Function to get rear of queue
def que_rear(self):
if self.isEmpty():
print("Queue is empty")
print("Rear item is", self.Q[self.rear])
#TEST :
q = Queue(100)
t = int(input())
for t in range(0,t):
n = int(input())
inp = input()
arr=inp.split()
i=0
while n>0:
if arr[i]=='1':
i=i+1
q.EnQueue(arr[i])
i=i+1
elif arr[i]=='2':
q.DeQueue()
i=i+1
n-=1