Linked list debug help

Status
Not open for further replies.

hanser

Ars Legatus Legionis
43,847
Subscriptor++
I having a bit of trouble getting this to work, and I'm not sure why. The idea is to insert a new node at the beginning of the linked list:

Code:
void InsertNewFirstNode(char *A, NodeType **L)  
/* L holds the address of a location containing */
/* a pointer to the first node of the list */
{	
	NodeType *NewNode;<P>	NewNode = malloc(sizeof(NodeType));				/* Allocate memory for a new node */
	strcpy(NewNode->Airport, A);					/* Copy necessary string information */
	PrintList(NewNode);
	NewNode->Link = L;						/* Set new node's link to address of the new list */
	printf("%lX",&NewNode->Link);
}

However that generates this output:

> ./a.out
(DUS, )
140002108()
(ORD, )
140002128()
(SAN, )
140002148()
(BRU, )
140002168()
(MEX, )
140002188()
(MIA, )
1400021A8()
()

The call to InsertNewFirstNode() is as follows:

InsertNewFirstNode("DUS",&L);
PrintList(L);
InsertNewFirstNode("ORD",&L);
PrintList(L);
InsertNewFirstNode("SAN",&L);
PrintList(L);
InsertNewFirstNode("BRU",&L);
PrintList(L);
InsertNewFirstNode("MEX",&L);
PrintList(L);
InsertNewFirstNode("MIA",&L);
PrintList(L);


Obviously there is a logic error, and I am fairly certain it's in InsertNewFirstNode().

I think I'm creating the new node, and not attaching it properly to the front of the list, and then I'm loosing the rest of the list somewhere in memory.
 
What you need to do is:
1) Set your new node's link to the head of the old list
2) Set the head of the list to your new node

This translates into:

NewNode->Link = *L;
*L = NewNode;

I think you need to study indirection better :cool:

Edit: Your problem is mostly that you're not dereferencing L to access the actual NodeType*, and that you're not changing the head of the list.
 

hanser

Ars Legatus Legionis
43,847
Subscriptor++
OK, so I have another question now. I want to delete a first node. The first thing I want to do is declare a new NodeType pointer that will be used temporarily.

I set this new pointer to List. I can then detach the rest of the list.


code:<HR>
Code:
temp = List;						/* Initialize a pointer (temp) */
*List.Link = (*List)->Link;					/* Detach the first node */
free(temp);

which then does:

bash-2.02$ gcc airports.c
airports.c: In function `DeleteFirstNode':
airports.c:79: warning: assignment from incompatible pointer type
airports.c:80: request for member `Link' in something not a structure or union
 
Yeah but does that work? Somehow making your list point to something you're going to free on the next line scares me... :p

You first version was almost correct. I believe it should look like:
code:<HR>
Code:
temp = *List;						/* Initialize a pointer (temp) */
*List = (*List)->Link;					/* Detach the first node */
free(temp);
The change being on the first and second line. It's the list head you want to change to point to a new node, not the list head's link pointer. Also, temp and List have different indirection levels, hence the need to dereference List to get a pointer to the node to be deleted.

Edit: alternatively, this might be more clear:
code:<HR>
Code:
temp = *List;
*List = temp->Link;
free(temp);
 
Status
Not open for further replies.