Visual Studio 2008中ç链表增加了尾,混乱
什么我不明白这个链表是if语句的其他部分增加了尾。
当头和尾部被分配了node_temp的内存地址时,尾部和头部都指向相同的内存位置。
但是,在其他部分,头部实际上仍指向尾部。有什么我无法解释,也不了解其他部分?
我希望有人能为我解释得更好。
static struct convert_temp
{
size_t cel;
size_t fah;
struct convert_temp *next;
} *head = NULL, *tail = NULL;
/** Add the new converted temperatures on the list */
void add(size_t cel, size_t fah)
{
struct convert_temp *node_temp = NULL; /* contain temp data */
node_temp = malloc(sizeof(*node_temp));
if(node_temp == NULL)
{
fprintf(stderr, "Cannot allocate memory [ %s ] : [ %d ]\n",
__FUNCTION__, __LINE__);
exit(0);
}
/* Assign data */
node_temp->cel = cel;
node_temp->fah = fah;
node_temp->next = NULL;
if(head == NULL)
{
/* The list is at the beginning */
head = node_temp; /* Head is the first node = same node */
tail = node_temp; /* Tail is also the last node = same node */
}
else
{
/* Append to the tail */
tail->next = node_temp;
/* Point the tail at the end */
tail = node_temp;
}
}
通过它与调试器。 – 2009-12-21 14:03:14