Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++. Given the following class declaration: struct Student { char * name; float

ID: 3697965 • Letter: C

Question

C++. Given the following class declaration:

struct Student

{

   char *     name;

   float      gpa;

};

class StudentList

{

public:

   bool remove(const char name[], Student& aStudent);

   void findHighestGpa(Student& aStudent) const;

   bool get(int index, Student& aStudent) const;

   …

private:

   struct Node

   {

        Student data;

        Node * next;

   };

   Node * head;

   int size;

};

Implement the function:

bool StudentList::get(int index, Student& aStudent) const;

This function passes back the student object at position “index” of the list through “aStudent.” It returns true if the operation is successful and false if index is out of range.

Explanation / Answer

/
bool StudentList::remove(const char name[], Student& aStudent)
{
   Node *   curr;       // Points to the node to be deleted
   Node * trailCurr; // Points to the node just before the node pointed to by curr
   bool found;

   curr = head;
   trailCurr = NULL;
   found = false;

   while (curr != NULL && !found)
   {
       // If found a match
       if (strcmp(name, curr->data.name) == 0)
       {
           found = true;
           // Pass student to be removed back through aStudent
           aStudent.name = curr->data.name;
           aStudent.gpa = curr->data.gpa;

           // If item to be deleted is the head
           if (head == curr)
           {
               head = head->next;
               delete curr;
           }
           // If item is somewhere else in the list
           else
           {
               trailCurr->next = curr->next;
               delete curr;
           }
           size--;
       }
       // Keep searching if not found
       else
       {
           trailCurr = curr;
           curr = curr->next;
       }
   }
      
   // Return false if student not found
   if (!curr)
       return false;
   else
       return true;
}

void StudentList::findHighestGpa(Student& aStudent) const
{
   Node *   curr;
   float highestGPA = 0;

   for (curr = head; curr != NULL; curr = curr->next)
   {
       if (highestGPA < curr->data.gpa)
       {
           // Assign if current gpa is higher than highest GPA value
           highestGPA = curr->data.gpa;

           // Pass student with highest GPA back through aStudent
           aStudent.name = curr->data.name;
           aStudent.gpa = curr->data.gpa;
       }
       else
       {
           continue;
       }
   }
}

bool StudentList::get(int index, Student& aStudent) const
{
   if (index < 0 || index >= size)
       return false;

   // Traverse to the position
   Node *   curr = head;
   for (int i = 0; i < index; i++)
   {
       curr = curr->next;
   }

   // Pass student at index back through aStudent
   aStudent.name = curr->data.name;
   aStudent.gpa = curr->data.gpa;

   return true;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote