Normally you'd (probably[1]) use a pointer to a pointer (**) in the function, something like:
Code:
struct node *addNode(struct Node **head, int value) {}
because you have to be able to change what head points to in the case of an empty list; i.e. in an empty list head is pointing to NULL so you need to be able to update what head points to in that situation. Another way is to use a struct. Maybe you're just having trouble understanding the Wikipedia example because the example there is using names that don't match up with your current code (or my thinking for that matter.... their use of 'temp' is weird because it's in no way temporary it just hasn't been added to the list yet). I'll use a struct so that you don't have to worry about pointers to pointers for now. Anyway since the Wikipedia example might be confusing I've written the exact same code using variable names closer to what you've used
Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int n;
struct node *next;
};
struct list
{
struct node *head; /* AKA 'first' */
};
void show(const struct list *mylist)
{
struct node *current;
for (current = mylist->head; current != NULL; current = current->next) {
printf("%d\n", current->n);
}
}
/* This function is identical to the Wikipedia pseudocode you provided
*/
struct node *addNode(struct list *mylist, int value)
{
struct node *newnode;
newnode = malloc(sizeof *newnode);
/* Should add error checking here to ensure malloc() succeeded */
newnode->next = NULL;
newnode->n = value;
if (mylist->head == NULL) { /* If list is empty then add at head */
mylist->head = newnode;
} else { /* otherwise find the tail (last node) of the list and add there */
/* find the tail, AKA 'last' */
struct node *p = mylist->head;
while (p->next != NULL) {
p = p->next;
}
/* p now points to the tail of the list. Add our new node here. p->next is currently NULL so change it to point to our new node */
p->next = newnode;
}
/* return a pointer to the newnode so the calling function can use it if it wants to.
* You could return NULL if the memory wasn't allocated to let the calling function
* know (see comment about about checking return value of malloc())
*/
return newnode;
}
int main(void)
{
struct list mylist;
mylist.head = NULL; /* Init to be an empty list */
addNode(&mylist, 5);
addNode(&mylist, 10);
show(&mylist);
/* ADD a function here to delete all the nodes in the list */
return 0;
}
Edit: Since I'd mentioned the for loop way, here is the alternative code that uses a for loop. As you can see it's essentially the same as in show (it traverses the list in the same way, it just doesn't print the value on ->n along the way)
Code:
struct node *addNode(struct list *mylist, int value)
{
struct node *newnode;
newnode = malloc(sizeof *newnode);
/* Should add error checking here to ensure malloc() succeeded */
newnode->next = NULL;
newnode->n = value;
if (mylist->head == NULL) { /* If list is empty then add at head */
mylist->head = newnode;
} else { /* otherwise find the tail (last node) of the list and add there */
/* find the tail, AKA 'last' */
struct node *p;
for (p = mylist->head; p->next != NULL; p = p->next)
;
/* p now points to the tail of the list. Add our new node here. p->next is currently NULL so change it to point to our new node */
p->next = newnode;
}
/* return a pointer to the newnode so the calling function can use it if it wants to.
* You could return NULL if the memory wasn't allocated to let the calling function
* know (see comment about about checking return value of malloc())
*/
return newnode;
}
I should add that it's relatively rare that I write working code in response to questions, but I think that the example code in the Wikipedia article is somewhat misleading (as mentioned). You went to the effort of at least reading the page, which many people don't seem to even bother doing, so I decided to use what I hope are better variable names. Aside from the naming of the variables the code is exactly the same but I hope it helps
[1] I say probably because there are other ways to implement linked lists and if you were using a dummy node -- which can eliminate the need to deal with empty lists separately -- then you'd be doing things differently (not passing pointers to pointers)