Question: Language: C++ Suppose you have a Node class defined as follow class Node{ public: int data; Node* next = NULL; }; Do this: Write the
Language: C++
Suppose you have a Node class defined as follow
class Node{
public:
int data;
Node* next = NULL;
};
Do this: Write the remainder parts of the function with the following prototype (see below), so that it will allow you to insert a Node in a linked list.
Node* head => where this pointer points to is where the linked list begins. This is so that you have a starting point on where to find the linked list you'd like to insert nodes at.
int index = a integer parameter provided by user input so that it will insert a new node in the linked list at the specified index position
int inputData = an integer provided by the user to set the newly inserted Node member data variable data to
Node* insert(Node* head, int index, int inputData) {
//Write your code here
//Returns the head of the linked list after the node was inserted at index passed in
return head;
}
Example case:
Say we have a linked list of nodes with member data as follows 0->1->2->3->4
0 is at index 0
1 is at index 1 ...
4 is at index 4
Now we insert at index 0 with the by calling the following function in the main() of the c++ program:
head = insert(head, 0, -1);
The result should be a new linked list that looks like this:
-1 -> 0 -> 1 -> 2 -> 3 -> 4
Using the resulting linked list from the first insert function call, we then do this:
head = insert(head, 3, 10);
Then we should have something like this in the end
-1 -> 0 -> 1 -> 10 -> 2 -> 3 -> 4
In the event that we tried in an index that is greater than the size of the linked list, then head will be set to NULL and returned.
For example, if the linked list was 0->1, and you called head = insert(head, 3, 2), then the function should return NULL and there should be no insertion performed on the linked list of 0->1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
