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