Queue using Array

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(REAR=SIZE-1)THEN
STEP 3: PRINT("QUEUE IS FULL")
STEP 4: ELSE
STEP 5: IF(FRONT=-1)THEN
STEP 6: FRONT=0
STEP 7: ENDIF
STEP 8: REAR=REAR+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=-1,front=-1;
 int top=-1,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(rear==size-1)
    {printf("Queue is FUll");}
    else {
         printf("Enter the item you want to insert?\n");
        scanf("%d",&item);
        if(front==-1)
        {
            front=0;
        }
    rear++;
    a[rear]=item;
    }
 }
 void dequeue()
 {
    if(front==-1)
    {
        printf("Queue is Empty");
    }
    else{
        item=a[front];
        front++;
        if(front>rear){
        front=-1;rear=-1;}
    }
 }
 void display()
 {
    if(rear==-1)
    {
        printf("Queue underflows");
    }
    for(int i=front;i<=rear;i++)
    {
        printf("%d ",a[i]);
    }
    printf("\n");
 }

Output

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

Leave a Comment

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