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

#include <string> #include <iostream> using namespace std; struct Grade { string

ID: 3766145 • Letter: #

Question

#include <string>
#include <iostream>
using namespace std;

struct Grade
{
   string name;
   string letterGrade;
   int numericGrade;
   Grade *pNext;       // used for linked lists
};

// put functions here

// main
int main()
{
   // 1. declare five Grade pointers named pAlbert, pBonnie,
   // pCandice, pDouglas, and pHead1 (the last character is a digit).

   // 2. dynamically allocate a Grade instance using the new command
   // and set the data fields to "Albert", "B", 73, and nullptr. Start with:
   pAlbert -> name = "Albert";

   // 3. repeat for Bonnie, D, 57, nullptr
   // repeat for Candice, A, 83, nullptr
   // repeat for Douglas, C, 68, nullptr
  
   // 4. connect the four together to form a linked list with the elements in
   // alphabetical order. One useful line is given.
   pAlbert -> pNext = pBonnie;

   // Set pHead1 to the beginning of the linked list. For example, if
   // Sam was first in the linked list (he isn't!), you would write:
   // pHead1 = pSam;

   // 5. Write a function named gradePrint (and place it in front of main)
   // to print one Grade element of the linked list. It should take a
   // pointer to a Grade as its parameter and return void.
   // For Albert, it should print:
   // ->[Albert B 73]

   // 6. Call gradePrint to print Bonnie's and Douglas's info. The info
   // should appear on separate lines.

   // 7. Using a 'for' loop, print out the linked list. Call gradePrint to
   // print each individual record. (Leave this loop in place even though
   // you later write a function for this purpose.)

   // Set all pointers, except pHead1, to nullptr.
   // Now the only way to access the linked list is to use the pHead1
   // pointer. This is the normal way with linked lists.

   // 8. Write a function named gradeListPrint to print a linked list.
   // It should take a parameter named pFront, which is a pointer to a
   // Grade, and return void. Place the function in front of the main
   // function.

   // 9. Call gradeListPrint to print the linked list by passing in the
   // pointer to the head of the linked list. No * or & symbols are
   // needed since pHead1 is of the right type.

   // 10. Use a 'for' loop to count the number of records in the linked list
   // and print the total. (Leave this loop in place even though you later
   // write a function for this purpose.) Don't change pHead1 or you will
   // lose your linked list.

   // 11. Write a function named gradeListCount to count the number of
   // records. It returns an unsigned integer. Its only parameter is a
   // pointer to a Grade named pFront.

   // Call the function gradeListCount to count the elements in the linked
   // list, as follows (uncomment the following line):
   // cout << "Count: " << gradeListCount(pHead1) << endl;

   // 12a. To create a new Grade record with the same information as the
   // record pointed to by pHead1, except with a nullptr in its pNext field,
   // do the following:
   Grade* temp = new Grade;
   // RIGHT: The following is a correct way to do it:
   temp -> name = pHead1 -> name;
   temp -> letterGrade = pHead1 -> letterGrade;
   temp -> numericGrade = pHead1 -> numericGrade;
   temp -> pNext = nullptr;
   // WRONG: The following is the wrong way to do it. It would lose the
   // new record. temp would point at the same thing as pHead1.
   // temp = pHead1;

   // 12b. The following code copies a linked list with head pHead1 to give
   // another with head pHead2
   Grade* pHead2 = nullptr;
   Grade* pPrev2 = nullptr;
   for (Grade* p1 = pHead1; p1 != nullptr; p1 = p1 -> pNext)
   {
       Grade* p2 = new Grade;
       // when creating the first node in the new list,
       // we should save a pointer to it because it is the head
       if (pHead2 == nullptr)
           pHead2 = p2;
       // copy in fields from other list element
       p2 -> name = p1 -> name;
       // FILL IN CODE HERE TO COPY OTHER FIELDS
       p2 -> pNext = nullptr;
       if (pPrev2 != nullptr)
           pPrev2 -> pNext = p2;
       pPrev2 = p2;
   }

   // Print the list with head pHead1; then print the one with head pHead2.

   // 13. Write a function named gradeListCopy that takes a pointer to the
   // head of one linked list a returns a pointer to the head of a new list
   // that contains newly allocated elements. The names and grades in the
   // two linked lists should be the same. (Copy the method given above.)

   // 14. Call gradeListCopy to copy the linked list pointed to by pHead2 to
   // create a new list. Assign the result of the function to pHead3.

   // Print the list with head pHead3.

   // 15. Create a new node with your name and desired grade in it. Attach it
   // to the end of the list with head pHead2. Don't lose the pointer to this
   // list.

   // Print the list with head pHead1 using GradeListPrint.
   cout << "List 1: " << endl;


   // 16. Print the list with head pHead2 using GradeListPrint.

   // 17. Delete the list with head pHead1 by using a 'for' loop and deleting
   // the nodes one by one. Afterwards, set pHead1 to nullptr.

   // 18. Print the list with head pHead1 using GradeListPrint (should print
   // nothing).


   // 19. Print the list with head pHead2 using GradeListPrint.

   // 20. Print the list with head pHead3 using GradeListPrint.

}

Explanation / Answer

Sample code:

Sample output: