Advertisement
Guest User

Untitled

a guest
Mar 6th, 2018
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. /*
  6.  Implement the "studentList" data structure with a doubly linked list implementation.
  7.  
  8.  You must implement all methods and they must work as described in the comments.  You must also achieve the stated run times, and know the big-Oh run times for each of your methods.
  9.  */
  10.  
  11.  
  12. class student
  13. {
  14. public:
  15.     string name;
  16.     unsigned int id;
  17.     double gpa;
  18.    
  19.     student() //constructor, defaul name, ID and GPA
  20.     {
  21.         name = "ghost";
  22.         id = 0;
  23.         gpa = 0;
  24.     }
  25.     student(string _name, unsigned int _id, double _gpa) //param constructor,
  26.     {
  27.         id = _id;
  28.         gpa = _gpa;
  29.         name = _name;
  30.     }
  31. };
  32.  
  33. class studentList
  34. {
  35. private:
  36.     //Implement a doubly linked list of students
  37.    
  38.     class node
  39.     {
  40.     public:
  41.         student data;
  42.         node * next;
  43.         node * prev;
  44.        
  45.         node(student x)
  46.         {
  47.             data = x;
  48.             next = NULL;
  49.             prev = NULL;
  50.         }
  51.     };
  52.    
  53.     node * head;
  54.     node * tail;
  55.     node * temp;
  56.    
  57. public:
  58.     studentList()
  59.     {
  60.         head = NULL;
  61.         tail = NULL;
  62.     }
  63.    
  64.     //add a student to the list.
  65.     //Must run in O(1) time.
  66.     void insert(student s) //addBack
  67.     {
  68.         if (head == NULL)
  69.         {
  70.             head = new node(s);
  71.             temp = head;
  72.             tail = head;
  73.         }
  74.         else
  75.         {
  76.             temp = new node(s);
  77.             tail->next = temp;
  78.             temp->prev = tail;
  79.             temp->next = NULL;
  80.             tail = temp;
  81.  
  82.         }
  83.     }
  84.    
  85.     //find the student with the given id number and return it
  86.     //What is the worst case run time of this?
  87.     //What is the average case run time of this?
  88.     student retrieveStudent(unsigned int idnumber)
  89.     {
  90.         for (temp = head; temp != NULL; temp = temp->next)
  91.         {
  92.             if (temp->data.id == idnumber)
  93.             {
  94.                 return temp->data;
  95.             }
  96.             else
  97.             {
  98.             }
  99.         }
  100.     }
  101.    
  102.     //Change the gpa of the student with given id number to newGPA
  103.     //What is the run time?
  104.     //run time: O(n)
  105.     void updateGPA(unsigned int idnumber, double newGPA)
  106.     {
  107.         for (temp = head; temp != NULL; temp = temp->next)
  108.         {
  109.             if (temp->data.id == idnumber)
  110.             {
  111.                 temp->data.gpa = newGPA;
  112.             }
  113.         }
  114.     }
  115.    
  116.     //Add all students from otherlist to this list.
  117.     //otherlist should be empty after this operation.
  118.     //Must run in O(1) time.
  119.     void mergeList(studentList &otherlist);
  120.     {
  121.        
  122.     }
  123.     //create a list of students whose gpa is at least minGPA.
  124.     //Return this list.  The original list should
  125.     //not be modified (do not remove the students from the original list).
  126.     //Run time?
  127.     studentList honorRoll(double minGPA);
  128.    
  129.     //sort the list by the given field ("name", "id", or "gpa").
  130.     //Run time?
  131.     void sort(string field);
  132.    
  133.     //Print out each student in the list.  This is mainly for testing purposes.
  134.     void printList()
  135.     {
  136.         for (temp = head; temp != NULL; temp = temp->next)
  137.         {
  138.             cout << temp->data.name << endl;
  139.             cout << temp->data.id << endl;
  140.             cout << temp->data.gpa << endl;
  141.         }
  142.     }
  143. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement