Question
#include using namespace std; class Node { public: int item; Node *next; Node(int d = 0) { item = d; next = NULL; } };
#include
class Node { public: int item; Node *next; Node(int d = 0) { item = d; next = NULL; } };
// To print the list of nodes from head to tail void print(Node *head) { Node *node = head; while (node) { cout item next; } cout
int main() { Node *head, *tail; head = tail = new Node(45); for (int n = 50; n next = new Node(n); tail = tail->next; }
print(head); Node *node;
//------------------------------------------------- // Task 1: Insert a new node (with the item = 111) at // the end of the list (3 marks)
print(head);
//------------------------------------------------- // Task 2: Insert a new node (with the item = 222) between // node 65 and node 70 (7 marks)
print(head); //------------------------------------------------- // Task 3: Delete the first node from the list (3 marks)
print(head); //------------------------------------------------- // Task 4: Swap the positions of the first and last nodes (9 marks)
print(head); //------------------------------------------------- // Task 5: Find a node in the list with the number that // you have entered (8 marks)
int num; bool found = false; cout > num; //Complete your Task 5 here
cout Question 1 [30 Marks Given a C++ program file, Test2_Q1.cpp, which creates a linked list with the initial state as shown in Figure 1. The variables head and tail are pointers that pointing to the first and last node of the list, respectively. 45 50 55 60 65 80 head Figure 1: The initial state of the list tail Append the appropriate lines of code at the space provided in Test2_Q1.cpp to accomplish each of the following tasks. Note that, cach task is continous from one to the next. The state of the list after the execution of each task is given in each question below. You must not change any line of code regarding the class definition and the given lines of code in the main function. Task 1: Insert a new node (with the item = 111) at the end of the list. The result is given in Figure 2. (3 marks) 13153503513601765177032132014) Figure 2 Task 2: Insert a new node (with the item = 222) between node 65 and node 70. The result is given in Figure 3. Note: In this task, you should use loop. (7 marks) head tail 00 75 head Figure 3 tail Task 3: Delete the first node from the list. Figure 4 shows the result of this task. (3 marks) 55 67 60 65 H210770 177510 80 head Figure 4 tail 2 Task 4: Swap the positions of the first and last nodes in the list, i.e. the node with the item - 111 becomes the first node, whereas the node with the item - 50 becomes the last node. The result is given in Figure 5. (9 marks) W55 60 65 7221770 175 1767) head Figure 5 tail Task 5: Find a node in the list with the number that you have entered. Figure 6 shows the example of runs for this task. Note: value in bold is input by the user. (8 marks) //Run 1 //Run 2 Enter a number that you want to Enter a number that you want to find in the list: 90 find in the list: 60 90 is NOT in the list 60 is in the list Figure 6: Example of runs for Task 5
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