Advertisement
splash365

queue (using linked list)

Feb 9th, 2021
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. struct Node
  5. {
  6.    int data;
  7.    struct Node *next;
  8. };
  9.  
  10. struct Node *front = NULL,*rear = NULL;
  11.  
  12. void EnQueue(int);
  13. void DeQueue();
  14. void display();
  15. void Front();
  16. void QueueSize();
  17. int main()
  18. {
  19.     int choice, value;
  20.     printf("\n*** Queue Implementation using Linked List ***\n");
  21.     while(1)
  22.     {
  23.         printf("\n****** MENU ******\n");
  24.         printf("1. Insert in Queue\n");
  25.         printf("2. Delete From Queue\n");
  26.         printf("3. Display Queue\n");
  27.         printf("4. Front of the Queue\n");
  28.         printf("5. Size of Queue\n");
  29.         printf("6. Exit\n");
  30.  
  31.         printf("Enter your choice: ");
  32.         scanf("%d",&choice);
  33.  
  34.         switch(choice)
  35.         {
  36.          case 1:
  37.                 printf("Insert the value you want to enter: ");
  38.                 scanf("%d", &value);
  39.  
  40.                 EnQueue(value);
  41.                 break;
  42.          case 2:
  43.                 DeQueue();
  44.                 break;
  45.          case 3:
  46.                 display();
  47.                 break;
  48.          case 4:
  49.                 Front();
  50.                 break;
  51.          case 5:
  52.                 QueueSize();
  53.                 break;
  54.          case 6:
  55.                 exit(0);
  56.          default:
  57.                 printf("\nInvalid Selection!!..Select valid number please!!\n");
  58.         };
  59.     }
  60.    return 0;
  61. }
  62.  
  63. void EnQueue(int value)
  64. {
  65.     struct Node *newNode;
  66.     newNode = (struct Node*)malloc(sizeof(struct Node));
  67.     newNode -> data = value;
  68.     newNode -> next = NULL;
  69.  
  70.     if(front == NULL)
  71.         front = rear = newNode;
  72.     else
  73.     {
  74.         rear -> next = newNode;
  75.         rear = newNode;
  76.     }
  77.     printf("\n Data inserted in Queue!!!\n");
  78. }
  79.  
  80. void DeQueue()
  81. {
  82.     if(front == NULL)
  83.         printf("\n Queue is Empty!!!\n");
  84.     else
  85.     {
  86.         struct Node *temp = front;
  87.         front = front -> next;
  88.         printf("\n Deleted element is: %d\n", temp->data);
  89.         free(temp);
  90.     }
  91. }
  92.  
  93. void display()
  94. {
  95.     if(front == NULL)
  96.         printf("\n Queue is Empty!!!\n");
  97.     else
  98.     {
  99.         struct Node *temp = front;
  100.         while(temp->next != NULL)
  101.         {
  102.             printf("%d --> ",temp->data);
  103.             temp = temp -> next;
  104.         }
  105.         printf("%d \n",temp->data);
  106.    }
  107. }
  108.  
  109. void Front()
  110. {
  111.     if(front==NULL)
  112.         printf("\n Queue is Empty!!!\n");
  113.     else
  114.         printf("\n Data at front of the queue is %d \n",front->data);
  115. }
  116.  
  117. void QueueSize()
  118. {
  119.     if(front==NULL)
  120.         printf("\n Queue is Empty!!!\n");
  121.     else
  122.     {
  123.         int count=0;
  124.         struct Node *temp = front;
  125.         while(temp->next != NULL)
  126.         {
  127.             count++;
  128.             temp = temp -> next;
  129.         }
  130.         printf("\n Size of the queue is %d \n",count+1);
  131.     }
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement