Reversing K elements of Queue


Submit solution

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

Authors:
Problem type
Allowed languages
Python

Problem Definition

You are requested to reverse the K elements of a queue using the below given template.Please note that we are following the circular queue implementation and the output also follows the same.

Sample Input 5

10

E 1

E 2

E 3

E 4

E 5

RK 3

RK 3

D

F

RK 2

Sample Output

1 None None None None

1 2 None None None

1 2 3 None None

1 2 3 4 None

1 2 3 4 5

3 2 1 4 5

1 2 3 4 5

1

2

3 2 4 5 None

Code Template

class MyStack():
    def __init__(self,size):
        self.stack = []
        # this is the stack container called 'stack'
        self.max_stack_size = size
        for i in range(self.max_stack_size):
            self.stack.append(None)
        # define the stack size 'max_stack_size' and initialize it
        self.t = -1

    # define the push operation which  pushes the value into the stack, must throw a stack full exception
    def push(self, value):
        if (self.size() == self.max_stack_size):
            print("Stack Full Exception")
        else:
            self.t= self.t+1
            self.stack[self.t] = value
        return


    # returns top element of stack if not empty, else throws stack empty exception
    def pop(self):
        if (self.size() == 0):
            print("Stack Empty Exception")
            return
        else:
            toret = self.stack[self.t]
            self.stack[self.t] = None
            self.t = self.t-1
            return toret


    # returns top element without removing it if the stack is not empty, else throws exception   
    def top(self):
        if (self.size() == 0):
            print ("Stack Empty Exception")
            return
        else:
            return self.stack[self.t]


    # returns True if stack is empty   
    def isEmpty(self):
        return  (self.t<0)

    # returns the number of elements currently in stack 
    def size(self):
        return self.t+1

    def printStack(self):
        if (self.isEmpty()):
            print ("Stack Empty Exception")
        else:
            for i in range(self.max_stack_size):
                if self.stack[i]!=None:
                    print(self.stack[i],end=" ")
            print(" ")

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
        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 Exception")
        else:
            for i in range(self.max_queue_size):
                print(self.queue[i],end=" ")
            print(" ")

    def reverse(self,k):
      #your code here...



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())
        elif(operation[0]=="RK"):
            k=int(operation[1])
            q1.reverse(k)
            q1.printQueue()

        inputs-=1


def main():
    testqueue()

if __name__ == '__main__':
    main()

Comments

There are no comments at the moment.