Experiment no9
Experiment no9
9
Aim: Write a program to understand the concept of queue using linked
list and its operations.
#include <stdio.h>
#include <stdlib.h>
#define MAX_CAPACITY 5
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node* front;
struct Node* rear;
int size;
};
do {
printf("\nQueue operation menu:\n");
printf("1. Enqueue (add item)\n");
printf("2. Dequeue (remove item)\n");
printf("3. Show Queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the item to enqueue: ");
scanf("%d", &item);
enqueue(&q, item);
break;
case 2:
item = dequeue(&q);
if (item != -1) {
printf("Dequeued %d\n", item);
}
break;
case 3:
display(&q);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please reenter the choice between 1 to 4.\n");
}
} while (choice != 4);
return 0;
}
if (q->rear == NULL) {
q->front = q->rear = newNode;
} else {
q->rear->next = newNode;
q->rear = newNode;
}
q->size++;
printf("Enqueued %d\n", item);
}
if (q->front == NULL) {
q->rear = NULL;
}
free(temp);
q->size--;
return item;
}