1 answer

Write a C++ function to add a node to the beginning of a linked list. Your...

Question:

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 

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; } 

.

Similar Solved Questions

1 answer
HiTiak Cond For the circuit in Fig. 5 (0)-12 V and (0) 30 mA. nd 2....
hiTiak Cond For the circuit in Fig. 5 (0)-12 V and (0) 30 mA. nd 2. Fig S 2X10 a) Find the initial energy stored in the inductor at t-0 b) Find the initial energy stored in the capacitor at to c) Find the type of damping...
1 answer
B) Assuming the following. P(S)-0.3 P(BIS) 0.75 P(BIS) 0.20 P(HS'B)-0.15 P(H S'nB') 0.9 P(HİSnB)-0.20 Write out...
b) Assuming the following. P(S)-0.3 P(BIS) 0.75 P(BIS) 0.20 P(HS'B)-0.15 P(H S'nB') 0.9 P(HİSnB)-0.20 Write out the equations and compute: PSPCS) 0.320.1 c) Now eompute the probabilities pertaining to each section in the Venn diagram B2 BnsnH 5 2 3...
1 answer
After several profitable years running her business, BLANK decided to acquire the assets of a small...
After several profitable years running her business, BLANK decided to acquire the assets of a small competing business. On May 1 of year 1, BLANK acquired the competing business for $372,000. BLANK allocated $62,000 of the purchase price to goodwill. BLANK’s business reports its taxable income...
1 answer
Please let it be legible, thanks in advance! Review constants I Porodic Table The elastic energy...
Please let it be legible, thanks in advance! Review constants I Porodic Table The elastic energy stored in your tendons can contribute up to 35 % of your energy needs when running. Sports scientists have studied the change in length of the knee extensor tendon in sprinters and nonathletes. They ...
1 answer
Question 1 n= c/v where vis the [x]of light in the medium. Question 2 [x] is...
Question 1 n= c/v where vis the [x]of light in the medium. Question 2 [x] is a great circle that passes through both poles, and it forms a right angle with equator....
1 answer
Lambeth, Inc. produces the basic fillings used in many popular frozen desserts and treats-vanilla and chocolate...
Lambeth, Inc. produces the basic fillings used in many popular frozen desserts and treats-vanilla and chocolate ice creams, puddings, meringues, and fudge. Lambeth uses standard costing and carries over no inventory from one month to the next. The ice-cream product group's results for June 2017 ...
1 answer
Cle Tutorial Step 7: Prepare the Adjusted Trial Balance ding balances after adjustments rify ending balances...
cle Tutorial Step 7: Prepare the Adjusted Trial Balance ding balances after adjustments rify ending balances after adjustments e adjusted balance after adjustments have been posted to the T-Accounts. Accounts Receivable Accumulated Depreciation- Furniture Office Supplies Nov. 10 2.500 140 Dec. 31 No...
1 answer
Demand and Supply in Labor Markets As the text suggests, the demand and supply model predicts...
Demand and Supply in Labor Markets As the text suggests, the demand and supply model predicts that the new computer and communications technologies will raise the pay of high-skill labor but reduce the pay of low-skill labor. Explain how this works using the four-step process....
1 answer
Why is chlorination less selective than bromination?
Why is chlorination less selective than bromination?...
1 answer
Question 8 (10 points) Use Boolean algebra to simplify the following expression X = ABC +...
Question 8 (10 points) Use Boolean algebra to simplify the following expression X = ABC + ĀBC + ĀBC + ABC + ABC (2.5 points) Sketch the simplified circuit...
1 answer
Infants weighing less than 1,500 grams at birth are classed as "very low birth weight." Low...
Infants weighing less than 1,500 grams at birth are classed as "very low birth weight." Low birth weight carries many risks. IQ tests are designed to produce IQ scores varying Normally with standard deviation ?=15 . They are also scaled so that the mean score in a large population should be ...
1 answer
Quick Cleaners, Inc. (QCI), has been in business for several years. It specializes in cleaning houses...
Quick Cleaners, Inc. (QCI), has been in business for several years. It specializes in cleaning houses but has some small business clients as well. a. Issued $23,000 of QCI stock for cash. b. Incurred $660 of utilities costs this month and will pay them next month. c Incurred and paid wages for the c...
1 answer
Crash! Forgot to do a backup! Hard drive is toast! You have losta portion of accounting...
Crash! Forgot to do a backup! Hard drive is toast! You have losta portion of accounting information from Jordan and Taylor. Admitting your mistake will not only shake their confidence, but might also end your brownie deliveries. They are coming over to discuss variances in a couple hours. The only i...
1 answer
Enter your answer in the box provided. Calculate the pH of a 1.0 x 10-5 M...
Enter your answer in the box provided. Calculate the pH of a 1.0 x 10-5 M solution of HNO2. pH= )...
1 answer
1. What are the strength and direction of the electric field at the position indicated by...
1. What are the strength and direction of the electric field at the position indicated by the dot in the following figure? -5.0 nC 2.0 cm 10 nc 4.0 cm 5.0 nC +...
1 answer
Quantitative Problem: Andrews Corporation has income from operations of $231,000. In addition, it received interest income...
Quantitative Problem: Andrews Corporation has income from operations of $231,000. In addition, it received interest income of $23,100 and received dividend income of $29,500 from another corporation. Finally, it paid $12,000 of interest income to its bondholders and paid $42,400 of dividends to its ...
1 answer
Edit View History Bookmarks Window Help Tue 2:35 e ction Test ati O CLOSE Question: 10...
Edit View History Bookmarks Window Help Tue 2:35 e ction Test ati O CLOSE Question: 10 of 25 Time Reming 162 TOOLS+ SHOW HOT FLAC A nurse is caring for a client who is about to begin taking metronidazole to treat an anaerobic intra-abdominal bacterial infection. The nurse should recognize that cauti...