-5

I am trying to make a function for inserting at the beginning of a linked list. I had compared my code with some codes on the web and the result is more or less the same. But, when i try to compile it with CodeBlocks, the output that is printed is only the number 0. Can anybody please explain why the insertFirst function doesn't work or show the mistake if there is?

struct node{
    int data;
    struct node *next;
};

node insertFirst(node *head, int newData){
        node *temp = new node;
        temp -> data = newData;
        temp -> next = head;
        head = temp;

        return *head;
}

int main(){
 int i;

 node *first, *cur;

 first = new node;

 first -> data = 0;
 first -> next = 0;

 for(i = 1; i <= 10; i++){
    insertFirst(first, i);
 }

 cur = first;

 while(cur){
    cout << cur -> data << endl;
    cur = cur -> next;
 }
}
Vun
  • 13
  • 4
  • 1
    Did you try to step through your code with a debugger, to figure out where it does something that you didn't expect? Update: I suggest reading a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). `head` in `insertFirst` is a local copy, and any assignments done to it (`head = temp;`) is lost once the function returns. Did you mean to pass by reference? – Algirdas Preidžius Oct 25 '17 at 13:26

3 Answers3

1

your first remains first, because you don't update it in the call. Define your function like this:

node insertFirst(node *&head, int newData)

So you'll get a reference on your "head"-pointer and it's passed back to the calling function.

Jazzco
  • 68
  • 5
  • `node insertLast(node *head, int newData){ node *temp = new node; node *cur,*prev; temp -> data = newData; temp -> next = 0; cur = head; prev = 0; while(cur){ prev = cur; cur = cur -> next; } if(!prev){ head = temp; }else{ prev -> next = temp; } return *head; }` i also have the above function and try to implement it the same way in the looping and it works. what make it different? – Vun Oct 25 '17 at 13:46
  • In your approach you define the parameter `**node *head**`. So you are able to change the data **head** points to but **not** the pointer itself in the caller. A reference to the pointer `**node *&head**` will give you access to the pointer too. It's like other reference definitions: `**node &x**` gives access to the caller variable, `**node x**` doesn't. – Jazzco Oct 27 '17 at 07:28
  • As you pass the content of your node back, you can easily make the following experiment: in your for-loop take the result: `node other = insertFirst(first, i);` and then compare it with the parameter in an additional line inside the for-loop: `cout << i << ": value: " << first->data << " expected: " << other.data << endl;`. With the parameter defined as `node *head` you'll get different values. ;-) – Jazzco Oct 27 '17 at 07:47
  • Now for your question: in your **insertLast**-function `prev` is never null so `head` is never changed. – Jazzco Oct 27 '17 at 08:05
1

I think what SHR wanted to mention is that you change your code such as

node* insertFirst(node *head, int newData){
    node *temp = new node;
    temp -> data = newData;
    temp -> next = head;
    return temp;
}

and later you can call that via

for(i = 1; i <= 10; i++){
    first = insertFirst(first, i);
}

so the function always returns a pointer to the first object in your list.

qdbp
  • 109
  • 1
  • 1
  • 12
0

in node insertFirst(node *head, int newData) head is in parameter, not in out.

therefor in main it not been changed.

in manner to update the first value use first = insertFirst(first, i);

SHR
  • 7,940
  • 9
  • 38
  • 57