//Write a program to implement Queue using Linked List.
#include<stdio.h>
#include<stdlib.h>
// Define the structure for a node in the linked list
typedef struct node
{
int data;
struct node *link;
} NODE;
// Declare global pointers for the front, rear, current node, and display node
NODE *front = NULL, *rear = NULL, *s, *ptr, *display;
// Main function
int main()
{
int no;
char c;
// Display program information
printf("\n\tPROGRAM TO INSERT, DELETE AND DISPLAY ELEMENTS TO QUEUE USING LINKEDLIST");
printf("\t\t..............................................................\n\n");
// Menu-driven loop
do
{
// Display menu options
printf("\t\t\t\tMENU");
printf("\n\t\t1. INSERT\n\t\t2. DELETE\n\t\t3. DISPLAY\n\t\t4. EXIT\n\t\tEnter your choice: ");
scanf("%d", &no);
// Switch based on the user's choice
switch (no)
{
case 1:
// Insert operation
ptr = (NODE*)malloc(sizeof(NODE));
printf("\t\tEnter the element: ");
scanf("%d", &ptr->data);
ptr->link = NULL;
// Check if the queue is empty
if (rear == NULL)
{
front = ptr;
rear = ptr;
}
else
{
rear->link = ptr;
rear = ptr;
}
break;
case 2:
// Delete operation
if (front == NULL)
printf("\t\tQueue is empty\n");
else
{
s = front;
printf("\t\tDeleted Element is %d\n", front->data);
front = front->link;
free(s);
// If the queue becomes empty after deletion
if (front == NULL)
rear = NULL;
}
break;
case 3:
// Display operation
if (front == NULL)
printf("\t\tQueue is empty\n");
else
{
printf("\t\tQueue elements are");
display = front;
// Traverse the queue and display elements
while (display != NULL)
{
printf(" %d", display->data);
display = display->link;
}
printf("\n");
}
break;
case 4:
// Exit the program
exit(0);
}
// Prompt the user to continue
printf("\t\tDo you want to continue(y/n) ");
scanf(" %c", &c);
} while (c == 'y' || c == 'Y');
return 0;
}