Insert, Delete and Display in Circular Queue in DSA Python

Program 1

# implementation of circular queue
import os 
MAXSIZE=10
cqueue=[]
front=-1
rear=-1

def cqinsert():
    global rear
    global front
    global MAXSIZE
    if(((rear+1)%MAXSIZE)==front):
        print("Queue is overflow")
    else:
        n=int(input("Enter an element for insert"))
        if(front==-1 and rear==-1):
            front=rear=0
        else:
            rear=(rear+1)%MAXSIZE    
        cqueue.insert(rear,n) 

def cqdelete():
    global rear
    global front
    global MAXSIZE
    if(rear==-1 and front==-1):
        print("Queue is empty")
    else:
        n=cqueue[front]
        print("Deleted element is: ",n)
        if(front==rear):
            front=rear=-1
        front=(front+1)%MAXSIZE        
    
    
def cqdisplay():
    global rear
    global front
    global MAXSIZE
    if(rear==-1 and front==-1):
        print("Queue is empty")
    else:
        print("Elements of Queue")
        i=front
        while(i!=rear):
            print(cqueue[i],end=" ") 
            i=(i+1)%MAXSIZE   

        print(cqueue[i],end=" ") 

os.system('cls')
while(1):
    print("\n----------------------Circular Queue----------------------")
    print("1.Insert")
    print("2.Delete")
    print("3.Display")
    print("4.Exit")
    print("\n------------------------------------------------------------")
    choice=int(input("Enter your choice"))
    if(choice==1):
        cqinsert()
    elif(choice==2):
        cqdelete()
    elif(choice==3):
        cqdisplay()
    else:
        break

 

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *