我写了一个反转列表的方法,但是结果列表仍然是空的。帮助我们了解问题所在。
反转列表的方法:
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;
}
}
}