Insertion And Deletion Operation Over Multiple Queue In C language

A simple C language program to implement multiple queues in a single dimension array.
This is a revised version of Insertion And Deletion Operation Over Multiple Queue | multiple queue in data structure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<stdio.h>
#include<conio.h>
# define max 20
 
int insq (int queue[max], int qno, int rear[], int limit[], int *data) {
	if (rear[qno] == limit[qno])
		return(-1);
	else {
		rear[qno]++; //... rear[qno] = rear[qno] + 1;
		queue[ rear[qno] ] = *data;
		return(1);
	} // else
} // insq
 
int delq (int queue[max], int qno, int front[], int rear[], int *data) {
	if( front[qno] == rear[qno] )
		return(-1);
	else {
		front[qno]++; //... front[qno] = front[qno] + 1;
		*data = queue[ front[qno] ];
		return(1);
	} // else
} // delq
 
int getQueueNumber(int n) {
	int qNo=0;
	Inva:
	printf("\n Enter a Logical Queue Number (1 to %d) : ", n);
	scanf("%d", &qNo);
	if (qNo<1 || qNo >n) {
		printf(" Invalid Queue Number. Please try again.\n");
		goto Inva;
	}
	return qNo;
}
 
void main() {
	int queue[max],  data;
	int bott[10], limit[10], f[10], r[10];
	int i, n, qno, size, option, reply;
 
	printf("\n C Language program to implement the Multiple Queues \n");
	printf("\n How Many Queues ? : ");
	scanf("%d", &n);
	size = max / n; //... Get Max. size for each Queue
 
	//... Initialize bottom for each Queue
 
	bott[0] = -1; //... Bottom of first Queue is -1
	for(i = 1; i < n; i++)
		bott[i] = bott[i-1] + size;
 
	//... Initialize Limit of each Queue
 
	for(i = 0; i < n; i++) //... Limit of i'th Queue is equal to bottom of i'th Queue + Size
		limit[i] = bott[i] + size;
 
	//... Initialize Front & Rear of each Queue
	//... Initial value of Front & Rear of each Queue is same as its Bottom Value
 
	for(i = 0; i < n; i++)
		f[i] = r[i] = bott[i];
 
	//... Process the Queues
 
	do {
		printf("\n\n C Language program to implement the Multiple Queues \n");
		printf("\n 1. Insert in a Queue");
		printf("\n 2. Delete from a Queue");
		printf("\n 3. Print from a Queue");
		printf("\n 3. Exit \n");
		printf("\n Select proper option ( 1 / 2 / 3 / 4) : ");
		scanf("%d", &option);
		switch(option) {
			case 1 : //... Insert
				qno	= getQueueNumber(n);
				printf("\n Enter Data : ");
				scanf("%d", &data);
				reply = insq(queue, qno-1, r, limit, &data);
				if( reply == -1)
					printf("\n Queue %d is Full \n", qno);
				else
					printf("\n %d is inserted in a Queue No. %d \n", data, qno);
				break;
			case 2 : //... Delete
				qno	= getQueueNumber(n);
				reply = delq(queue, qno-1, f, r, &data);
				if( reply == -1)
					printf("\n Queue %d is Empty \n", qno);
				else
					printf("\n %d is deleted from Queue No. %d \n", data, qno);
				break;
			case 3:
				qno	= getQueueNumber(n);
				printf("\n Elements of Queue %d are as : ", qno);
				if (f[qno-1]==r[qno-1]) {
					printf("\n Queue is empty");
					break;
				}
				for (i=f[qno-1]+1; i<=r[qno-1]; i++)
					printf("%d\t", queue[i]);
				printf("\n");
				break;
			case 4 :
				break;
			default:
				printf("\n Invalid input. Please try again.");
		} // switch
	}while(option!=4);
} // main
Follow me

Abhishek Gupta

I am Abhishek from Lucknow (India). I have been working as a software engineer from last 6+ years, and its my passion to learn new things and implement them as a practice.
Follow me

Latest posts by Abhishek Gupta (see all)

One Reply to “Insertion And Deletion Operation Over Multiple Queue In C language”

Leave a Reply