Question
c++ linked list #include using namespace std; struct Node { int data; Node* next; }; /* Task 1: Implement the following function for adding a
c++ linked list
#include using namespace std;
struct Node { int data; Node* next; };
/* Task 1: Implement the following function for adding a new node to the front of the given list with double the given value.
input Nodes* head: a head pointer to a list int val: an integer that represent a value.
return the head pointer of the new list after the insertion
Example: add a node with given value 9 before HEAD->1->2->3->NULL after HEAD->18->1->2->3->NULL
*/
Node* addDouble(Node* head, int val){
fill in here????
}
/* Task 2: Implement the following function for summing up the elements in the even position of the given list. Assume the head node is in position 1.
input Nodes* head: a head pointer to a list
return void output The sum of all elements in the even position of a given list
Example: input HEAD->1->2->3->9->12->NULL output 11 (2+9=11)
*/
void sumEven(Node* head){ fill in here????
}
/* Task 3: Implement the following function for reversing the node with the minimum value between Node n1 and Node n2.
input Nodes* n1: a pointer to a node in a list Nodes* n2: a pointer to a node in the same list
return the node got removed which has the minimum value between the two given nodes.
Example:
before HEAD->1->2->3->9->12->NULL
after HEAD->1->3->2->9->12->NULL
*/
Node* reverse(Node* n1, Node* n2){ fill in here??????
}
// You are not supposed to change the main function int main() {
Node* head = NULL; Node *p4, *p7;
for(int i = 1; i < 10; i++) { head = addDouble(head, i); if (i==4) p4 = head; if (i==7) p7 = head; } // at this point, we have created the following list: HEAD->18->16->14->12->10->8->6->4->2->NULL // p4 now points to node 4 (the node containing 8); p7 now points to node 7 (the node containing 14)
reverse(p4, p7); // between p4 and p7, there are 2 nodes. // The resulting list is HEAD->18->16->14->10->12->8->6->4->2->NULL
//You can uncomment this line to test. //the output should be: 38 //Please remember to comment this line out before submitting // cout <- sumEven(head);
head = addDouble(head, 16); head = addDouble(head, 20); // at this point, the list is: HEAD->40->32->18->16->14->10->12->8->6->4->2->NULL
reverse(p7, head); // between p7 and head, there are 3 nodes (32, 18 and 16). //The list after the function call is: HEAD->40->16->18->32->14->10->12->8->6->4->2->NULL cout << sumEven(head); // the output should be: 70
// free all nodes Node* tmp; while (head) { tmp = head; head = head->next; delete tmp; } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started