Question: Write a C++ function to add a node to the beginning of a linked list. Please note the function call is void AddNode(Node*& head, double

Write a C++ function to add a node to the beginning of a linked list. Please note the function call is

void AddNode(Node*& head, double key) 

Your function takes two arguments - the head of the linked list and the value key to be added. Note that the list may be empty!

Your function should modify the head of the linked list to point to the new node, and set the new node to point to the rest of the list. If the rest of the list is empty, the new node points to null.

Example:

Initial List: 4->2->3, key = 5

List After Function Call: 5->4->2->3

void AddNode(Node*& head, double key); 

The linked list structure:

struct Node { double val; node *next; }; 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!