Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedimage text in transcribedimage text in transcribed

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. num/ num2 next num/ num2 next num/ num 2 next 5 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.cpp 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 numl 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) mumi mm2 next mm mm2 next num! rum2 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) 2 numl num2 ne mumi mm2 next numl num2 next numl 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) numl num2 next muml num2 numl num2 numl num2 5 12 next next next 1 8 + 2 9 3 10 head last Figure 4 Task 5: Write the code to insert new node with data num1 = 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) numl num2 nec numl num2 next numl rum 2 hect numl num2 next numinum2 next 1 8 2 9 3 10 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) numl num2 next numl num2 numl num2 nert nul num2 next nect numl rum2 next HOBOBBLE head last 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 node as new head Traverse and display linklist data 11 81 12 91 13 101 15 12 Executing Task 5 - insert new node (4, 11) before last node Traverse and display linklist data 11 81 12 91 13 101 14 111 15 121 Executing Task 6 - delete the second node Traverse and display linklist data 11 81 13 101 1 4 11 15 121 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

How Do I Use A Database Research Tools You Can Use

Authors: Laura La Bella

1st Edition

1622753763, 978-1622753765

More Books

Students also viewed these Databases questions

Question

Show that is as given in Eq. (11.2.5).

Answered: 1 week ago

Question

Approaches to Managing Organizations

Answered: 1 week ago

Question

Communicating Organizational Culture

Answered: 1 week ago