c code for queue using linked list.

  • cqueue using linked list
  • write a c code for queue using linked list.

    Write a menu-driven program to implement queue operations such as Enqueue, Dequeue, Peek, Display of elements, and Is Empty using a linked list.

    Code

    #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *next; }; struct Node *f = NULL; struct Node *r = NULL; int isFull() { struct Node *n = (struct Node *)malloc(sizeof(struct Node)); if (n == NULL) { return 1; } return 0; } int isEmpty() { if (f == NULL) { return 1; } return 0; } void linkedListTraversal(struct Node *ptr) { printf("Printing the elements of this linked list\n"); while (ptr != NULL) { printf("Element: %d\n", ptr->data); ptr = ptr->next; } } void enqueue(int val) { struct Node *n = (struct Node *)malloc(sizeof(struct Node)); if (isFull()) { printf("Queue is Full"); } else { n->data = val; n->next = NULL; if (f == NULL) { f = r = n; } else { r->next = n; r = n; } } } int dequeue() { int val = -1; struct Node *ptr = f; if (f == NULL) { printf("Queue is Empty\n"); } else { f = f->next; val = ptr->data; free(ptr); } return val; } int main() { int c, i; while (1) { options: printf("\n1.enqueue\n"); printf("2.dequeue\n"); printf("3.Display element at the front\n"); printf("4.Display element at the rear\n"); printf("5.Display all elements of the queue\n"); printf("6.isFull\n"); printf("7.isEmpty\n"); printf("8.Quit\n"); printf("\nEnter your choice : "); scanf("%d", &c); switch (c) { case 1: printf("\nInput the element for adding in queue : "); scanf("%d", &i); enqueue(i); break; case 2: i = dequeue(); printf("\nDeleted element is %d\n", i); break; case 3: printf("\nElement at the front is %d\n", f->data); break; case 4: printf("\nElement at the rear is %d\n", r->data); break; case 5: linkedListTraversal(f); break; case 6: if (isFull()) { printf("Queue is full\n"); } else { printf("Queue is not full\n"); } break; case 7: if (isEmpty()) { printf("Queue is empty\n"); } else { printf("Queue is not empty\n"); } break; case 8: exit(1); default: printf("\nWrong choice\n"); } } return 0; }