Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add a node to a linked list Write a program add(Node* head, int index, int value) to add a node to a linked list. The

Add a node to a linked list

Write a program add(Node* head, int index, int value) to add a node to a linked list. The head of the linked list is input, as well as the index where the node should be added and the value associated with the node. The program returns the head of the updated list. If the index is greater than the size of the list, the program should return NULL.

We have defined the following node C++ class for you:

class Node { public: int value; Node* next = NULL; };

The program input is a value, an index, and a list. The output is a traversal of the updated list.

Sample Input 1:

5 1 0->1->2->3 

Sample Output 1:

0->5->1->2->3 

Sample Input 2:

5 0 0->1->2->3

Sample Output 2:

5->0->1->2->3

Sample Input 3:

5 4 0->1->2->3

Sample Output 3:

0->1->2->3->5

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

Advances In Knowledge Discovery In Databases

Authors: Animesh Adhikari, Jhimli Adhikari

1st Edition

3319132121, 9783319132129

Students also viewed these Databases questions