I had to initialise the list, before adding to the list.
It's working now.
I'm a bit new to linked lists.
Thanks Paul.
Start a New Thread
> //-------------
> void addItem(struct Node* llist, char* name, unsigned long id) {
> struct Node* newNameNode;
> unsigned char buffer[20];
>
> if (newNameNode = malloc(sizeof(struct Node)))
> {
> strcpy(newNameNode->name, name);
One assumes that newNodeName is not simply a char *? And that strlen(name)
< sizeof(newNameNode->name)?
> newNameNode->id = id;
>
Loop precondition: llist != 0. You did enter with llist != 0?
> while(llist->next != NULL)
> llist = llist->next;
Loop postcondition: llist->next == NULL.
> newNameNode->next = llist->next;
llist->next is NULL, so this is effectively newNameNode->next = 0; Why not
set that up when you set the id above?
> llist->next = newNameNode;
You did enter with llist != 0 didn't you?
--
Paul Curtis, Rowley Associates Ltd
http://www.rowley.co.uk
SolderCore running Defender...
http://www.vimeo.com/25709426
Start a New Thread
I am trying to add a node to a linked list. I have written the code in a
standard commandline application, and it worked.
Then, copied the code over to mspgcc. It is failing to set the pointer of the
last node to the new node.
llist->next = newNameNode;
It causes the msp to stop, and fails to write "add Node - finished" to the lcd
display.
Any help appreciated.
//-------------
void addItem(struct Node* llist, char* name, unsigned long id)
{
struct Node* newNameNode;
unsigned char buffer[20];
if (newNameNode = malloc(sizeof(struct Node)))
{
strcpy(newNameNode->name, name);
newNameNode->id = id;
while(llist->next != NULL)
llist = llist->next;
newNameNode->next = llist->next;
llist->next = newNameNode;
}
else
LCD_WriteString("Out of Memory", 7,0);
LCD_WriteString("add Node - finished", 7,0);
}
Start a New Thread