Program to perform insert and delete operations on linear queue using an array
/*_________________Name: Vedant Walanj_______________*/
/*_________________Roll no:960______________________*/
/*_________________Branch: SYCO____________________*/
/*_________________Subject: DSU__________________*/
/*_________________Practical no:21____________________*/
/*__program to perform insert and delete operations on linear queue using an array__*/
/*_________________Date:1/10/24____________________*/
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef struct {
int data[MAX];
int front;
int rear;
} Queue;
void initialize(Queue* q) {
q->front = -1;
q->rear = -1;
}
int isEmpty(Queue* q) {
return q->front == -1;
}
int isFull(Queue* q) {
return q->rear == MAX - 1;
}
void enqueue(Queue* q, int value) {
if (isFull(q)) {
printf("Queue overflow! Cannot insert %d\n", value);
return;
}
if (isEmpty(q)) {
q->front = 0;
}
q->data[++(q->rear)] = value;
printf("Inserted %d into the queue.\n", value);
}
int dequeue(Queue* q) {
if (isEmpty(q)) {
printf("Queue underflow! Cannot delete.\n");
return -1;
}
int value = q->data[q->front];
if (q->front == q->rear) {
q->front = q->rear = -1;
} else {
q->front++;
}
return value;
}
void display(Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty.\n");
return;
}
printf("Queue elements: ");
for (int i = q->front; i <= q->rear; i++) {
printf("%d ", q->data[i]);
}
printf("\n");
}
int main() {
Queue queue;
initialize(&queue);
int choice, value;
while (1) {
printf("\nQueue Operations:\n");
printf("1. Insert (Enqueue)\n");
printf("2. Delete (Dequeue)\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to insert: ");
scanf("%d", &value);
enqueue(&queue, value);
break;
case 2:
value = dequeue(&queue);
if (value != -1) {
printf("Deleted %d from the queue.\n", value);
}
break;
case 3:
display(&queue);
break;
case 4:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
/*__________________________________________End of the Program_______________________________________*/
/*_________________________________________Output of the Program_____________________________________*/