Basic Queue Implementation


Submit solution

Points: 15 (partial)
Time limit: 10.0s
Memory limit: 64M

Authors:
Problem type
Allowed languages
C++, Python

Problem Statement

This is a simple Queue problem. Your goal is to complete the queue ADT and evaluate its functions. This Queue follows circular queue implementation where front and rear pointers are incremented in a circular way.

Input format:

  • Each testcase contains integers N, M, where N is the size of the stack, and M is the number of operations in the test case. First line will include the queue size, and next M lines will include series of operations of the following type Character, value, where character indicates the queue operation E-enqueue, D-dequeue, F-front, S-size, I-isempty. Only for enqueue, it is followed by a value, others will not be followed by a value. After dequeue, the dequeued element only should be displayed.

Exceptions are of format: (full is followed by queue contents, empty is followed by none)

Queue Full Exception

1 2 3 4 5

Queue Empty Exception

None

Sample Input:

5

15

E 1

E 2

E 3

D

D

D

D

E 5

E 6

E 7

E 8

S

I

F

E 9

Sample output:

1

1 2

1 2 3

1

2

3

Queue Empty Exception

None

5

5 6

7 5 6

7 8 5 6

4

False

5

7 8 9 5 6

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 'max_queue_size' and initialize it

        #code here


        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
    def enqueue(self, value):
        #code here
        return

    # returns first elt of the queue if not empty, else throws queue empty
    # exception

    def dequeue(self):
        #code here
            return element #element is the the popped out item.


    # returns front element without removing it if the queue is not empty, else throws exception   
    def front(self):
        #code here also, handle queue empty condition

    # returns True if queue is empty
    def isEmpty(self):
        return #code here

    # returns the number of elements currently in queue

    def size(self):
        return #code here

    def printQueue(self):
        if (self.isEmpty()):
            print ("Queue Empty Exception")
        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]))
            q1.printQueue()
        elif(operation[0]=="D"):
            print(q1.dequeue())
        elif(operation[0]=="F"):
            print(q1.front())

        inputs-=1


def main():
    testqueue()

if __name__ == '__main__':

main()

Comments

There are no comments at the moment.