1
+ # Python3 program for array implementation of queue
2
+
3
+ # Class Queue to represent a queue
4
+ class Queue :
5
+ # __init__ function
6
+ def __init__ (self , capacity ):
7
+ self .front = self .size = 0
8
+ self .rear = capacity - 1
9
+ self .Q = [None ]* capacity # empty Queue
10
+ self .capacity = capacity
11
+
12
+ # Queue is full when size becomes
13
+ # equal to the capacity
14
+ def isFull (self ):
15
+ return self .size == self .capacity
16
+
17
+ # Queue is empty when size is 0
18
+ def isEmpty (self ):
19
+ return self .size == 0
20
+
21
+ # Function to add an item to the queue.
22
+ # It changes rear and size
23
+ def EnQueue (self , item ):
24
+ if self .isFull ():
25
+ return
26
+ self .rear = (self .rear + 1 ) % (self .capacity )
27
+ self .Q [self .rear ] = item
28
+ self .size = self .size + 1
29
+
30
+ # Function to remove an item from queue.
31
+ # It changes front and size
32
+ def DeQueue (self ):
33
+ if self .isEmpty ():
34
+ print ("-1" ,end = " " )
35
+ return
36
+
37
+ print (str (self .Q [self .front ]),end = " " )
38
+ self .front = (self .front + 1 ) % (self .capacity )
39
+ self .size = self .size - 1
40
+
41
+ # Function to get front of queue
42
+ def que_front (self ):
43
+ if self .isEmpty ():
44
+ print ("Queue is empty" )
45
+ print ("Front item is" , self .Q [self .front ])
46
+
47
+ # Function to get rear of queue
48
+ def que_rear (self ):
49
+ if self .isEmpty ():
50
+ print ("Queue is empty" )
51
+ print ("Rear item is" , self .Q [self .rear ])
52
+
53
+
54
+ #TEST :
55
+ q = Queue (100 )
56
+ t = int (input ())
57
+ for t in range (0 ,t ):
58
+ n = int (input ())
59
+ inp = input ()
60
+ arr = inp .split ()
61
+ i = 0
62
+ while n > 0 :
63
+ if arr [i ]== '1' :
64
+ i = i + 1
65
+ q .EnQueue (arr [i ])
66
+ i = i + 1
67
+ elif arr [i ]== '2' :
68
+ q .DeQueue ()
69
+ i = i + 1
70
+ n -= 1
0 commit comments