ppathak35

Queue with linked list

Jun 9th, 2022 (edited)
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.38 KB | None | 0 0
  1. // Online C compiler to run C program online
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. struct Node {
  6.     int data;
  7.     struct Node *next;
  8. } *start = NULL;
  9.  
  10. int maxsize = 5;
  11. int length = 0;
  12.  
  13. void Queue(int d) {
  14.     struct Node *temp;
  15.     temp = (struct Node *)malloc(sizeof(struct Node));
  16.     temp->data = d;
  17.     temp->next = NULL;
  18.     if (start == NULL) {
  19.         start = temp;
  20.         printf("Queue Created successfully");
  21.         length = 1;
  22.     }
  23. }
  24.  
  25. int size() {
  26.     return length;
  27. }
  28.  
  29. int isEmpty() {
  30.     return size() == 0;
  31. }
  32.  
  33. void enqueue(int d) {
  34.     struct Node *temp;
  35.     temp = (struct Node *)malloc(sizeof(struct Node));
  36.     temp->data = d;
  37.     temp->next = NULL;
  38.     if (size() < maxsize) {
  39.         temp->next = start;
  40.         start = temp;
  41.         length += 1;
  42.     }
  43.     else {
  44.         printf("Queue overflow");
  45.     }
  46. }
  47.  
  48. void dequeue() {
  49.     struct Node *temp, *ptr;
  50.     if (isEmpty()) {
  51.         printf("Queue underflow");
  52.     }
  53.     else {
  54.         ptr = start;
  55.         while (ptr->next->next != NULL) {
  56.             ptr = ptr->next;
  57.         }
  58.         temp = ptr->next;
  59.         ptr->next = NULL;
  60.         printf("Element deleted %d", temp->data);
  61.         free(temp);
  62.         length -= 1;
  63.     }
  64. }
  65.  
  66. int main(){
  67.     int choice, data=0;
  68.     while (choice!=5) {
  69.         printf("\n1. Create a Queue");
  70.         printf("\n2. Enqueue an element");
  71.         printf("\n3. Dequeue an element");
  72.         printf("\n4. Get Queue Size");
  73.         printf("\n5. Exit");
  74.        
  75.         printf("\n Enter a choice");
  76.         scanf("%d", &choice);
  77.        
  78.         switch(choice) {
  79.             case 1:
  80.                     printf("Enter a start element : ");
  81.                     scanf("%d", &data);
  82.                     Queue(data);
  83.                     break;
  84.             case 2:
  85.                     if (start == NULL) {
  86.                         printf("Create a Queue First");
  87.                     }
  88.                     else{
  89.                         printf("Enter a start element : ");
  90.                         scanf("%d", &data);
  91.                         enqueue(data);
  92.                     }
  93.                     break;
  94.             case 3:
  95.                     dequeue();
  96.                     break;
  97.             case 4:
  98.                     size();
  99.                     break;
  100.             case 5: break;
  101.             default: break;
  102.         }
  103.     }
  104. }
Add Comment
Please, Sign In to add comment