线性表(顺序表)的销毁问题

hyde100 2014-08-27 02:11:39
各位,以下语句在做线性表(顺序表)销毁的时候, 执行DestoryList函数程序直接包括掉,不知是什么原因!

Status DestoryList(SqList *L)
{
if(L) //这里改成L->data也没有用
{
free(L); //这里改成L->data也没有用
L = NULL; //这里改成L->data也没有用
return OK;
}
else
return ERROR;
}


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

#define MAXSIZE 20 /*存储空间初始分配量*/
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int Status; /*Status是函数的类型,其值是函数结果状态代码,如OK等*/
typedef int ElemType; /*ElemType类型根据实际情况而定,这里假设为int*/
typedef struct
{
ElemType data[MAXSIZE]; /*数组存储数据元素,最大值为MAXSIZE*/
int length; /*线性表当前长度*/
}SqList;

/*操作结果:构造一个空的线性表*/
Status InitList(SqList *L)
{
L->length = 0;
return OK;
}

/*初始条件:线性表L已存在*/
/*操作结果:销毁线性表L*/
Status DestoryList(SqList *L)
{
if(L)
{
free(L);
L = NULL;
return OK;
}
else
return ERROR;
}

/*初始条件:线性表L已存在*/
/*操作结果:将L重置为空表*/
Status ClearList(SqList *L)
{
L->length = 0;
return OK;
}

/*初始条件:线性表L已存在*/
/*操作结果:若L为空表,则返回TRUE,否则返回FALSE*/
Status ListEmpty(SqList L)
{
if(L.length == 0)
return TRUE;
else
return FALSE;
}

/*初始条件:顺序线性表L已存在,1≤i≤ListLength(L)*/
/*操作结果:用e返回L中第i个数据元素的值*/
Status GetElem(SqList L,int i,ElemType *e)
{
if(L.length == 0 || i < 1 || i > L.length)
return ERROR;
*e = L.data[i-1];
return OK;
}

int main()
{
SqList L;
ElemType e;//元素值
Status s;//返回状态
int i;//元素位序

i = 10;
s = InitList(&L);
s=DestoryList(&L);
//s = GetElem(L,i,&e);
}
...全文
3450 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
黑色价值 2014-08-27
  • 打赏
  • 举报
回复
推介楼主看看经典的《C语言程序设计语言》,我最近也在学吴伟民 严蔚敏的《数据结构》,推介配套看看高一凡的《数据结构算法实现及解析》用c和c++实现,完全配套《数据结构》,还有就是,对于有些自己掌握不好的函数,最好查查经典的《C语言程序设计语言》,
hope2reality 2014-08-27
  • 打赏
  • 举报
回复
哎,上面插队很严重啊
hope2reality 2014-08-27
  • 打赏
  • 举报
回复
再补充一下: 如果楼主不是用指针,而是直接用SqList L;定义,那么就不用malloc动态分配内存了,所以也就不用free回收内存了,因为此时L是系统自动在栈上分配内存,当此函数结束时,此函数内申明的变量所占空间都被回收,所以变量也就无效了。而malloc是在堆上分配内存,当此函数结束时,堆还是继续存在,所以需要程序员自己用free释放掉内存。 下面贴上非指针的代码,效果和上面指针的一样:
	
        SqList L;
    s = InitList(&L);
	printf("初始化后长度为:%d\n",L.length);
	for (i=0;i<MAXSIZE;i++)
	{
		L.data[i]=i;
	}
	L.length=i;

	for (i=0;i<L.length;i++)
	{
		printf("%d ",L.data[i]);
	}
赵4老师 2014-08-27
  • 打赏
  • 举报
回复
仅供参考
//带表头结点的单向链表
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
struct NODE {
    int          data;
    struct NODE *next;
} H,*head,*p,*q,*s1,*s2,*s3,*s4,*s;
int i,j,k,n,t,m;
int main() {
    srand(time(NULL));

    //填写头节点数据
    H.data=-1;
    H.next=NULL;
    head=&H;

    //创建10个节点的单链表
    p=head;
    for (i=0;i<10;i++) {
        q=(struct NODE *)malloc(sizeof(struct NODE));
        if (NULL==q) return 1;
        q->data=rand()%100;//填写0..99的随机值
        q->next=NULL;
        p->next=q;
        p=q;
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //将值为5的结点插入到单链表的第k个结点前
    k=3;
    n=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        n++;
        if (k==n) {
            q=(struct NODE *)malloc(sizeof(struct NODE));
            if (NULL==q) return 1;
            q->data=5;
            q->next=p->next;
            p->next=q;
            break;
        }
        p=p->next;
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //删除第k个节点
    k=5;
    n=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        n++;
        if (k==n) {
            q=p->next;
            if (q) {
                p->next=q->next;
                free(q);
            }
            break;
        }
        p=p->next;
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //从小到大排序
    for (p=head;p!=NULL && p->next!=NULL;p=p->next) {
        for (q=p->next;q!=NULL && q->next!=NULL;q=q->next) {
            if (p->next->data > q->next->data) {

                //交换data
//              printf("swap %02d %02d\n",p->next->data,q->next->data);
//              t=p->next->data;p->next->data=q->next->data;q->next->data=t;

                //或者

                //交换next
//              printf("swap %02d %02d\n",p->next->data,q->next->data);
                s1=p->next;
                s2=p->next->next;
                s3=q->next;
                s4=q->next->next;

                if (s2!=s3) {
                     p->next=s3;
                    s3->next=s2;
                     q->next=s1;
                    s1->next=s4;
                } else {
                     p->next=s3;
                    s3->next=s1;
                           q=s3;
                    s1->next=s4;
                }

                //输出整个单链表
//              s=head->next;
//              while (1) {
//                  if (NULL==s) {
//                      printf("\n");
//                      break;
//                  }
//                  printf("%02d->",s->data);
//                  s=s->next;
//              }
//              getchar();
            }
        }
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //将单链表中前 m 个结点和后 n 个结点进行互换,m+n为链表总长10
    m=4;
    n=6;
    k=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        k++;
        if (m+1==k) {
            q=p;
        }
        s=p;
        p=p->next;
    }
    s1=head->next;
    head->next=q->next;
    s->next=s1;
    q->next=NULL;

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //释放所有节点
    p=head->next;
    while (1) {
        if (NULL==p) {
            break;
        }
        q=p->next;
        free(p);
        p=q;
    }

    return 0;
}
//18->94->58->17->27->20->43->57->75->78->
//18->94->05->58->17->27->20->43->57->75->78->
//18->94->05->58->27->20->43->57->75->78->
//05->18->20->27->43->57->58->75->78->94->
//43->57->58->75->78->94->05->18->20->27->
//
hope2reality 2014-08-27
  • 打赏
  • 举报
回复
3楼正解。 如果楼主定义的是指针类型,那么就要用malloc动态分配内存,后面才能用free回收内存。

	SqList *L = (SqList*)malloc(sizeof(SqList));
    s = InitList(L);
	printf("初始化后长度为:%d\n",L->length);
	for (i=0;i<MAXSIZE;i++)
	{
		L->data[i]=i;
	}
	L->length=i;

	for (i=0;i<L->length;i++)
	{
		printf("%d ",L->data[i]);
	}
    s=DestoryList(L);
在mian()函数中加了个测试,可以运行出正确结果。
modyaj 2014-08-27
  • 打赏
  • 举报
回复
引用 7 楼 hyde100 的回复:
[quote=引用 6 楼 modyaj 的回复:] 那样的话 不需要你操作吧 自动销毁
我也有这种考虑,但是数据结构的教材里的ADT里有这个函数,或者是对于这种不是动态分配方式产生的顺序表根本就不需要这个函数?[/quote] 当时那个书我就看得晕乎乎的!有 没关系 空函数吧! 而且重点在于实现 你知道是怎么回事! 人是活的!
707wk 2014-08-27
  • 打赏
  • 举报
回复
神奕 2014-08-27
  • 打赏
  • 举报
回复
引用 7 楼 hyde100 的回复:
我也有这种考虑,但是数据结构的教材里的ADT里有这个函数,或者是对于这种不是动态分配方式产生的顺序表根本就不需要这个函数?
这种情况确实不需要destroy
我还在迷路 2014-08-27
  • 打赏
  • 举报
回复
malloc和free 一定要一一对应,有malloc的地方一定要在某个地方使用free进行释放 使用free之前确定你之前对这个地方进行过malloc操作
hyde100 2014-08-27
  • 打赏
  • 举报
回复
引用 6 楼 modyaj 的回复:
那样的话 不需要你操作吧 自动销毁
我也有这种考虑,但是数据结构的教材里的ADT里有这个函数,或者是对于这种不是动态分配方式产生的顺序表根本就不需要这个函数?
modyaj 2014-08-27
  • 打赏
  • 举报
回复
那样的话 不需要你操作吧 自动销毁
hyde100 2014-08-27
  • 打赏
  • 举报
回复
引用 3 楼 lisong694767315 的回复:
[quote=引用 1 楼 hyde100 的回复:] 有人在吗?我调试了半天,还是奔溃!
在main函数里:

SqList *L = (SqList*)malloc(sizeof(SqList));
// ...
s = InitList(L);
s=DestoryList(L);
[/quote] 但这样会导致我的L变成了指针,那换一个思路,如果不应用动态分配(malloc)的话, 按照我的L声明方式,DestoryList该如何正确书写?
modyaj 2014-08-27
  • 打赏
  • 举报
回复
就是楼上那样 要申请空间才能释放
神奕 2014-08-27
  • 打赏
  • 举报
回复
引用 1 楼 hyde100 的回复:
有人在吗?我调试了半天,还是奔溃!
在main函数里:

SqList *L = (SqList*)malloc(sizeof(SqList));
// ...
s = InitList(L);
s=DestoryList(L);
神奕 2014-08-27
  • 打赏
  • 举报
回复
没有malloc,free必然出错啊。。。
hyde100 2014-08-27
  • 打赏
  • 举报
回复
有人在吗?我调试了半天,还是奔溃!

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧