Title: Linear Queue using Array
1. Aim:
To write a C program to perform INSERT and DELETE operations on a Linear Queue using an
Array.
2. Theory (in short):
A Queue is a linear data structure that follows the FIFO (First In First Out) principle. In a Linear
Queue, insertion (enqueue) is performed at the rear end and deletion (dequeue) is performed from
the front end. It is implemented using an array with two pointers — front and rear.
3. Program:
#include <stdio.h>
#define SIZE 5
int queue[SIZE];
int front = -1, rear = -1;
void insert() {
int item;
if (rear == SIZE - 1) {
printf("\nQueue Overflow! Cannot insert element.\n");
} else {
printf("Enter the element to insert: ");
scanf("%d", &item);
if (front == -1)
front = 0;
rear++;
queue[rear] = item;
printf("Inserted %d successfully.\n", item);
}
}
void delete() {
if (front == -1 || front > rear) {
printf("\nQueue Underflow! Cannot delete element.\n");
} else {
printf("Deleted element: %d\n", queue[front]);
front++;
if (front > rear) {
front = rear = -1;
}
}
}
void display() {
if (front == -1) {
printf("\nQueue is Empty!\n");
} else {
printf("\nQueue elements are:\n");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}
int main() {
int choice;
printf("---- Linear Queue Operations Using Array ----\n");
while (1) {
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
printf("Exiting program...\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
}
4. Sample Input & Output Screen:
---- Linear Queue Operations Using Array ----
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the element to insert: 10
Inserted 10 successfully.
Enter your choice: 1
Enter the element to insert: 20
Inserted 20 successfully.
Enter your choice: 3
Queue elements are:
10 20
Enter your choice: 2
Deleted element: 10
Enter your choice: 3
Queue elements are:
20
Enter your choice: 4
Exiting program...