simple question on linked list insert
From: Digital Puer (digital_puer_at_hotjail.comet)
Date: 01/27/04
- Next message: Dark Alchemist: "Re: stl vector find."
- Previous message: Jerry Coffin: "Re: completed my Varaint class."
- Next in thread: B. v Ingen Schenau: "Re: simple question on linked list insert"
- Reply: B. v Ingen Schenau: "Re: simple question on linked list insert"
- Reply: Leor Zolman: "Re: simple question on linked list insert"
- Reply: Karl Heinz Buchegger: "Re: simple question on linked list insert"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 27 Jan 2004 10:20:24 -0800
Can someone explain why the linked list insertion code (emphasised below)
does not work? It is a function to create a linked list and insert
two elements. The code in question creates the head fine, but
subsequent nodes are not inserted.
/* this function creates a linked list with two integers */
void insert(Node **head, int a, int b)
{
Node *ptr;
*head = make_node(a);
/* this works fine */
ptr = *head;
ptr->next = make_node(b);
/*
BUT THIS CODE DOESN'T -- WHY?????
ptr = (*head)->next;
ptr = make_node(b);
*/
}
/* this function allocates memory for a Node and initialises it */
Node *make_node(int data)
{
Node *node;
node = (Node *)malloc(sizeof(Node));
/* I know, I should check for error here */
node->data = data;
node->next = NULL;
return(node);
}
int main()
{
Node *head;
insert(&head, 10, 20);
print(head);
}
- Next message: Dark Alchemist: "Re: stl vector find."
- Previous message: Jerry Coffin: "Re: completed my Varaint class."
- Next in thread: B. v Ingen Schenau: "Re: simple question on linked list insert"
- Reply: B. v Ingen Schenau: "Re: simple question on linked list insert"
- Reply: Leor Zolman: "Re: simple question on linked list insert"
- Reply: Karl Heinz Buchegger: "Re: simple question on linked list insert"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]