Trusted by Students Everywhere
Why Choose Us?
0% AI Guarantee

Human-written only.

24/7 Support

Anytime, anywhere.

Plagiarism Free

100% Original.

Expert Tutors

Masters & PhDs.

100% Confidential

Your privacy matters.

On-Time Delivery

Never miss a deadline.

Following are sample problems for the in-lab final exam for next week

Computer Science Mar 10, 2023

Following are sample problems for the in-lab final exam for next week. These all assume that we have:

   struct Node {

      int data;

      Node *pNext;

   };

and in main() we have:

   Node *pHead = NULL; // pointer to the head of the list

   Node *pTemp; // pointer used to get new nodes
 

For each node in a linked list, modify it to have the sum of the previous nodes and itself. The first node would then contain the value of the first element, the second node would contain the sum of the first and second nodes, and so on and so forth until the last node contains the sum of all the nodes on the list. The function declaration would be:

void accumulateSum(Node* pHead);

 

 

Assume the list we are starting with is: 

     2->8->9->5->11->3->6

Calling accumulateSum(...) from within main would look like:

 

accumulateSum(pHead);
		displayList(pHead);

and it would result in the following output:

 2->10->19->24->35->38->44

 

Starter Code, C++:


#include <iostream>
using namespace std;

// Declare the Node structure to be used for the linked list.
// Each node should have an int for data, and
// a pointer to the next node.
struct Node {
   int data;
   Node *pNext;
};

// Displays the list number values
void display( Node *pTemp){
    while( pTemp != NULL) {
        cout << pTemp->data << " ";
        pTemp = pTemp->pNext;
    }
    cout << endl;
}

// Create a new node, put the userInput number in it, and APPEND
// it to the END of the list.
void append(Node* &pHead, int userInput) {
    Node *pTemp = new Node;
    pTemp->data = userInput;
    pTemp->pNext = NULL;
    Node *pLast = pHead;

    // Find the last node on the list
    while(pLast != NULL && pLast->pNext != NULL) {
        pLast = pLast->pNext;
    }

    if( pLast == NULL) {
        // Make this new node the first node on an otherwise empty list
        pHead = pTemp;
    }
    else {
        // Append this new node to the end of the existing list
        pLast->pNext = pTemp;
    }
}

//-----------------------------------------------------------------------
//       *** Do not change anything above this point. ***
//------------------------------------------------------------------------
// The function you have to complete would be here:
void someFunctionToDo(Node* &pHead) {   
    //...
}

//-----------------------------------------------------------------------
//       *** Do not change anything below this point. ***
//------------------------------------------------------------------------
int main() {
   int userInput;
   Node *pHead = NULL;  // pointer to the head of the list

   // Create the linked list
   cout << "Enter list numbers separated by space, followed by -1: ";
   cin >> userInput;

   // Keep looping and appending nodes on the list until 
   // end-of-input flag of -1 is given
   while(userInput != -1) {
        // Store this number on the list
        append(pHead, userInput);
        cin >> userInput;
    }

    // Call the function and display the list before and after
    cout << "The list is: " << endl;
    display(pHead);
    someFunctionToDo(pHead);
    display(pHead);
    return 0;
}// end main()

Expert Solution

For detailed step-by-step solution, place custom order now.
Need this Answer?

This solution is not in the archive yet. Hire an expert to solve it for you.

Get a Quote
Secure Payment