【发布时间】:2020-08-16 11:34:15
【问题描述】:
我编写了一个反转列表的方法,但结果列表仍然为空。帮助我们了解问题所在。
反转列表的方法:
void reverseList(pLIST pL){
pNODE pN = pL->top;
pLIST pLReverse = createList();
while(pN){
pNODE pNew = malloc(sizeof(NODE));
pNew->value = pN->value;
if(!pLReverse->top){
pNew->next = NULL;
pLReverse->top = pNew;
}
else{
pNew->next = pLReverse->top;
pLReverse->top = pNew;
}
pN = pN->next;
}
showList(pLReverse);
}
列表的结构:
typedef struct Node{
int value;
struct Node * next;
} NODE, *pNODE;
typedef struct List{
int len;
pNODE top;
} LIST, *pLIST;
打印列表的方法:
void showList(pLIST pL){
if(isEmpty(pL)) printf("Empty\n");
else{
pNODE temp = pL->top;
printf("Length: %d\n", pL->len);
while(temp){
printf("Pointer: %p\tValue: %d\tNext pointer: %p\n", temp, temp->value, temp->next);
temp = temp->next;
}
}
}
【问题讨论】:
-
请提供minimal verifiable example。比如
createList是如何实现的,reverseList是如何调用的,原始列表是如何构造的?此外,您是否进行了任何调试,以便在调试器中运行程序和/或添加更多调试打印以跟踪程序执行? -
你没有增加
pLReverse->len -
谢谢,你的评论有帮助,我不专心
-
@Макс 不需要将列表创建为指针 pLIST pLReverse = createList();我怀疑返回指向动态分配列表的指针的函数 createList 没有意义..
标签: c linked-list reverse singly-linked-list function-definition