Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

image text in transcribed

image text in transcribed

#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

Node *current = head;

while (current)

{

cout num1 num2

current = current->next;

}

cout

} //display

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

{

if (counter != 0)

{

f++;

s++;

last->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

int sum = 0;

//Calculate the value of sum

//Answer Task 1 start here

cout

//Task 1 end

/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////

//Task 2 : Write the code to add 3 to the num2 value in all the nodes in the

// linklist [6 marks]

cout

//Answer Task 2 start here

//Task 2 end

/////////////////////////////////////////////////////////////////////////////

display(head);

/////////////////////////////////////////////////////////////////////////////

//Task 3 : Write the code to create a new node with data num1 = 5 and num2 = 12

// and set it is as the new head [4 marks]

cout

//Answer Task 3 start here

//Task 3 end

/////////////////////////////////////////////////////////////////////////////

display(head);

/////////////////////////////////////////////////////////////////////////////

//Task 4 : Write the code to move the head to become the last node and then

// set the second node as the new head [4 marks]

cout

//Answer Task 4 start here

//Task 4 end

/////////////////////////////////////////////////////////////////////////////

display(head);

/////////////////////////////////////////////////////////////////////////////

//Task 5: Write the code to insert new node with data num1 = 4 and num2 = 11

// before the last node [8 marks]

cout

//Answer Task 5 start here

//Task 5 end

/////////////////////////////////////////////////////////////////////////////

display(head);

/////////////////////////////////////////////////////////////////////////////

//Task 6: Write the code to delete the second node in the list [4 marks]

cout

//Answer Task 6 start here

//Task 6 end

/////////////////////////////////////////////////////////////////////////////

display(head);

return 0;

} //main

Given a CH program file, Test2(031219)_Q1.cpp, 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. numl num2 next num num2 next ! / num2 next 1 5 1 2 6 3 7 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) numl num2 next num! num2 next num/ num2 next 8 2 9 10 head last Figure 2 Task 3: Write the code to add new node with data num1 = 5 and num2 = 12 and set it as the new head. The result is given in Figure 3. (4 marks) mum/ num2 next numl num2 next num] mum2 next num/ 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) muml num2 next numl num2 next numl num2 next numl num2 next 8 2 2 9 3 10 5 12 head last Figure 4 Task 5: Write the code to insert new node with data numl = 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) numinum2 next numl num2 next numl num2 next numl num2 next muml num2 next 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) muml num2 next mum num2 next 2 next 2 numl num2 8 310 4 5 12 next 1 head last Figure 6 Traverse and display linklist data 11 51 12 61 13 71 Executing Task i second node calculate the sum of numl value for the first and 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 1291 13 101 Executing Task 3 add new node (5, 12) as new head Traverse and display linklist data 15 12 11 81 12 91 13 101 Executing Task 4 - set head to last and set second node as new head Traverse and display linklist data 11 81 1291 13 101 15 121 Executing Task 5 - insert new node (4, 11) before last node Traverse and display linklist data 1181 12 91 13 101 14 111 15 121 Executing Task 6 - delete the second node Traverse and display linklist data 1181 13 101 14 111 15 12 Figure 7: The output of the program for Question 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Neo4j

Authors: Gregory Jordan

1st Edition

1484200225, 9781484200223

More Books

Students also viewed these Databases questions

Question

Describe Yaloms therapeutic factors for group psychotherapy.

Answered: 1 week ago