A Modified Queue


Submit solution

Points: 10 (partial)
Time limit: 20.0s
Memory limit: 64M

Author:
Problem type
Allowed languages
C++, Python

Problem Statement

This is a simple Queue problem. Your goal is to implement an additional queue function findAndDelete(element). The aim is to find an element in the queue and remove it from the queue. If the element is not in the queue, you should return "Element Not Found". Note that 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, FD - findAndDelete. Only for enqueue and findAndDelete, 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 0:

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 0:

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

Sample Input 1:

5

5

E 1

E 2

E 3

E 4

FD 2

Sample Output: 1

1 2

1 2 3

1 2 3 4

1 3 4

CODE TEMPLATE

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
    def enqueue(self, value):
        if  self.size() == self.max_queue_size:
            print ("Queue Full Exception")
            return
        else:
            self.r = (self.r+1) % self.max_queue_size
            self.queue[self.r] = value
            self.sz+=1
            #self.printQueue()
        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:
            obj = self.queue[self.f]
            self.queue[self.f] = None                
            self.f = (self.f+1)%self.max_queue_size            
            self.sz = self.sz-1
            return obj


    # returns front element without removing it if the queue is not empty, else throws exception   
    def front(self):
        if (self.size()!=0):
            return self.queue[self.f]
        else:
            print("Queue Empty Exception")

    # returns True if queue is empty
    def isEmpty(self):
        return (self.sz==0)

    # returns the number of elements currently in queue

    def size(self):
        return (self.sz)

    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 findAndDelete(self,element):
        flag=0
        if self.size() == 0:
            print("Queue Empty Exception")
            return
        #CODE HERE
        if(flag==0):
            print("Element Not Found")
        else:
            self.printQueue()


#----------------------------------Driver Code------ Do Not Modify-------------------------


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())
            q1.printQueue()           
        elif(operation[0]=="F"):
            print(q1.front())
        elif(operation[0]=="R"):
            print(q1.r)
        elif(operation[0]=="FD"):
            q1.findAndDelete(int(operation[1]))
        inputs-=1


def main():
    testqueue()

if __name__ == '__main__':
    main()

Comments

There are no comments at the moment.