Advertisement
kebria

Implementing Queue in C. Using Linked List

Jun 23rd, 2021
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1. /* Implementing Queue in C. Using Linked List */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. struct Node
  7. {
  8.     int data;
  9.     struct Node* next;
  10. }*front = NULL, *rear = NULL;
  11.  
  12. void insert(int value)//enqueue
  13. {
  14.     struct Node* newNode;
  15.     newNode = (struct Node*)malloc(sizeof(struct Node));
  16.     newNode->data = value;
  17.     newNode->next = NULL;
  18.     if (front == NULL)
  19.         front = rear = newNode;
  20.     else
  21.     {
  22.         rear->next = newNode;
  23.         rear = newNode;
  24.     }
  25.     printf("\nInsertion is Successful!!!\n");
  26. }
  27.  
  28. void delete()//dequeue
  29. {
  30.     if (front == NULL)
  31.         printf("\nQueue is Empty!!!\n");
  32.     else
  33.     {
  34.         struct Node* temp = front;
  35.         front = front->next;
  36.         printf("\nDeleted element: %d\n", temp->data);
  37.         free(temp);
  38.     }
  39. }
  40.  
  41. void print()
  42. {
  43.     if (front == NULL)
  44.         printf("\nQueue is Empty!!!\n");
  45.     else
  46.     {
  47.         struct Node* temp = front;
  48.         while (temp->next != NULL) {
  49.             printf("%d--->", temp->data);
  50.             temp = temp->next;
  51.         }
  52.         printf("%d--->NULL\n", temp->data);
  53.     }
  54. }
  55.  
  56. int main()
  57. {
  58.     int choice, value;
  59.     printf("\n:: Queue Implementation using Linked List ::\n");
  60.     while (1)
  61.     {
  62.         printf("\n****** MENU ******\n");
  63.         printf("1. Insert\n2. Delete\n3. Print\n4. Exit\n");
  64.         printf("Enter your choice: ");
  65.         scanf("%d", &choice);
  66.         switch (choice)
  67.         {
  68.         case 1: printf("Enter the value to be insert: ");
  69.             scanf("%d", &value);
  70.             insert(value);
  71.             break;
  72.         case 2: delete(); break;
  73.         case 3: print(); break;
  74.         case 4: exit(0);
  75.         default: printf("\nWrong selection!!! Please try again!!!\n");
  76.         }
  77.     }
  78. }
  79.  
  80.  
  81. /*
  82. Output:
  83.  
  84. ::Queue Implementation using Linked List ::
  85.  
  86. ****** MENU******
  87. 1. Insert
  88. 2. Delete
  89. 3. Print
  90. 4. Exit
  91. Enter your choice : 1
  92. Enter the value to be insert : 10
  93.  
  94. Insertion is Successful!!!
  95.  
  96. ******MENU * *****
  97. 1. Insert
  98. 2. Delete
  99. 3. Print
  100. 4. Exit
  101. Enter your choice : 1
  102. Enter the value to be insert : 20
  103.  
  104. Insertion is Successful!!!
  105.  
  106. ******MENU * *****
  107. 1. Insert
  108. 2. Delete
  109. 3. Print
  110. 4. Exit
  111. Enter your choice : 2
  112.  
  113. Deleted element : 10
  114.  
  115. * *****MENU * *****
  116. 1. Insert
  117. 2. Delete
  118. 3. Print
  119. 4. Exit
  120. Enter your choice : 3
  121. 20--->NULL
  122.  
  123. * *****MENU * *****
  124. 1. Insert
  125. 2. Delete
  126. 3. Print
  127. 4. Exit
  128. Enter your choice : 4
  129. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement