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
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#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;
}

Add a comment
Know the answer?
Add Answer to:
Write a C++ function to add a node to the beginning of a linked list. Your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A linked list of integers is built using the following struct: struct node {   int data;...

    A linked list of integers is built using the following struct: struct node {   int data;   struct node *next; }; Define a function named max that returns the maximum integer in a list. The function takes one arguments, a pointer to the head of the list. The function returns an integer, which is the maximum value. If the list is empty, return zero. NOTE: You know nothing about the values in the list. They could all be negative!

  • Define a struct Node to represent a node in a double linked-list that stores a string...

    Define a struct Node to represent a node in a double linked-list that stores a string as data. Write a function to insert a node to the head of the linked list. The function takes two arguments: a pointer to the first node in the double linked list and a string value. It should create a new node with the given value to the head of the double linked list.

  • C LANGUAGE I just need the void push() function, which inserts new node to the front...

    C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • C++ Consider the following structure of node and linked list. struct Node { int key; Node...

    C++ Consider the following structure of node and linked list. struct Node { int key; Node *next; }; 10 -> 20 -> 30 -> 10 -> 10 -> 50 -> 10 -> NULL What will be the output of following pseudo-code? Consider head is the pointer to the first node of above linked list. Node *walker = head; int count = 0; while(walker!= NULL && count < 3) { if(walker->key == 10) { count = count + 1; } walker...

  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

  • Structure struct Node int Manth; // Mont h double dAvg: 1/ Average struct Node pNext // with, the linked İist 3hown above the function will return gven that the average is 3.8 Ptr to next -Nod...

    Structure struct Node int Manth; // Mont h double dAvg: 1/ Average struct Node pNext // with, the linked İist 3hown above the function will return gven that the average is 3.8 Ptr to next -Node; Ret (3,3.8) (4,2.5) (20pts)( Recursive function) Show the code for a function that receives a pointer to the head of an ordered singly linked list that uses the structure in the top left. The function will return the pointer node that shows the highest...

  • Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that...

    Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that holds ints instead of strings. In your main, insert 25 to 75 random integers with range (0 to 100) into a linked list of your nodes. Use a random to determine how many to make. Write a function int sum(NodePointer current) that returns the sum of the integers in the list by looping through the linked list. Write a function int count(NodePointer current) that...

  • 5.8 Sort Linked List Use mergersort to sort a linked list Do not use the following...

    5.8 Sort Linked List Use mergersort to sort a linked list Do not use the following libraries: algorithm, cmath. Do not load the values into a vector and sort them. The sort must be done with a linked list Example Two lists 6->3->1->2->4->5 will return a list with 1->2->3->4->5->6. Input There will be no input read in anymore. Going forward use the hard-coded input on the template and ensure the program works. There will be one compare output test based...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT