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:
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.
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.