首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

链表销毁有关问题?着

2013-03-12 
链表销毁问题?--在线等着!运行到销毁链表函数时就出错,调试不出问题根源!请帮忙看看!谢谢!#include stdio

链表销毁问题?--在线等着!
运行到销毁链表函数时就出错,调试不出问题根源!请帮忙看看!谢谢!链表销毁有关问题?着

#include <stdio.h>
#include <malloc.h>

#define ARRAY_LENGTH 5    // 指定链表结点数最大值

// 需要输入的数据的类型的别名
typedef int ElemType;

// 函数返回值,表状态
typedef int Status;

// 链表结构体
typedef struct LNode
{
    ElemType data;
struct LNode *next;
}LNode, *LinkList;

// 头插法创建链表
Status CreateList(LNode *pHead, ElemType arrDatas[], int nLength);

// 按值查找
Status SearchList(LNode head, ElemType key, LNode *pResult);

// 遍历链表值
void DisplayListNodes(LNode head);

// 释放链表
void DestroyList(LNode *pHead);

int main()
{
LNode head;
LNode *pFound = NULL;      // 若按值查找找到值,则存放该结点的地址,否则为NULL
int nLength = 0;           // 指定链表结点数
int i = 0;
ElemType key;              // 需要查找的值

    ElemType arrDatas[ARRAY_LENGTH];   // 存放链表值的数据
printf("输入多少数据?至多输入%d个:  ", ARRAY_LENGTH);
do 
{   // 链表结点数,直到输入的值合法
scanf("%d", &nLength);
} while (nLength > ARRAY_LENGTH || nLength < 0);

printf("输入%d个数据:", nLength);
for (i = 0; i < nLength; ++ i)
{
scanf("%d",&arrDatas[i]);
}

// 创建链表,若为NULL,则表示创建失败
if (!CreateList(&head, arrDatas, nLength))
{
printf("创建链表失败!");
return 0;
}

// 打印链表结点的值
DisplayListNodes(head);
 
    printf("输入要查找的值:");
scanf("%d", &key);

// 若查找成功,则把找到的值所在的结点地址给pFound,否则为NULL
if (SearchList(head, key, pFound))
printf("Found!");
else
printf("Not Found!");

// 销毁链表,释放内存
DestroyList(&head);
    
return 0;
}

/************************************************************************/
/* 函数名:  CreateList
/* 前置条件:pHead指示的链表为空,数组arrDatas不为空,nLength是不为0的整数
/* 返回值:  返回创建好的链表的头指针,以及指示状态Status
             若创建成功,返回1,否则,返回0.
/* 参数:    pHead--指示链表的头指针 
             arrDatas--存放链表结点数据的ElemType型数组
 nLength--指示链表结点个数的int型数值
/* 实现者:  HuangYiBiao
/************************************************************************/
Status CreateList(LinkList pHead, ElemType arrDatas[], int nLength)
{
LinkList pTail = pHead;
LinkList pCur;
int i = 0;

//创建头结点
pHead = (LinkList)malloc(sizeof(LNode));
pHead->next = NULL;

while (i < nLength)
{
pCur = (LinkList)malloc(sizeof(LNode));
if (!pCur)
{
printf("申请空间失败!");
return 0;
}
        pCur->data = arrDatas[i++];
pTail->next = pCur;
pTail = pCur;


}
pTail->next = NULL;

return 1;
}

/************************************************************************/
/* 函数名:  DisplayListNodes
/* 前置条件:链表已经存在
/* 返回值:  void
/* 参数:    head--指示链表的头结点
/* 实现者:  HuangYiBiao
/************************************************************************/
void DisplayListNodes(LNode head)
{
LinkList pCur = &head;

while (pCur->next)
{
        printf("%d\n", pCur->next->data);
pCur = pCur->next;
}
}

/************************************************************************/
/* 函数名:  SearchList
/* 前置条件:pHead指示的链表已存在,key值合法
/* 返回值:  返回创建好的链表的头指针,以及指示状态Status
             若创建成功,返回1,否则,返回0.
/* 参数:    head--指示链表的头结点
             key--待查找的ElemType类型的值
 pResult--若查找成功,则用来存放该结点的地址,否则为NULL
/* 实现者:  HuangYiBiao
/************************************************************************/
Status SearchList(LNode head, ElemType key, LNode *pResult)
{
LinkList pCur = head.next;

while (pCur && pCur->data != key)
{
pCur = pCur->next;
}

if (!pCur) //找不到
{
pResult = NULL;
return 0;
}
else
{
pResult = pCur;
}

return 1;
}

/************************************************************************/
/* 函数名:  DestroyList
/* 前置条件:链表已经存在
/* 返回值:  void
/* 参数:    pHead--指示链表的头指针
/* 实现者:  HuangYiBiao
/************************************************************************/
//方式1
/*void DestroyList(LNode *pHead)
{
LinkList pCur = pHead->next;

while (pCur)
{
pHead->next = pCur->next;
free(pCur);
pCur = pHead->next;
}
free(pHead);
}*/
/*方式2
void DestroyList(LNode *pHead)
{
LinkList pCur = pHead->next;

free(pHead);

while (pCur)
{
        pHead = pCur;
pCur = pCur->next;
free(pHead);
}
}
*/

void DestroyList(LNode *pHead)
{
LinkList pCur = NULL;
int nCount = 0;

while (pHead)
{
pCur = pHead;
pHead = pHead->next;
free(pCur);
nCount++;
}
printf("销毁了%d个结点", nCount);
}

c
[解决办法]
Status CreateList(LinkList pHead, ElemType arrDatas[], int nLength)
CreateList(&head, arrDatas, nLength)
这里的意思其实是
LinkList pHead = &head;
pHead = (LinkList)malloc(sizeof(LNode));
你只是改变了pHead的指向 没有修改head的内容
其实你这里就内存泄露了 

后面的你的删除
从head节点开始 而你的head节点不是malloc处理的 是直接定义得到的 不能被free的

你的链表结构的这样的
第一个head 无效数据 只做链表头 栈分配的内存
其他节点 有效数据 堆分配的内存

热点排行