Question: #include using namespace std; //Represents node of the linked list class Node { public: int num1; int num2; Node* next; Node(int a, int b) {

 #include using namespace std; //Represents node of the linked list class

Node { public: int num1; int num2; Node* next; Node(int a, int

b) { num1 = a; num2 = b; next = NULL; }

#include using namespace std;

//Represents node of the linked list class Node { public: int num1; int num2; Node* next; Node(int a, int b) { num1 = a; num2 = b; next = NULL; } }; //Node

void display(Node *head) //Function to display - GIVEN { cout num1 num2 next; } cout

int main() { int counter = 0, f = 1; int s = 5; //Code to insert 3 nodes - GIVEN Node *head, *last; head = last = new Node(f, s); while (counter next = new Node(f, s); last = last->next; } counter++; }

display(head);

///////////////////////////////////////////////////////////////////////////// //Task 1 : Write the code to calculate and display the sum of num1 value for // first and second node [4 marks] cout

cout

cout

//Task 5 end ///////////////////////////////////////////////////////////////////////////// display(head); ///////////////////////////////////////////////////////////////////////////// //Task 6: Write the code to delete the second node in the list [4 marks] cout

return 0; } //main

Question 1 (30 Marks Given a C++ program file, Test2(031219)_Q1.epp, which creates a linked list with the initial state as shown in Figure 1. The variables head and last are pointers that pointing to the first and last node of the list, respectively. numi num2 next num num2 next num/ num2 next 1 5 2 6 3 7 4 head last Figure 1: The initial state of the list Append the appropriate lines of code at the space provided in Test2(031219)_Q1.epp to accomplish each of the following tasks. Note that each task is continous from one to the next. You must not change any line of code regarding the class definition and the given lines of code in the display and main function. Figure 7 shows the expected output when the program runs. Task 1: Write the code to calculate and display the sum of num1 value in the first and second node. (4 marks) Task 2: Write the code to add 3 to num2 value in all the nodes. Figure 2 shows the result of this task. Note: In this task, you should use loop. (6 marks) mm mm2 next mex mum rum2 Next 1 8 2 9 3 10 head last Figure 2 Task 3: Write the code to add new node with data num] = 5 and num2 = 12 and set it as the new head. The result is given in Figure 3. (4 marks) numi mm2 next num mam2 next mum! mm2 nex muml num2 Next 5 12 1 8 2 9 3 10 head last Figure 3 Task 4: Write the code to set the head as the last node and the second node as the new head node. The result is given in Figure 4. (4 marks) numlum2 next num! m2 muml num2 numl num2 next exi next 8 9 3 10 5 12 head last Figure 4 Task 5: Write the code to insert new node with data num] = 4 and num2 - 11 before the last node. Figure 5 shows the result of this task. Note: In this task, you should use loop. (8 marks) MI 2 next um m2 met RAMI Num2 next m2 next 2 t 1 8 2 9 3 10 11 5 12 head last Figure 5 Task 6: Write the code to delete the second node in the list. Figure 6 shows the result of this task. (4 marks) um num2 um m2 next um 2 next um 2 next next 2 1 8 10 4 5 12 head Figure 6 Traverse and display linklist data 11 51 12 61 13 71 Executing Task 1 - calculate the sum of numl value for the first and second node Sum of numl value in first and second node is equal to: 3 Executing Task 2 - add 3 to num2 value in all the nodes Traverse and display linklist data 11 81 12 91 13 101 Executing Task 3 - add new node (5, 12) as new head Traverse and display linklist data 15 121 11 81 12 91 13 101 Executing Task 4 - set head to last and set second nade as new head Traverse and display linklist data 11 81 12 91 13 101 15 121 Executing Task 5 - insert new node(4, 11) before last node Traverse and display linklist data 11 81 12 91 13 101 14 11 15 121 Executing Task 6 - delete the second node Traverse and display linklist data 11 81 13 101 14 111 15 121 Figure 7: The output of the program for Question 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!