2013-01-08 82 views
2

我有一个链表,现在我想穿起来走路每个我在它前进时前和节点,我当前节点移动到列表的开头是这样的:插入在链表

4 5 3 2 7 
5 4 3 2 7 
3 5 4 2 7 
2 3 5 4 7 
7 2 3 5 4 

代码:

#include <stdio.h> 
#include <stdlib.h> 

typedef struct _node { 
    int p; 
    struct _node *next; 
} node; 

main(int argc, char **argv) 
{ 
    int i, n; 
    node *nod = NULL; 
    node *nod2 = NULL; 
    node *nod_tmp = NULL; 

    node *nod_next = NULL; 
    node *nod_before = NULL; 

    printf("Enter n: "); 
    scanf("%d", &n); 
    for(i = 0; i < n; ++i) 
    { 
     nod_tmp = (node *)malloc(sizeof(node)); 
     scanf("%d", &nod_tmp->p); 
     if (i == 0) 
     { 
      nod = nod2 = nod_tmp; 
     } 
     nod2->next = nod_tmp; 
     nod2 = nod_tmp; 
    } 

    nod_tmp = nod; 
    while (nod_tmp->next != NULL) 
    { 
     nod_before = nod_tmp; // save current node 
     nod_next = nod_tmp->next->next; // save next node 
     nod_before->next = nod_next; // link the previous with the next one 
     nod_tmp->next = nod; // point the current node to the beginning of list 
    } 

    while(nod != NULL) 
    { 
     printf("%d\n", nod->p); 
     nod = nod->next; 
    } 
    return 0; 
} 
+4

你的问题是什么? – prprcupofcoffee

+1

您不应该在全局名称空间中使用具有前导下划线的名称,因为此类名称是由标准保留的。 –

+0

所以..你想扭转一个链表? – WhozCraig

回答

2
nod_tmp = nod->next; 
nod_before= nod; 
while (nod_tmp != NULL) 
{ 
    nod_before->next = nod_tmp->next; 
    nod_tmp->next = nod; 
    nod = nod_tmp; 
    nod_tmp = nod_before->next; 
} 

将您的第二个循环更改为上面的循环。请注意,node_tmp指向头后的第二个节点,而nod_before指向第一个节点。这样你可以避免无限循环。

0

Inifinite循环就在这里:

while (nod_tmp->next != NULL) 
    { 
     nod_before = nod_tmp; // save current node 
     nod_next = nod_tmp->next->next; // save next node 
     nod_before->next = nod_next; // link the previous with the next one 
     nod_tmp->next = nod; // point the current node to the beginning of list 
    } 
2

认真想想while循环扭转你的LIS t实际上在做。例如,假设我们有这个名单:

1 --> 2 --> 3 -->null 

让你走,而循环代码:

nod_tmp = nod; // nod and nod_tmp now point to 1 
while (nod_tmp->next != NULL) 
{ 
    nod_before = nod_tmp; // nod_before, nod_tmp, and nod all point to 1 
    nod_next = nod_tmp->next->next; // nod_next points to 3 
    nod_before->next = nod_next; // 1-->3 
    nod_tmp->next = nod; // 1-->1 
} 

的第一个节点(由nod_tmp仍引用)现在指向自身。这不仅会导致您的while-condition无限旋转,还会泄漏列表中的其余内存。

0
#include <stdio.h> 
#include <stdlib.h> 

typedef struct _node { 
    int p; 
    struct _node *next; 
} node; 

main(int argc, char **argv) 
{ 
    int i, n; 
    node *nod = NULL; 
    node *nod2 = NULL; //header of the linked list 
    node *nod_tmp = NULL; 

    printf("Enter n: "); 
    scanf("%d", &n); 
    for(i = 0; i < n; i++) 
    { 
     nod_tmp = (node *)malloc(sizeof(node)); 
     scanf("%d", &nod_tmp->p); 
     nod_tmp->next = nod2; 
     nod2 = nod_tmp; 
    } 

    nod = nod2; 

    printf("Linked list order before reversing: "); 
    while(nod != NULL) 
    { 
     printf("%d ", nod->p); 
     nod = nod->next; 
    } 
    printf("\n"); 

    nod = nod2; 
    while (nod!=NULL && nod->next != NULL) 
    { 
     nod_tmp = nod2; 
     nod2 = nod->next; 
     nod->next = nod2->next; 
     nod2->next = nod_tmp; 
    } 

    nod = nod2; 

    printf("Linked list order after reversing: "); 
    while(nod != NULL) 
    { 
     printf("%d ", nod->p); 
     nod = nod->next; 
    } 
    printf("\n"); 
    return 0; 
}