大家好,我有个很棘手的问题,帮忙看下,能编译通过但是不能运行

mr_caoke 2008-09-17 08:55:37


LinkList.h

typedef struct LNode
{
int nData;
LNode *pNext;
}LinkList;

bool InitLinkList(LinkList *L, const int arrData[], const int nLength);
bool DisplayLinkList(const LinkList *L);




LinkList.cpp

#include <iostream>
#include "LinkList.h"
using namespace std;

bool InitLinkList(LinkList *L, const int arrData[], const int nLength)
{

L = (LinkList *)malloc(sizeof(LinkList));
L->pNext = NULL;
LNode *pNode = NULL;

for (int i = 0; i < nLength; i++)
{
pNode = (LNode *)malloc(sizeof(LNode));

if (NULL == pNode)
{
printf("InitList Failed!");
return false;
}

pNode->nData = arrData[i];
pNode->pNext = L->pNext;
L->pNext = pNode;
}

return true;
}


bool DisplayLinkList(const LinkList *L)
{

if (NULL == L->pNext)
{
return false;
}

LNode *pNode = L->pNext;


while (NULL != pNode->pNext) //就到这里就过不去了
{
cout << 1 << endl;

cout << pNode->nData << ",";
pNode = pNode->pNext;
}

cout << pNode->nData << endl;
return true;

}






main.cpp

#include "LinkList.h"

int main(int argc, char* argv[])
{
LinkList List;
int Array[10] = {2, 3, 5, 8, 9, 11, 34, 101, 66, 90};

if( !InitLinkList(&List, Array, sizeof(Array)/sizeof(int)) )
{
return -1;
}

if (!DisplayLinkList(&List))
{
return -1;
}

return 0;
}



这个是一个简单的链表程序,可以编译通过,但是无法运行,问题出在DisplayLinkList这个函数,
哪位大侠帮忙解释一下为啥,谢谢了






...全文
79 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsmdiao0812 2008-09-17
  • 打赏
  • 举报
回复
L = (LinkList *)malloc(sizeof(LinkList));
L已经重新指向了一片内存,建议看看effectivec+++,讲的很详细
ding1188 2008-09-17
  • 打赏
  • 举报
回复
bool InitLinkList(LinkList *L, const int arrData[], const int nLength);

改为
bool InitLinkList(LinkList *&L, const int arrData[], const int nLength);
hooked 2008-09-17
  • 打赏
  • 举报
回复
就多了一句话
L = (LinkList *)malloc(sizeof(LinkList));
wangdeqie 2008-09-17
  • 打赏
  • 举报
回复

//LinkList.cpp

#include <iostream>
#include "LinkList.h"
using namespace std;

bool InitLinkList(LinkList *L, const int arrData[], const int nLength)
{

L->pNext = NULL;
LNode *pNode = NULL;

for (int i = 0; i < nLength; i++)
{
pNode = (LNode *)malloc(sizeof(LNode));

if (NULL == pNode)
{
printf("InitList Failed!");
return false;
}

pNode->nData = arrData[i];
pNode->pNext = L->pNext;
L->pNext = pNode;
}

return true;
}


bool DisplayLinkList(const LinkList *L)
{

if (NULL == L->pNext)
{
return false;
}

LNode *pNode = L->pNext;


while (NULL != pNode->pNext) //就到这里就过不去了
{
cout << 1 << endl;

cout << pNode->nData << ",";
pNode = pNode->pNext;
}

cout << pNode->nData << endl;
return true;

}

greatws 2008-09-17
  • 打赏
  • 举报
回复
最后没有free哦,内存泄露
greatws 2008-09-17
  • 打赏
  • 举报
回复

bool InitLinkList(LinkList *L, const int arrData[], const int nLength)
{

//L = (LinkList *)malloc(sizeof(LinkList)); //已经是对象了,还要分配干嘛?
L->pNext = NULL;
LNode *pNode = NULL;
LNode *pLast = L;


for (int i = 0; i < nLength; i++)
{
pNode = (LNode *)malloc(sizeof(LNode));

if (NULL == pNode)
{
printf("InitList Failed!");
return false;
}

pNode->nData = arrData[i];
pNode->pNext = NULL;
//pNode->pNext = L->pNext;
//L->pNext = pNode; //原地踏步?
pLast->pNext = pNode;
pLast = pNode;

}

return true;
}

oyster2008 2008-09-17
  • 打赏
  • 举报
回复
你的initlinklist这个函数参数传递不正确
第一个参数应该传递LinkList**
nipa1989 2008-09-17
  • 打赏
  • 举报
回复
有点麻烦

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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