[Solved] Queue Array Implementation using element count with C

Queue Array Implementation using element count: Consider implementing a fixed size queue of maximum size 5 using an array.

Create a structure

struct queue {

    int contents[5];

    int front;

    int count;

};

Note that the array contents holds the contents of the queue and the integer front stores the index of the front element in the queue.
count stores the count of elements in the queue.

Write a program to implement enQueue and deQueue operation on queue and to display the contents of the queue.

In the initQueue function intialize the value of front and count to 0.

Print the message “Queue is full” in the enQueue function when an attempt is made to insert a data into a full queue.

Print the message “Queue is empty” in the deQueue function and return the value -1000 when an attempt is made to delete data from an empty queue.

Function specifications:

void initQueue(struct queue * q)
void enQueue(struct queue * q, int element)
int deQueue(struct queue * p)
void display(struct queue q)

Input and Output Format:

Refer sample input and output for formatting specifications.

Note that the statement “The contents of the queue are” is in the main function. In the display function, if the queue is empty, print “ {}”.

[All text in bold corresponds to input and the rest corresponds to output]



Sample Input and Output:

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

3

The contents of the queue are {}

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

1

Enter the element to be inserted/entered

10

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

1

Enter the element to be inserted/entered

20

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

1

Enter the element to be inserted/entered

30

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

1

Enter the element to be inserted/entered

40

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

1

Enter the element to be inserted/entered

50

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

1

Enter the element to be inserted/entered

60

Queue is full

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

2

The deleted element is 10

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

2

The deleted element is 20

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

1

Enter the element to be inserted/entered

60

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

3

The contents of the queue are 30 40 50 60

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

2

The deleted element is 30

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

2

The deleted element is 40

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

2

The deleted element is 50

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

3

The contents of the queue are 60

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

2

The deleted element is 60

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

2

Queue is empty

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice

3

The contents of the queue are {}

Choice 1 : Enter element into Queue

Choice 2 : Delete element from Queue

Choice 3 : Display

Any other choice : Exit

Enter your choice
4

Function Definitions:

void  initQueue (struct queue *q)

void enQueue (struct queue *q, int element)

int deQueue (struct queue *q)

void display (struct queue q)

Solution

#include<stdio.h>

struct queue {
    int contents[5];
    int front;
    int count;
};
void  initQueue (struct queue *q){
    q->front = 0;
    q->count = 0;
}
void enQueue (struct queue *q, int element){
    if(q->count == 5){
        printf("Queue is full\n");
    }else{
        q->contents[(q->front + q->count) % 5] = element;
        q->count++; 
    }
}

int deQueue (struct queue *q){
    if(q->count == 0){
        printf("Queue is empty\n");
        return -1000;
    }
    int temp = q->contents[q->front];
    q->front = (q->front + 1) % 5;
    q->count--;
    return temp;
        
}
void display (struct queue q){
    if(q.count == 0){
        printf(" {}\n");
        return;
    }
    int i;
    for(i = 0; i < q.count; i++)
        printf(" %d",q.contents[(q.front + i) % 5]);
    printf("\n");    
}

int main(){
    int n, choice;
    struct queue q;
    initQueue(&q);
    do{
        printf("Choice 1 : Enter element into Queue\nChoice 2 : Delete element from Queue\nChoice 3 : Display\nAny other choice : Exit\nEnter your choice\n");
        scanf("%d",&choice);
        
        switch(choice){
            case 1:
                printf("Enter the element to be inserted/entered\n");
                scanf("%d",&n);
                enQueue(&q,n);
            break;
            case 2:
                n = deQueue(&q);
                if(n != -1000)
                    printf("The deleted element is %d\n",n);
            break;
            case 3:
                printf("The contents of the queue are");
                display(q);
            break;
        }
    }while(choice > 0 && choice < 4);
    return 0;
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

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