C Program to implement basic operations of Linear Queue using arrays and pointers
Program
#include<stdio.h>
#include<stdlib.h>
#define QUEUE_SIZE 5
int qfull(int);
int qempty(int, int);
void enqueue(int, int*, int*, int*);
void dequeue(int*, int*, int*);
void display(int*, int, int);
void main()
{
int choice, item;
int f, r, q[QUEUE_SIZE];
f = 0;
r = -1;
printf("Linear Queue\n");
for(;;)
{
printf("Please enter a choice\n");
printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Choice:\t");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the element to be inserted:\t");
scanf("%d", &item);
enqueue(item, q, &f, &r);
break;
case 2:
dequeue(q, &f, &r);
break;
case 3:
display(q, f, r);
break;
default:
exit(0);
}
}
}
int qfull(int r)
{
return (r == QUEUE_SIZE -1) ? 1 : 0;
}
void enqueue(int item, int q[], int *f, int *r)
{
if(qfull(*r))
{
printf("Queue is full\nInsertion not possible\n");
return;
}
(*r)++;
q[*r] = item;
}
int qempty(int f, int r)
{
return (f > r) ? 1 : 0;
}
void dequeue(int q[], int *f, int *r)
{
int item_deleted;
if(qempty(*f, *r))
{
printf("Queue underflow\n");
return;
}
item_deleted = q[*f];
(*f)++;
printf("The element deleted is:\t%d\n", item_deleted);
if(*f > *r)
{
*f = 0;
*r = -1;
}
}
void display(int q[], int f, int r)
{
int i;
if(qempty(f, r))
{
printf("Queue is empty\nNo elements to display\n");
return;
}
printf("Displaying the contents of the queue\n");
for(i = f; i <= r; i++)
printf("%d\n", q[i]);
}
Output
Linear Queue
Please enter a choice
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 3
Queue is empty
No elements to display
Please enter a choice
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 2
Queue underflow
Please enter a choice
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Enter the element to be inserted: 10
Please enter a choice
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Enter the element to be inserted: 20
Please enter a choice
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Enter the element to be inserted: 30
Please enter a choice
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Enter the element to be inserted: 40
Please enter a choice
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Enter the element to be inserted: 50
Please enter a choice
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 3
Displaying the contents of the queue
10
20
30
40
50
Please enter a choice
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Enter the element to be inserted: 60
Queue is full
Insertion not possible
Please enter a choice
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 4