Practical No.
22: * Write a 'C' Program to perform INSERT and DELETE operations
on Linear Queue using a Linked List.
CODE :
#include <stdio.h> if (isEmpty(q)) {
#include <stdlib.h> printf("Queue Underflow! Cannot
delete from an empty queue.\n");
struct QueueNode {
return -1;
int data;
} else {
struct QueueNode* next;
struct QueueNode* temp = q-
}; >front;
struct Queue { int deletedValue = temp->data;
struct QueueNode* front; q->front = q->front->next;
struct QueueNode* rear; if (q->front == NULL) {
}; q->rear = NULL;
void initQueue(struct Queue* q) { }
q->front = q->rear = NULL; free(temp);
} return deletedValue;
int isEmpty(struct Queue* q) { }
return q->front == NULL; }
} void display(struct Queue* q) {
void insert(struct Queue* q, int value) { if (isEmpty(q)) {
struct QueueNode* newNode = (struct printf("Queue is empty.\n");
QueueNode*)malloc(sizeof(struct
QueueNode)); return;
newNode->data = value; }
newNode->next = NULL; struct QueueNode* temp = q->front;
if (isEmpty(q)) { printf("Queue elements: ");
q->front = q->rear = newNode; while (temp != NULL) {
} else { printf("%d ", temp->data);
q->rear->next = newNode; temp = temp->next;
q->rear = newNode; }
} printf("\n");
printf("%d inserted into the queue.\n", }
value);
int main() {
}
struct Queue q;
int delete(struct Queue* q) {
int choice, value;
initQueue(&q); default:
while (1) { printf("Invalid choice! Please
enter a valid choice (1-4).\n");
printf("\nQueue Operations Menu:\
n"); }
printf("1. Insert (Enqueue)\n"); }
printf("2. Delete (Dequeue)\n");
printf("3. Display Queue\n"); return 0;
printf("4. Exit\n"); }
printf("Enter your choice (1-4): "); OUTPUT :
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to
insert: ");
scanf("%d", &value);
insert(&q, value);
break;
case 2:
value = delete(&q);
if (value != -1) {
printf("Deleted value: %d\
n", value);
break;
case 3:
display(&q);
break;
case 4:
printf("Exiting program...\n");
return 0;