0% found this document useful (0 votes)
11 views2 pages

Queue C

This C program implements a queue using a linked list with options to insert, delete, view, or exit. It includes a dummy head node for easier management of the list. The program handles memory allocation and provides user feedback for each operation.

Uploaded by

akshaya31032007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Queue C

This C program implements a queue using a linked list with options to insert, delete, view, or exit. It includes a dummy head node for easier management of the list. The program handles memory allocation and provides user feedback for each operation.

Uploaded by

akshaya31032007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <stdio.

h>
#include <stdlib.h> // For malloc, exit()

struct node {
int label;
struct node *next;
};

int main() {
int ch = 0;
struct node *h, *temp, *head;

// Create a dummy head node


head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;

while (1) {
printf("\nQueue using Linked List\n");
printf("1 -> Insert\n");
printf("2 -> Delete\n");
printf("3 -> View\n");
printf("4 -> Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);

switch (ch) {
case 1: // Insert at rear
temp = (struct node *)malloc(sizeof(struct node));
if (!temp) {
printf("Memory allocation failed.\n");
break;
}
printf("Enter label for new node: ");
scanf("%d", &temp->label);
temp->next = NULL;

h = head;
while (h->next != NULL)
h = h->next;

h->next = temp;
printf("Node inserted.\n");
break;
case 2: // Delete from front
if (head->next == NULL) {
printf("Queue Underflow (Empty Queue)\n");
break;
}
h = head->next;
head->next = h->next;
printf("Node %d deleted\n", h->label);
free(h);
break;

case 3: // View queue


printf("\nHEAD -> ");
h = head->next;
while (h != NULL) {
printf("%d -> ", h->label);
h = h->next;
}
printf("NULL\n");
break;

case 4: // Exit
printf("Exiting...\n");
exit(0);

default:
printf("Invalid choice. Try again.\n");
}
}

return 0;
}

You might also like