注册 登录
编程论坛 数据结构与算法

关于列表删除函数的问题。

YoleBooM 发布于 2021-08-25 10:31, 3301 次点击
大神给看看,关于列表删除函数的问题。
现在L为头节点,往下一共储存了9个数据

1,2,3,4,5,6,7,8,9

但是执行结果到第九个循环就卡住了,而且数据9也丢失了,
本人新人,刚刚开始学习数据结构,望各位前辈给予帮助,抱拳了~!


struct LNode
{
    ElemType data;
    LNode * next;
};

typedef LNode * LinkList;

void DestroyList(LinkList &L)
{
    LinkList q;
   
    int i = 0;

    for( L; L != NULL; i++ )
    {
        printf(" 第 %d 个", i);
        printf(" L -> data = %d \n", L -> data);
        q = L -> next;
        printf(" q = L->next add = %u \n", q);
        free(L);

        printf("freed L add = %u \n", L);
        L = q;
        printf(" L = q add = %u \n", L );
        printf("\n");
        
    }
    printf(" \n\n");
   
}

这是编译器运行结果:
L -> data = 0
 L -> next -> data = 1
 L -> next-> next -> data = 2
 L -> next-> next -> next -> data = 3
 L -> next-> next -> next -> next -> data = 4
 L -> next-> next -> next -> next -> next -> data = 5
 L -> next-> next -> next -> next -> next -> next -> data = 6
 L -> next-> next -> next -> next -> next -> next -> next -> data = 7
 L -> next-> next -> next -> next -> next -> next -> next -> next -> data = 8
 L -> next-> next -> next -> next -> next -> next -> next -> next -> next -> data = 9
 L -> next-> next -> next -> next -> next -> next -> next -> next -> next -> next = 0
 
 第 0 个 L -> data = 0
 q = L->next add = 1681938784
freed L add = 1681938768
 L = q add = 1681938784

 第 1 个 L -> data = 1
 q = L->next add = 1681938800
freed L add = 1681938784
 L = q add = 1681938800

 第 2 个 L -> data = 2
 q = L->next add = 1681938816
freed L add = 1681938800
 L = q add = 1681938816

 第 3 个 L -> data = 3
 q = L->next add = 1681938832
freed L add = 1681938816
 L = q add = 1681938832

 第 4 个 L -> data = 4
 q = L->next add = 1681938848
freed L add = 1681938832
 L = q add = 1681938848

 第 5 个 L -> data = 5
 q = L->next add = 1681938864
freed L add = 1681938848
 L = q add = 1681938864

 第 6 个 L -> data = 6
 q = L->next add = 1681938880
freed L add = 1681938864
 L = q add = 1681938880

 第 7 个 L -> data = 7
 q = L->next add = 1681938896
freed L add = 1681938880
 L = q add = 1681938896

 第 8 个 L -> data = 8
 q = L->next add = 1681938912
freed L add = 1681938896
 L = q add = 1681938912

 第 9 个 L -> data = 0
 q = L->next add = 0

小弟我看了一天了,也没查出毛病。望大神相助,抱拳~!
0 回复
1