反转链接列表

时间:2012-11-20 08:28:42

标签: c linked-list

  

可能重复:
  Reverse a singly linked list

我查了Reverse a singly linked list链接,但我真的没有得到答案。所以我重新打开这个问题。

我正在尝试撤消链接列表,功能在这里:

void reverse (SLINK list)
{
  NODE *p_tmp_node;
  SLINK p_tmp_list;
  p_tmp_list = list->next;
  list->next = NULL;

  while (NULL != p_tmp_list)
  {
    p_tmp_node = p_tmp_list;
    p_tmp_list = p_tmp_list->next; // A
    p_tmp_node->next = list->next;
    list->next = p_tmp_node;
    //p_tmp_list = p_tmp_list->next; // B
  }
}

我的问题是: 语句A与语句B相同,但如果B执行而不是A,则无法获得预期结果。 A和B之间有什么不同?

节点结构如下:

typedef struct tag_node
{
  int elem;
  struct tag_node *next;
} NODE, *SLINK; 

0 个答案:

没有答案
相关问题