C Program for Normal Queue using Array with Queue Structure
A regular queue is defined as a linear data structure that is open at both ends where data gets inserted from the rear, data gets deleted from the front, and operations are performed in the First In First Out (FIFO) order.
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
//declaration of Normal Queue Array Structure
typedef struct QueueArray{
int arr[MAX];
int rear,front;
}Queue;
int arr[MAX];
int rear,front;
}Queue;
//function declarations
void insert(Queue*);
void delete(Queue*);
void display(Queue*);
void initialize(Queue*);
void delete(Queue*);
void display(Queue*);
void initialize(Queue*);
//starting of main function
int main(){
Queue* q=(Queue*)malloc(sizeof(Queue)); //creating a pointer variable of the Queue structure
int a;
do{
printf("\n What do you want to do with the Queue?\n");
printf("\n 0:Initialize \n 1:Insert \n 2:Delete \n 3:Display \n 4:Exit \n");
scanf("%d",&a);
switch(a){
case 0: initialize(q);break;
case 1: insert(q);break;
case 2: delete(q);break;
case 3: display(q);break;
Queue* q=(Queue*)malloc(sizeof(Queue)); //creating a pointer variable of the Queue structure
int a;
do{
printf("\n What do you want to do with the Queue?\n");
printf("\n 0:Initialize \n 1:Insert \n 2:Delete \n 3:Display \n 4:Exit \n");
scanf("%d",&a);
switch(a){
case 0: initialize(q);break;
case 1: insert(q);break;
case 2: delete(q);break;
case 3: display(q);break;
case 4: exit(0);
default: printf("Wrong Input!Try again:\n");
}
}while(1); //infinite loop for menu
return 0;
}
default: printf("Wrong Input!Try again:\n");
}
}while(1); //infinite loop for menu
return 0;
}
//Initialize function for eliminating the garbage values from the queue array
void initialize(Queue* q){
int i;
for(i=0;i<MAX;i++){
q->arr[i]=0;
}
q->rear=q->front=-1;
}
void initialize(Queue* q){
int i;
for(i=0;i<MAX;i++){
q->arr[i]=0;
}
q->rear=q->front=-1;
}
//Insert [through rear] integer data into queue
void insert(Queue* q){
if(q->rear==MAX-1){
printf("Queue is overflowed,can't insert.");
}
else{
if(q->front==-1&&q->rear==-1){
q->front=q->rear=0;
}
else{
q->rear++;
}
printf("Enter data:\n");
scanf("%d",&q->arr[q->rear]);
}
}
void insert(Queue* q){
if(q->rear==MAX-1){
printf("Queue is overflowed,can't insert.");
}
else{
if(q->front==-1&&q->rear==-1){
q->front=q->rear=0;
}
else{
q->rear++;
}
printf("Enter data:\n");
scanf("%d",&q->arr[q->rear]);
}
}
//Delete [through front] data from queue
void delete(Queue* q){
if(q->front==-1){
printf("Queue underflow,nothing to delete.");
}
else{
printf("Deleted:%d",q->arr[q->front]);
q->front++;
if(q->front>q->rear)
q->front=q->rear=-1;
}
}
void delete(Queue* q){
if(q->front==-1){
printf("Queue underflow,nothing to delete.");
}
else{
printf("Deleted:%d",q->arr[q->front]);
q->front++;
if(q->front>q->rear)
q->front=q->rear=-1;
}
}
//Display the values of the queue
void display(Queue* q){
if(q->front==-1&&q->rear==-1)
printf("Queue is empty,no items to be displayed.");
else{
int i;
printf("Queue elements are:\n");
for(i=q->front;i<=q->rear;i++){
printf("%d\t",q->arr[i]);
}
}
}
void display(Queue* q){
if(q->front==-1&&q->rear==-1)
printf("Queue is empty,no items to be displayed.");
else{
int i;
printf("Queue elements are:\n");
for(i=q->front;i<=q->rear;i++){
printf("%d\t",q->arr[i]);
}
}
}
Output:-
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
0
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
1
Enter data:
4
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
1
Enter data:
6
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
1
Enter data:
8
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
3
Queue elements are:
4 6 8
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
2
Deleted:4
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
2
Deleted:6
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
3
Queue elements are:
8
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
2
Deleted:8
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
3
Queue is empty, no items to be displayed.
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
4
Try:-
0 Comments