Advertisement
coffeebeforecode

Untitled

Jun 8th, 2021
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.78 KB | None | 0 0
  1. /*
  2. 5a) Write a program in C to create two queues using linked list and identify which queue is having
  3. more number of elements and search for the duplicate elements is present in both queues display
  4. the count of duplicates elements..
  5. All standard operations of queue are to be performed
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. // A linked list (LL) node to store a queue entry
  12. struct QNode {
  13.     int key;
  14.     struct QNode* next;
  15. };
  16.  
  17. // The queue, front stores the front node of LL and rear stores the
  18. // last node of LL
  19. struct Queue {
  20.     struct QNode *front, *rear;
  21. };
  22.  
  23. // A utility function to create a new linked list node.
  24. struct QNode* newNode(int k)
  25. {
  26.     struct QNode* temp = (struct QNode*)malloc(sizeof(struct QNode));
  27.     temp->key = k;
  28.     temp->next = NULL;
  29.     return temp;
  30. }
  31.  
  32. // A utility function to create an empty queue
  33. struct Queue* createQueue()
  34. {
  35.     struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
  36.     q->front = q->rear = NULL;
  37.     return q;
  38. }
  39.  
  40. // The function to add a key k to q
  41. void enQueue(struct Queue* q, int k)
  42. {
  43.     // Create a new LL node
  44.     struct QNode* temp = newNode(k);
  45.  
  46.     // If queue is empty, then new node is front and rear both
  47.     if (q->rear == NULL) {
  48.         q->front = q->rear = temp;
  49.         return;
  50.     }
  51.  
  52.     // Add the new node at the end of queue and change rear
  53.     q->rear->next = temp;
  54.     q->rear = temp;
  55. }
  56.  
  57. // Function to remove a key from given queue q
  58. void deQueue(struct Queue* q)
  59. {
  60.     // If queue is empty, return NULL.
  61.     if (q->front == NULL){
  62.         printf("\nEmpty Queue!");
  63.         return;
  64.     }
  65.     // Store previous front and move front one node ahead
  66.     struct QNode* temp = q->front;
  67.     printf("\nDequeued %d", temp->key);
  68.  
  69.     q->front = q->front->next;
  70.  
  71.     // If front becomes NULL, then change rear also as NULL
  72.     if (q->front == NULL)
  73.         q->rear = NULL;
  74.  
  75.     free(temp);
  76. }
  77.  
  78. void display(struct Queue* q){
  79.     struct QNode* temp = q->front;
  80.     if (temp == NULL){
  81.         printf("\nEmpty queue");
  82.         return;
  83.     }
  84.     printf("\n");
  85.     while(temp != NULL){
  86.         printf("%d->", temp->key);
  87.         temp = temp->next;
  88.     }
  89. }
  90.  
  91. void identifyLarger(struct Queue* q1, struct Queue* q2){
  92.     int a=0, b=0;
  93.     struct QNode* temp = q1->front;
  94.     while(temp != NULL){
  95.         a++;
  96.         temp = temp->next;
  97.     }
  98.  
  99.     temp = q2->front;
  100.     while(temp != NULL){
  101.         b++;
  102.         temp = temp->next;
  103.     }
  104.  
  105.     if (a>b){
  106.         printf("\nQueue 1 has more number of elements");
  107.     }
  108.     else if (a==b){
  109.         printf("\nBoth Queues have same number of elements");
  110.     }
  111.     else{
  112.         printf("\nQueue 2 has more number of elements");
  113.     }
  114. }
  115.  
  116. void dupElem(struct Queue* q1, struct Queue* q2){
  117.     struct QNode* t1= q1->front;
  118.  
  119.     int count = 0;
  120.     printf("\nDuplicate elements: ");
  121.    
  122.     while (t1 != NULL){
  123.         struct QNode* t2= q2->front;
  124.         while(t2 != NULL){
  125.             if (t1->key == t2->key){
  126.                 count++;
  127.                 printf("%d ", t1->key);
  128.             }
  129.             t2 = t2->next;
  130.         }
  131.         t1 = t1->next;
  132.     }
  133.     printf("\nHence there are %d duplicate elements", count);
  134. }
  135.  
  136.  
  137. // Driver Program to test anove functions
  138. int main()
  139. {
  140.     struct Queue* q1 = createQueue();
  141.     struct Queue* q2 = createQueue();
  142.  
  143.     int ch;
  144.     int temp;
  145.     while(1){
  146.         printf("\n\tMENU\n");
  147.         printf("\n1. Enqueue into Queue 1");
  148.         printf("\n2. Enqueue into Queue 2");
  149.         printf("\n3. Dequeue from Queue 1");
  150.         printf("\n4. Dequeue from Queue 2");
  151.         printf("\n5. Display Queue 1");
  152.         printf("\n6. Display Queue 2");
  153.         printf("\n7. Identify which Queue is having more number of elements");
  154.         printf("\n8. Count and print the duplicate Elements");
  155.         printf("\n9: Exit\n>>");
  156.  
  157.         scanf("%d", &ch);
  158.  
  159.         switch (ch)
  160.         {
  161.         case 1:
  162.             printf("\nEnter data to enter into Queue 1 : ");
  163.             scanf("%d", &temp);
  164.             enQueue(q1, temp);
  165.             break;
  166.         case 2:
  167.             printf("\nEnter data to enter into Queue 2 : ");
  168.             scanf("%d", &temp);
  169.             enQueue(q2, temp);
  170.             break;
  171.         case 3:
  172.             deQueue(q1);
  173.             break;
  174.         case 4:
  175.             deQueue(q2);
  176.             break;
  177.         case 5:
  178.             display(q1);
  179.             break;
  180.         case 6:
  181.             display(q2);
  182.             break;
  183.         case 7:
  184.             identifyLarger(q1,q2);
  185.             break;
  186.         case 8:
  187.             dupElem(q1, q2);
  188.             break;
  189.         case 9:
  190.             exit(0)  ;
  191.         default:
  192.             printf("\nWrong Choice!");
  193.             break;
  194.         }
  195.  
  196.     }
  197.  
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement