Circular Queue

Algorithm

INPUT: FRONT IS A POINTER WHICH IS USED TO POINT TO THE ELEMENT FROM WHERE DELETION OF ELEMENT TAKES PLACE AND THE ELEMENT TO BE ENQUEUE AND REAR IS A POINTER WHICH POINTS TO THE ELEMENT FROM WHERE INSERTION TAKES PLACE
OUTPUT: STACK AFTER PUSH OPERATION
DATA STRUCTURE: STACK USING ARRAY

Algo Enqueue

STEP 1: START
STEP 2: IF(FRONT=(REAR%SIZE)+1)THEN
STEP 3: PRINT("QUEUE IS FULL")
STEP 4: ELSE
STEP 5: IF(FRONT=0)THEN
STEP 6: FRONT=1
STEP 7: ENDIF
STEP 8: REAR=REAR%SIZE+1
STEP 9: A[REAR]=ITEM
STEP 10: ENDIF
STEP 11: END

Algo Dequeue

STEP 1: START
STEP 2: IF(FRONT=-1)THEN
STEP 3: PRINT("QUEUE IS EMPTY")
STEP 4: ELSE
STEP 5: ITEM=A[FRONT]
STEP 6: FRONT=FRONT+1
STEP 7: IF(FRONT>REAR)THEN
STEP 8: FRONT=-1
STEP 9: REAR=-1
STEP 10: END

Code

#include <stdio.h>
 int size;
 int rear=0,front=0;
 int item,size=4;
 int a[4],n;
 void display();
 void enqueue();void dequeue();
 int main()
 {
    char ch;
    
    do{
    printf("enter 1 for enqueue, 2 for dequeue:\n");
    scanf("%d",&n);
    switch(n)
    {
        case 1:
        enqueue();
        printf("Queue after operation\n");
        display();
        break;
        case 2:
        dequeue();
           printf("Queue after operation\n");
        display();
        break;
        default:
        printf("Wrong choice\n");
    }
    printf("do you want to do again?");
    scanf("%c",&ch);
    }
    while((getchar())!='n');
    // display();
    printf("\n");
    return 0;
 }
 void enqueue(){
    if(front==(rear%size)+1)
    {printf("Queue is FUll");}
    else {
         printf("Enter the item you want to insert?\n");
        scanf("%d",&item);
         a[rear]=item;
        rear=(rear%size)+1;
   
    if(front==0){
        front =1;
    }
    }
    
}
 void dequeue()
 {
    if(front==0)
    {
        printf("Queue is Empty");
    }
    else{
        item=a[front];
        front=(front%size)+1;
        printf("%d",front);
         if(front==rear){
        front=0;rear=0;}
      
    } 
}
 void display()
 {
    // if(front ==-1)
    // {
    //     printf("Queue underflows");
    // }
    for(int i=0;i<size;i++)
    {
        printf("%d ",a[i]);
    }
    
    printf("\n");
 }

Output

user@computer$ : enter 1 for enqueue, 2 for dequeue:
2
Queue is EmptyQueue after operation
0 0 0 0
do you want to do again?y
enter 1 for enqueue, 2 for dequeue:
1
Enter the item you want to insert?
5
Queue after operation
5 0 0 0
do you want to do again?n

Leave a Comment

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