Question
Write a C++ function to add a node to the beginning of a linked list. Your...
Write a C++ function to add a node to the beginning of a linked list.
Your function takes two arguments - the head of the linked list and the value num 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 not empty).
Example:
Initial Array: 4->2->3, key = 5
Array After Function Call: 5->4->2->3
void AddNode(node** head, int num);
The linked list structure:
struct node { int key; node *next; };
The objective is to understand how to make use of a pointer-to-a-pointer, and why it is important.
For example:
Test | Result |
---|---|
// head = 4 // AddNode(head, 2) // AddNode(head, 3) // AddNode(head, 5) | 5->3->2->4 |
Computer Science Engineering
< Previous
Next >
Answers
#include <iostream> using namespace std; struct Node { int val; Node *next; //it should Node instead of node }; /*Note if our list is empty then head must point to nullptr*/ void Add(Node **head, int key){ Node *temp = new Node; //Allocate memory for new node to be added temp->val = key; //assign key value temp->next = *head; //setting next of newly created node to head node to complete link *head = temp; //make newly created node head node } // main function to work correctness of Add function int main(){ Node *head = new Node; head->val = 4; head->next = nullptr; Add(&head, 2); //Now we have to pass address of head node Add(&head, 3); Add(&head, 5); Node *temp = head; while(temp->next != nullptr){ cout<<temp->val<<"->"; temp = temp->next; } cout<<temp->val<<endl; return 0; }
< Previous
Next >
Similar Solved Questions
3 answers
When is Diego's school's anniversary
When is Diego's school's anniversary?1. It will be next month.2. It is next month.(Which answer is correct? Are both correct? Which one is commonly used?)...
1 answers
What happens to water molecules when water is heated from 90°C to 110°C?
What happens to water molecules when water is heated from 90°C to 110°C?...
1 answers
Two docnete fo Random varrable X and Y have the following joint pre fro,y)= {k (2x²y...
Two docnete fo Random varrable X and Y have the following joint pre fro,y)= {k (2x²y + 2y x² - 1 x = 0, 1, 2 Dothonorge 9 = 1,3 ① bay = Cor(xy) (1) Use Mct,, tz) to check of your answer to (1) is correct...
1 answers
A. Required: 1. Please calculate the following ratios and amounts: a) working capital, current ratio, acid-test...
A. Required: 1. Please calculate the following ratios and amounts: a) working capital, current ratio, acid-test ratio, cash to current liabilities ratio, days’ sales in receivables (based on ending accounts receivables), days’ sales in inventory (based on cost of goods and ending invento...
1 answers
1. (a) A point charge of -6.0 C is placed at = -10.0 cm and a...
1. (a) A point charge of -6.0 C is placed at = -10.0 cm and a second point charge +3.00 AC is placed at 2 = 30.0 cm along the s-axis of a Cartesian coordinate system. (1) (3 pts) Find the electric field at 1 = 40.0 cm on the s-axis due to the two charges. Specify both the magnitude and direction of ...
1 answers
ASEPSI UNT INSTAD Unit 12: Upper Body Musculoskeletal Injuries Help O e. tennis elbow 5. A...
ASEPSI UNT INSTAD Unit 12: Upper Body Musculoskeletal Injuries Help O e. tennis elbow 5. A gymnast twists his elbow during a handspring. He has tenderness along the outside of the elbow and slight swelling. He is able to bend and straighten the elbow but has mild pain. He likely has what injury? O a...
1 answers
Please solve for X and Y. THANK YOU!
Please solve for X and Y. THANK YOU!...
1 answers
HW 13.1-13.4 Score: 8.6/29 9/23 answered Question 15 > Compute the STS (to two decimals) for...
HW 13.1-13.4 Score: 8.6/29 9/23 answered Question 15 > Compute the STS (to two decimals) for a comparison of two population standard deviations or variances given that: a. The sample standard deviations are 19.6 for Population 1 and 18.7 for Population 2. F= b. The sample variances are 19.6 for P...
1 answers
Michael is an occupational health nurse in Clay County, a rural county in Vermont. Clay County...
Michael is an occupational health nurse in Clay County, a rural county in Vermont. Clay County has experienced dramatic demographic changes in the past 3 years. Michael works at one of two new industries, which have brought in more than 50 new families of Hispanic origin. The dominant culture is whi...
1 answers
1. Agan Interior Design provides home and office decorating assistance to its customers. In normal operation,...
1. Agan Interior Design provides home and office decorating assistance to its customers. In normal operation, an average of 2.1 customers arrive each hour. One design consultant is available to answer customer questions and make product recommendations. Agan's management would like to evaluate t...
1 answers
What is it called when you mix up the sounds of words?
What is it called when you mix up the sounds of words?...
1 answers
QUESTION 13 X and Y are the divorced parents of JR. The divorce was granted in...
QUESTION 13 X and Y are the divorced parents of JR. The divorce was granted in June of 2020 and X was given custody, but Y has specific visitation rights. X provided $3,500 and Y provided $4,000 toward JR's total support of $7,900. Which of the following is true regarding who gets to claim JR as...
1 answers
Sheffield Company uses special strapping equipment in its packaging business. The equipment was purchased in January...
Sheffield Company uses special strapping equipment in its packaging business. The equipment was purchased in January 2019 for $11,700,000 and had an estimated useful life of 8 years with no salvage value. At December 31, 2020, new technology was introduced that would accelerate the obsolescence of S...
1 answers
The American Heart Association is about to conduct an anti-smoking campaign and wants to know the...
The American Heart Association is about to conduct an anti-smoking campaign and wants to know the fraction of Americans over 46 who smoke. Step 2 of 2 : Suppose a sample of 1089 Americans over 46 is drawn. Of these people, 774 don't smoke. Using the data, construct the 85% confidence...
1 answers
One significant consequence of the COVID-19 situation is that many meetings that formerly were held face-to-face...
One significant consequence of the COVID-19 situation is that many meetings that formerly were held face-to-face are now held virtually using something like Zoom or Skype. Suppose the first population is all face-to-face meetings held in March 2020, the second population is all Zoom meetings held in...
1 answers
Please I need help with variables that follows a normal distribution in medicine such as blood pressure. if I consider b...
please I need help with variables that follows a normal distribution in medicine such as blood pressure. if I consider blood pressure to have a larger standard variation such as number of different patients with high blood pressure. Now if I take into consideration those who are on blood pressure me...
1 answers
Variable Consideration Henderson Farms Inc. sells land to Harris Development Corporation for $2,000,000 It is also...
Variable Consideration Henderson Farms Inc. sells land to Harris Development Corporation for $2,000,000 It is also entitled to receive 6% of any future sales price of the developed land in excess of $5 million. Henderson Farms Inc. determines that its experience with similar contracts is of little p...