Implementation of Queue using Array in C Programming

In this source code example, we will write a code to implement the Queue data structure using an Array in C programming language.

In this C program, we will take input from the User or console and print the result to the output.

Implementation of Queue using Array in C Programming

#include<stdio.h>
#include<stdlib.h>
#define MAXQ 100
int front = 0, rear = -1;
int items[MAXQ];
int Isempty();
int Isfull();
void Insert(int);
int Delete();
void Display();
void main() {
	int x;
	char ch = '1';
	while (ch != '4') {
		printf("\n 1-INSERT");
		printf("\n 2-DELETE");
		printf("\n 3-DISPLAY");
		printf("\n 4-QUIT");
		printf("\n Enter your choice:");
		fflush(stdin);
		ch = getchar();
		switch (ch) {
		case '1':
			printf("\n Enter the element to be inserted:");
			scanf("%d", &x);
			Insert(x);
			break;
		case '2':
			x = Delete();
			printf("\n Delete element is %d\n:", x);
			break;
		case '3':
			Display();
			break;
		case '4':
			break;
		default:
			printf("\n Wrong choice!Try again:");
		}
	}
}
int Isempty() {
	if (rear < front)
		return 1;
	else
		return 0;
}
int Isfull() {
	if (rear == MAXQ - 1)
		return 1;
	else
		return 0;
}
void Insert(int x) {
	if (Isfull()) {
		printf("\n Queue full");
		return;
	}
	rear++;
	items[rear] = x;
}
int Delete() {
	int x;
	if (Isempty()) {
		printf("\n Queue is empty");
		exit(0);
	}
	x = items[front];
	front++;
	return x;
}
void Display() {
	int i;
	if (Isempty()) {
		printf("\n Queue is empty");
		return;
	}
	printf("\n Elements in the Queue are :\n");
	for (i = front; i <= rear; i++)
		printf("%d\n", items[i]);
}

Output:


 1-INSERT
 2-DELETE
 3-DISPLAY
 4-QUIT
 Enter your choice:1

 Enter the element to be inserted:10

 1-INSERT
 2-DELETE
 3-DISPLAY
 4-QUIT
 Enter your choice:1

 Enter the element to be inserted:20

 1-INSERT
 2-DELETE
 3-DISPLAY
 4-QUIT
 Enter your choice:1

 Enter the element to be inserted:30

 1-INSERT
 2-DELETE
 3-DISPLAY
 4-QUIT
 Enter your choice:1

 Enter the element to be inserted:40

 1-INSERT
 2-DELETE
 3-DISPLAY
 4-QUIT
 Enter your choice:1

 Enter the element to be inserted:50

 1-INSERT
 2-DELETE
 3-DISPLAY
 4-QUIT
 Enter your choice:3

 Elements in the Queue are :
10
20
30
40
50

 1-INSERT
 2-DELETE
 3-DISPLAY
 4-QUIT
 Enter your choice:2

 Delete element is 10
:
 1-INSERT
 2-DELETE
 3-DISPLAY
 4-QUIT
 Enter your choice:2

 Delete element is 20
:
 1-INSERT
 2-DELETE
 3-DISPLAY
 4-QUIT
 Enter your choice:2

 Delete element is 30
:
 1-INSERT
 2-DELETE
 3-DISPLAY
 4-QUIT
 Enter your choice:3

 Elements in the Queue are :
40
50

 1-INSERT
 2-DELETE
 3-DISPLAY
 4-QUIT
 Enter your choice:4

Related Data Structures in C


Comments