python program to Implement the following (a) A queue as a list which you add and delete items from. (b) A circular queue. (The beginning items of the queue can be reused).

(a) A queue as a list which you add and delete items from. 
(b) A circular queue. (The beginning items of the queue can be reused). 

(a) A queue as a list which you add and delete items from.
code:

class QueuePL:

    def __init__(self):

        self.q=list()

    def isEmpty(self):

        if self.length() is 0:

            return True

        else:

            return False

    def Enqueue(self,val):

        self.q.append(val)

 

    def length(self):

        return len(self.q)

    

    def Dequeue(self):

        if self.isEmpty():

            return None

        else:

            val=self.q.pop(0)

            return val

    def display(self):

        if self.isEmpty():

            print("Queue Empty")

        else:

            print(self.q)

 

queue=QueuePL()

choice=int(input("1.Enqueue\t2.Dequeue\t3.length\t4.check queue empty or not\t5.Display Queue"))

while choice is not 6:

    if choice is 1:

        val=int(input("enter element to be inserted in the queue"))

        queue.Enqueue(val)

    if choice is 2:

        val=queue.Dequeue()

        if val is None:

            print("Queue is empty.Cannot delete from empty queue")

        else:

            print("The removed element is",val)

    if choice is 3:

        l=queue.length()

        print("Number of elements in the queue are",l)

    if choice is 4:

        if(queue.isEmpty() is True):

            print("Queue is empty")

        else:

            print("Queue is not empty")

    if choice is 5:

        queue.display()

    choice=int(input("Enter your choice"))

 o/p:-

A queue as a list which you add and delete items from.

(b) A circular queue. (The beginning items of the queue can be reused).

code:

import array as arr

class QueuePL:

    def __init__(self,maxsize):

        self.q=arr.array('i',[1,2,3,4,5])

        self.front=-1

        self.rear=-1

        self.count=0

    def isEmpty(self):

        if self.count is 0:

            return True

        else:

            return False

    def isFull(self):

        if self.count==len(self.q):

            return True

        else:

            return False

 

    def Enqueue(self,val):

        if(self.isFull())is not True:

            if self.front is -1:

                self.front=0

                self.rear=0

            else:

                maxsize=len(self.q)

                self.rear=(self.rear+1)%maxsize

            self.q[self.rear]=val

            self.count+=1

        else:

            print("queue full")

    def length(self):

        return self.count

    def Dequeue(self):

        if self.isEmpty():

            return None

        else:

            if self.count is 1:

                val=self.q[self.front]

                self.front=-1

                self.rear=-1

            else:

                val=self.q[self.front]

                maxsize=len(self.q)

                self.front=(self.front+1)%maxsize

            self.count-=1

            return val

    def display(self):

        if self.isEmpty():

            print("Queue Empty")

        else:

            i=self.front

            while(i!=self.rear):

                print(self.q[i])

                maxsize=len(self.q)

                i=(i+1)%maxsize

            print(self.q[i])

 

 

queue=QueuePL(5)

choice=int(input("1.Enqueue \t2.Dequeue \t3.Length of Queue \t4.Check queue empty or not \t5.Display Queue"))

while choice is not 6:

    if choice is 1:

        val=int(input("enter element to be inserted in the queue"))

        queue.Enqueue(val)

    if choice is 2:

        val=queue.Dequeue()

        if val is None:

            print("Queue is Empty.Cannot delete from empty queue")

        else:

            print("The removed element is",val)

    if choice is 3:

        l=queue.length()

        print("Number of elements in the queue are",l)

    if choice is 4:

        if(queue.isEmpty()is True):

            print("Queue is Empty")

        else:

            print("Queue is not empty")

    if choice is 5:

        queue.display()

    choice=int(input("Enter your choice"))    

output:

A circular queue. (The beginning items of the queue can be reused).

Post a Comment

0 Comments