Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct StudentInfo{
  6.     string name;
  7.     double CGPA;
  8.     int roll;
  9. };
  10.  
  11. struct Node{
  12.     StudentInfo student;
  13.     Node* left;
  14.     Node* right;
  15. };
  16.  
  17. void menu();
  18.  
  19. void heapify(StudentInfo * student,int val,int total);
  20.  
  21. bool push(StudentInfo * student, StudentInfo newStudent, int infoCap, int * cntr);
  22.  
  23. StudentInfo extractMax(StudentInfo * student,int * cntr);
  24.  
  25. void insertData(Node ** root,Node * newNode);
  26.  
  27. void printSortedData(Node * root);
  28.  
  29. int main()
  30. {
  31.     int infoCap = 100;
  32.     int cntr = 0;
  33.     int input=1;
  34.     StudentInfo student[105];
  35.     while(input)
  36.     {
  37.         menu();
  38.         cin>>input;
  39.         if(input == 1)
  40.         {
  41.             StudentInfo newStudent;
  42.             cout<<"Enter name"<<endl;
  43.             cin>>newStudent.name;
  44.             cout<<"Enter CGPA"<<endl;
  45.             cin>>newStudent.CGPA;
  46.             cout<<"Enter roll"<<endl;
  47.             cin>>newStudent.roll;
  48.             if(!push(student,newStudent,infoCap,&cntr)) cout<<"Priority queue is full"<<endl;
  49.         }
  50.         else if(input == 2)
  51.         {
  52.             if(cntr >0)
  53.             {
  54.                 StudentInfo maxStudent;
  55.                 maxStudent = extractMax(student,&cntr);
  56.                 cout<<"Name : "<<maxStudent.name<<endl;
  57.                 cout<<"CGPA : "<<maxStudent.CGPA<<endl;
  58.                 cout<<"Roll : "<<maxStudent.roll<<endl;
  59.             }
  60.             else cout<<"Queue empty"<<endl;
  61.         }
  62.         else if(input == 3)
  63.         {
  64.             Node * root = new Node;
  65.             root =  NULL;
  66.             for(int i = 1; i <=cntr;i++)
  67.             {
  68.                 Node * newNode = new Node;
  69.                 newNode->left = NULL;
  70.                 newNode->right = NULL;
  71.                 newNode->student.name = student[i].name;
  72.                 newNode->student.CGPA = student[i].CGPA;
  73.                 newNode->student.roll = student[i].roll;
  74.                 insertData(&root,newNode);
  75.             }
  76.             printSortedData(root);
  77.         }
  78.     }
  79.  
  80.     return 0;
  81. }
  82.  
  83. void menu()
  84. {
  85.     cout<<"Enter 1 to push new data"<<endl;
  86.     cout<<"Enter 2 to pop maximum value"<<endl;
  87.     cout<<"Enter 3 show sorted data"<<endl;
  88. }
  89.  
  90. void heapify(StudentInfo * student,int val,int total)
  91. {
  92.     int left = 2*val;
  93.     int right = 2*val +1;
  94.     int maxVal=1;
  95.     if(left<=total && student[left].CGPA > student[val].CGPA) maxVal = left;
  96.     else maxVal = val;
  97.     if(right<=total &&student[right].CGPA > student[val].CGPA) maxVal = right;
  98.     if(maxVal != val)
  99.     {
  100.         StudentInfo temp = student[maxVal];
  101.         student[maxVal] = student[val];
  102.         student[val] = temp;
  103.         heapify(student, maxVal,total);
  104.     }
  105. }
  106.  
  107. bool push(StudentInfo * student, StudentInfo newStudent, int infoCap, int * cntr)
  108. {
  109.     if(*cntr >= infoCap) return false;
  110.     student[++(*cntr)] = newStudent;
  111.     for(int i=(*cntr)/2;i>0;i--) heapify(student,i,*cntr);
  112.     return true;
  113. }
  114.  
  115. StudentInfo extractMax(StudentInfo * student,int * cntr)
  116. {
  117.     StudentInfo newStudent;
  118.     newStudent = student[1];
  119.     StudentInfo temp = student[*cntr];
  120.     (*cntr)--;
  121.     student[1] = temp;
  122.     for(int i=(*cntr)/2;i>0;i--) heapify(student,i,*cntr);
  123.     return newStudent;
  124. }
  125.  
  126. void insertData(Node ** root,Node * newNode)
  127. {
  128.     if(*root == NULL)
  129.     {
  130.         *root =  newNode;
  131.     }
  132.     else
  133.     {
  134.         if(newNode->student.CGPA < (*root)->student.CGPA) insertData(&((*root)->left),newNode);
  135.         else insertData(&((*root)->right),newNode);
  136.     }
  137. }
  138.  
  139. void printSortedData(Node * root)
  140. {
  141.     if(root == NULL) return ;
  142.  
  143.     printSortedData(root->left);
  144.     cout<<"Name : "<<root->student.name<<endl;
  145.     cout<<"CGPA : "<<root->student.CGPA<<endl;
  146.     cout<<"Roll : "<<root->student.roll<<endl;
  147.  
  148.     printSortedData(root->right);
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement