标题:大神给看看,关于列表删除函数的问题。
只看楼主
YoleBooM
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2021-8-25
结帖率:0
已结贴  问题点数:20 回复次数:1 
大神给看看,关于列表删除函数的问题。
现在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

小弟我看了一天了,也没查出毛病。望大神相助,抱拳~!
搜索更多相关主题的帖子: add next data 删除 printf 
2021-08-25 10:25
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:10 
void DestroyList(LinkList &L)
你这是C++代码了吧,那为什么不直接用 std::list ?

还有好多前言不搭后语的地方,懒得问,懒得说了,给你一段C语言代码吧(记住是C语言代码,C++无法编译通过)
程序代码:
#include <stdio.h>
#include <stdlib.h>

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

void DestroyList( struct LNode* head )
{
    for( ; head; )
    {
        printf( " %d ->", head->data );

        struct LNode* tmp = head;
        head = head->next;
        free( tmp );
    }
    printf( " NULL\n" );
}

int main( void )
{
    struct LNode* p = malloc( sizeof(struct LNode) );
    p->data = 0;
    p->next = malloc( sizeof(struct LNode) );
    p->next->data = 1;
    p->next->next = malloc( sizeof(struct LNode) );
    p->next->next->data = 2;
    p->next->next->next = malloc( sizeof(struct LNode) );
    p->next->next->next->data = 3;
    p->next->next->next->next = malloc( sizeof(struct LNode) );
    p->next->next->next->next->data = 4;
    p->next->next->next->next->next = malloc( sizeof(struct LNode) );
    p->next->next->next->next->next->data = 5;
    p->next->next->next->next->next->next = malloc( sizeof(struct LNode) );
    p->next->next->next->next->next->next->data = 6;
    p->next->next->next->next->next->next->next = malloc( sizeof(struct LNode) );
    p->next->next->next->next->next->next->next->data = 7;
    p->next->next->next->next->next->next->next->next = malloc( sizeof(struct LNode) );
    p->next->next->next->next->next->next->next->next->data = 8;
    p->next->next->next->next->next->next->next->next->next = malloc( sizeof(struct LNode) );
    p->next->next->next->next->next->next->next->next->next->data = 9;
    p->next->next->next->next->next->next->next->next->next->next = NULL;

    DestroyList( p );
}

2021-08-25 11:13
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:10 
#include <stdio.h>

typedef struct LNode
{
    int data;
    struct LNode *next;
}*LinkList;

void PlayList(LinkList L)
{
    LinkList q = L;

    printf("L = %p\n", L);
    while(q) {
#define USER_VAL 2
//destroy node
        if (q->data == USER_VAL) {
            ((LinkList)&q[-1])->next = q->next;
            q->next = 0;
            q = q - 1;
        } else
        printf("L[%d]->data = %d, L[%d]->next = %p\n",
                q->data,  q->data, q->data, q->next);
        q = q -> next;
    }
}
int main(int argc, char *argv[])
{
    struct LNode L[] = {
        0, (LinkList)(L + 1),
        1, (LinkList)(L + 2),
        2, (LinkList)(L + 3),
        3, (LinkList)(L + 4),
        4, (LinkList)(L + 5),
        5, 0
    };
   
    PlayList(L);
   
    return 0;
}
//online parser: https://www.bccn.net/run/
L = 0x7ffc2db0caa0
L[0]->data = 0, L[0]->next = 0x7ffc2db0cab0
L[1]->data = 1, L[1]->next = 0x7ffc2db0cac0
L[3]->data = 3, L[3]->next = 0x7ffc2db0cae0
L[4]->data = 4, L[4]->next = 0x7ffc2db0caf0
L[5]->data = 5, L[5]->next = (nil)

[此贴子已经被作者于2021-8-25 12:01编辑过]

2021-08-25 11:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-506639-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.058042 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved