About
CONTEST CSE-E PASSWORD : PHASE4 dont open from mine
1.
TASK
Implement the queue ADT. Your queue should be able to implement the following ADT operations
Copy enqueue(self, value): #pushes the value into the front of the queue if full, throws "Queue Full Exception"
dequeue(self): # returns and removes element at the front of the queue if not empty else throws "Queue Empty"
front(self): # returns front element without removing it if the queue is not empty, else throws exception
size(self):#returns the number of elements currently in queue
isEmpty(self): #returns True if queue is empty Input Format:
The first line of the input is the queue size
The second line of input is the number of subsequent operations
List of Operations
S - Size of queue I - is empty E x - Enqueues the value X into the queue D - Dequeue F - Displays the index position of the first element in the queue R - Displays the index position of the last element in the rear of the queue Fr - Displays the element at the front of the queue Output Format:
Each line corresponds to the operation executed.
Sample Input:
Copy 7 16 I E 1 E 7 E 3 E 9 E 4 E 2 S F R I E 23 Fr F R S Sample Output:
Copy
True
1
1 7
1 7 3
1 7 3 9
1 7 3 9 4
1 7 3 9 4 2
6
0
5
False
1 7 3 9 4 2 23
1
0
6
7
Note: Queue moves from left to right. This implies that after each Dequeue operation, the front must move to the right(i.e. increment by one).
code :
class MyQueue():
def __init__(self,size):
# this is the queue container called 'queue'
self.queue = []
# front and back indexes
self.f = 0
self.r = -1
# define the queue size 'max_queue_size' and initialize it
self.max_queue_size = size
for i in range(0,self.max_queue_size):
self.queue.append(None)
self.sz=0
# define the enqueue operation which inserts the value into the queue, must throw a queue full exception("Queue Full Exception")
def enqueue(self, value):
#@start-editable@
self.queue=[]
#@end-editable@
return
# returns first elt of the queue if not empty, else throws "Queue Empty Exception"
def dequeue(self):
if self.size()==0:
print("Queue Empty Exception")
else:
#@start-editable@
#@end-editable@
return obj
# returns front element without removing it if the queue is not empty, else throws exception ("Queue Empty Exception")
def front(self):
#@start-editable@
#@end-editable@
# returns True if queue is empty
def isEmpty(self):
#@start-editable@
#@end-editable@
# returns the number of elements currently in queue
def size(self):
#@start-editable@
#@end-editable@
def printQueue(self):
if (self.isEmpty()):
print ("Queue Empty")
else:
for i in range(self.max_queue_size):
if self.queue[i]!=None:
print(self.queue[i],end=" ")
print(" ")
def testqueue():
queuesize=int(input())
q1 = MyQueue(queuesize)
inputs=int(input())
while inputs>0:
command=input()
operation=command.split()
if(operation[0]=="S"):
print(q1.size())
elif(operation[0]=="I"):
print(q1.isEmpty())
elif(operation[0]=="E"):
q1.enqueue(int(operation[1]))
elif(operation[0]=="D"):
print(q1.dequeue())
q1.printQueue()
elif(operation[0]=="Fr"):
print(q1.front())
elif(operation[0]=="F"):
print(q1.f)
elif(operation[0]=="R"):
print(q1.r)
inputs-=1
def main(): testqueue()
if __name__ == '__main__': main()
ok vaa
ANS