C Program for Circular Queue Using Array with Queue Data Structure
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
//declaration of Circular Queue Array Structure
typedef struct CircularQueueArray{
int arr[MAX];
int rear,front;
}CQueue;
//function declarations
void insert(CQueue*);
void delete(CQueue*);
void display(CQueue*);
void initialize(CQueue*);
//starting of the main function
int main(){
CQueue* q = (CQueue*)malloc(sizeof(CQueue)); //creating a pointer variable of the circular 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;
}
//Initialize function for eliminating the garbage values from the queue array
void initialize(CQueue* 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(CQueue* q){
if((q->front==0&&q->rear==MAX-1)||(q->rear==q->front-1)){
printf("Queue is overflowed,can't insert.");
}
else{
if(q->front==-1&&q->rear==-1)
q->front=q->rear=0;
else if(q->rear==MAX-1&&q->front>0)
q->rear=0; //rear moves circular way
else
q->rear++;
printf("Enter data:\n");
scanf("%d",&q->arr[q->rear]);
}
}
//Delete [through front] data from queue
void delete(CQueue* q){
if(q->front==-1){
printf("Queue underflow,nothing to delete.");
}
else{
printf("Deleted:%d",q->arr[q->front]);
if(q->front==MAX-1)
q->front=0; //front moves circular way
else if(q->front==q->rear)
q->front=q->rear=-1;
else
q->front++;
}
}
//Display the values of the queue
void display(CQueue* q){
if(q->front==-1)
printf("Queue is empty,no items to be displayed.");
else{
int i;
printf("Queue elements are:\n");
if(q->front<=q->rear){
for(i=q->front;i<=q->rear;i++){
printf("%d\t",q->arr[i]);
}
}
else{
for(i=0;i<=q->rear;i++){
printf("%d\t",q->arr[i]);
for(i=q->front;i<MAX;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:
1
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
1
Enter data:
2
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
1
Enter data:
3
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:
5
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
1
Queue is overflowed, can't insert.
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
3
Queue elements are:
1 2 3 4 5
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
2
Deleted:1
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
2
Deleted:2
What do you want to do with the Queue?
0:Initialize
1:Insert
2:Delete
3:Display
4:Exit
4
Try here:-
0 Comments