Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / Following are sample problems for the in-lab final exam for next week

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

Computer Science

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()

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE