thecoderenroute

Name

Oct 13th, 2020
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.98 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. void enqueue(struct node **p, struct node **pb, string name, int element, double priority);
  9. void display(struct node *p);
  10. void dequeue(struct node **p);
  11.  
  12. struct node
  13. {
  14.     int data;
  15.     double priority;
  16.     int order;
  17.     string name;
  18.     struct node *link;
  19. };
  20.  
  21. int main()
  22. {
  23.     struct node *front = (struct node *)malloc(sizeof(struct node));
  24.     front = NULL;
  25.  
  26.     struct node *rear = (struct node *)malloc(sizeof(struct node));
  27.     rear = front;
  28.  
  29.     while (true)
  30.     {
  31.         cout << "\n1. Add a Student\n2. Removing lowest ranking student\n3. View all student's marks\n4. Exit";
  32.         int ch;
  33.         cout << "\n Enter your chioce: ";
  34.         cin >> ch;
  35.  
  36.         if (ch == 4)
  37.         {
  38.             break;
  39.         }
  40.  
  41.         switch (ch)
  42.         {
  43.         case 1:
  44.         {
  45.             string name;
  46.             int id;
  47.             double cgpa;
  48.             cout << "Enter Student's name: ";
  49.             cin >> name;
  50.             cout << "Enter Student's id no.: ";
  51.             cin >> id;
  52.             cout << "Enter Student's CGPA: ";
  53.             cin >> cgpa;
  54.             enqueue(&front, &rear, name, id, cgpa);
  55.         }
  56.         break;
  57.         case 2:
  58.             dequeue(&front);
  59.             break;
  60.         case 3:
  61.             display(front);
  62.             break;
  63.         default:
  64.             cout << "Wrong choice";
  65.             break;
  66.         }
  67.     }
  68. }
  69.  
  70. void enqueue(struct node **p, struct node **pb, string name, int element, double priority)
  71. {
  72.     struct node *temp = (struct node *)malloc(sizeof(struct node));
  73.     cout << "Name: " << name;
  74.     temp->name = name;
  75.     cout << "Element: " << element;
  76.     temp->data = element;
  77.     cout << "prioritiy is problem";
  78.     temp->priority = priority;
  79.     cout << "Here, Priority: " << temp->priority;
  80.  
  81.     for (int i = 0; i < rand() % 10; i++)
  82.     {
  83.         rand();
  84.     }
  85.  
  86.     temp->order = rand() % 100;
  87.     temp->link = NULL;
  88.  
  89.     cout << "Element: " << temp->data << ". " << temp->name << ": " << temp->priority << "(" << temp->order << ")";
  90.  
  91.     if ((*p) == NULL)
  92.     {
  93.         (*p) = temp;
  94.         (*pb) = (*p);
  95.     }
  96.     else if ((*p) == (*pb))
  97.     {
  98.         if ((*pb)->priority > temp->priority)
  99.         {
  100.             struct node *t = (*pb);
  101.             temp->link = t;
  102.             (*pb) = temp;
  103.         }
  104.         else if ((*pb)->priority < temp->priority)
  105.         {
  106.             struct node *t = (*pb);
  107.             t->link = temp;
  108.             (*pb) = t->link;
  109.         }
  110.         else
  111.         {
  112.             if ((*p)->order > temp->order)
  113.             {
  114.                 struct node *t = (*pb);
  115.                 temp->link = t;
  116.                 (*pb) = temp;
  117.             }
  118.             else
  119.             {
  120.                 struct node *t = (*pb);
  121.                 t->link = temp;
  122.                 (*pb) = t->link;
  123.             }
  124.         }
  125.     }
  126.     else
  127.     {
  128.         struct node *t = (*p);
  129.         if (t->priority > temp->priority)
  130.         {
  131.             temp->link = t;
  132.             (*p) = temp;
  133.         }
  134.         else
  135.         {
  136.             while (true)
  137.             {
  138.                 if (t->link == NULL)
  139.                 {
  140.                     break;
  141.                 }
  142.                 if ((t->link)->priority > temp->priority)
  143.                 {
  144.                     break;
  145.                 }
  146.                 else if ((t->link)->priority == temp->priority)
  147.                 {
  148.                     if ((t->link)->order > temp->order)
  149.                     {
  150.                         break;
  151.                     }
  152.                 }
  153.                 t = t->link;
  154.             }
  155.  
  156.             temp->link = t->link;
  157.             t->link = temp;
  158.         }
  159.     }
  160. }
  161.  
  162. void dequeue(struct node **p)
  163. {
  164.     if ((*p) == NULL)
  165.     {
  166.         cout << "\nEmpty queue\n";
  167.     }
  168.     else
  169.     {
  170.         struct node *t = (*p);
  171.         (*p) = (*p)->link;
  172.         free(t);
  173.     }
  174. }
  175.  
  176. void display(struct node *p)
  177. {
  178.     struct node *t = p;
  179.     cout << "\n";
  180.     while (t != NULL)
  181.     {
  182.         cout << t->data << ". " << t->name << "\n";
  183.         t = t->link;
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment