Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <stdlib.h>
- #include <iostream>
- #include <string>
- using namespace std;
- void enqueue(struct node **p, struct node **pb, string name, int element, double priority);
- void display(struct node *p);
- void dequeue(struct node **p);
- struct node
- {
- int data;
- double priority;
- int order;
- string name;
- struct node *link;
- };
- int main()
- {
- struct node *front = (struct node *)malloc(sizeof(struct node));
- front = NULL;
- struct node *rear = (struct node *)malloc(sizeof(struct node));
- rear = front;
- while (true)
- {
- cout << "\n1. Add a Student\n2. Removing lowest ranking student\n3. View all student's marks\n4. Exit";
- int ch;
- cout << "\n Enter your chioce: ";
- cin >> ch;
- if (ch == 4)
- {
- break;
- }
- switch (ch)
- {
- case 1:
- {
- string name;
- int id;
- double cgpa;
- cout << "Enter Student's name: ";
- cin >> name;
- cout << "Enter Student's id no.: ";
- cin >> id;
- cout << "Enter Student's CGPA: ";
- cin >> cgpa;
- enqueue(&front, &rear, name, id, cgpa);
- }
- break;
- case 2:
- dequeue(&front);
- break;
- case 3:
- display(front);
- break;
- default:
- cout << "Wrong choice";
- break;
- }
- }
- }
- void enqueue(struct node **p, struct node **pb, string name, int element, double priority)
- {
- struct node *temp = (struct node *)malloc(sizeof(struct node));
- cout << "Name: " << name;
- temp->name = name;
- cout << "Element: " << element;
- temp->data = element;
- cout << "prioritiy is problem";
- temp->priority = priority;
- cout << "Here, Priority: " << temp->priority;
- for (int i = 0; i < rand() % 10; i++)
- {
- rand();
- }
- temp->order = rand() % 100;
- temp->link = NULL;
- cout << "Element: " << temp->data << ". " << temp->name << ": " << temp->priority << "(" << temp->order << ")";
- if ((*p) == NULL)
- {
- (*p) = temp;
- (*pb) = (*p);
- }
- else if ((*p) == (*pb))
- {
- if ((*pb)->priority > temp->priority)
- {
- struct node *t = (*pb);
- temp->link = t;
- (*pb) = temp;
- }
- else if ((*pb)->priority < temp->priority)
- {
- struct node *t = (*pb);
- t->link = temp;
- (*pb) = t->link;
- }
- else
- {
- if ((*p)->order > temp->order)
- {
- struct node *t = (*pb);
- temp->link = t;
- (*pb) = temp;
- }
- else
- {
- struct node *t = (*pb);
- t->link = temp;
- (*pb) = t->link;
- }
- }
- }
- else
- {
- struct node *t = (*p);
- if (t->priority > temp->priority)
- {
- temp->link = t;
- (*p) = temp;
- }
- else
- {
- while (true)
- {
- if (t->link == NULL)
- {
- break;
- }
- if ((t->link)->priority > temp->priority)
- {
- break;
- }
- else if ((t->link)->priority == temp->priority)
- {
- if ((t->link)->order > temp->order)
- {
- break;
- }
- }
- t = t->link;
- }
- temp->link = t->link;
- t->link = temp;
- }
- }
- }
- void dequeue(struct node **p)
- {
- if ((*p) == NULL)
- {
- cout << "\nEmpty queue\n";
- }
- else
- {
- struct node *t = (*p);
- (*p) = (*p)->link;
- free(t);
- }
- }
- void display(struct node *p)
- {
- struct node *t = p;
- cout << "\n";
- while (t != NULL)
- {
- cout << t->data << ". " << t->name << "\n";
- t = t->link;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment