链表问题

smdszgzh 2009-02-26 10:04:19

#include <iostream>
#include <string>
using namespace std;
struct Node
{
string ID;
string Name;
Node *pNext;
};
Node* CrateList(int n)//创建n个元素的链表
{
Node* pHead;
Node* pNew;
pNew=new Node();
for(int i(0);i<n;i++)
{
do
{
cout<<"Please input the ID and name"<<endl;
cin>>pNew->ID>>pNew->Name;
if((pNew->Name).size()>10)
cout<<"输出超过10个字符,请重新输入"<<endl;
}while((pNew->Name).size()>10);
if(0==i)
{
pHead=pNew;
}
else
{
pNew=pNew->pNext;
}
}
return pHead;

}

void OutPut(Node *pHead)//输出链表所有元素
{
Node* pNew;
pNew=pHead;
for(;pNew!=NULL;)
{
cout<<pNew->ID<<" "<<pNew->Name<<endl;
pNew=pNew->pNext;
}
}
int main()
{
int i=3;
Node* p=CrateList(i);
OutPut(p);
return 0;
};

运行有错误
...全文
94 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lq651659889 2009-02-26
  • 打赏
  • 举报
回复
多说清楚了,学习学习
sdljgxb 2009-02-26
  • 打赏
  • 举报
回复
学习学习
smdszgzh 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 chevin 的回复:]
稍微修改了下

C/C++ code

pTail->pNext=pNew;
//这里怎么不是pNew=pTail->pNext
pTail=pNew;

[/Quote]
zhh157 2009-02-26
  • 打赏
  • 举报
回复
将楼主的代码修改了一下,应该可以运行了
注释中间为修改部分


#include <iostream>
#include <string>
using namespace std;
struct Node
{
string ID;
string Name;
Node *pNext;
};
Node* CrateList(int n)//创建n个元素的链表
{
Node* pHead;
Node* pNew;

/////////////////////////
Node* pTemp;
//pNew=new Node();
/////////////////////////

for(int i(0);i<n;i++)
{
pNew=new Node();
do
{
cout<<"Please input the ID and name"<<endl;
cin>>pNew->ID>>pNew->Name;
if((pNew->Name).size()>10)
cout<<"输出超过10个字符,请重新输入"<<endl;
}while((pNew->Name).size()>10);
if(0==i)
{
pHead=pNew;
pTemp = pHead;
}
else
{
/////////////////////////
pTemp->pNext = pNew;
pTemp = pTemp->pNext;
//pNew=pNew->pNext;
/////////////////////////
}
}

/////////////////////////
pTemp->pNext = NULL;
/////////////////////////

return pHead;

}

void OutPut(Node *pHead)//输出链表所有元素
{
Node* pNew;
pNew=pHead;
for(;pNew!=NULL;)
{
cout<<pNew->ID<<" "<<pNew->Name<<endl;
pNew=pNew->pNext;
}
}

/////////////////////////
void DeleteList( Node *pHead )
{
//delete
}
/////////////////////////

int main()
{
int i=3;
Node* p=CrateList(i);
OutPut(p);
/////////////////////////
DeleteList( p );
/////////////////////////
getchar();
return 0;
};
Chevin 2009-02-26
  • 打赏
  • 举报
回复
稍微修改了下

#include <iostream>
#include <string>
using namespace std;
struct Node
{
string ID;
string Name;
Node *pNext;
};

Node* CrateList(int n)//创建n个元素的链表
{
Node* pHead;
Node* pTail;
Node* pNew;

for(int i(0);i<n;i++)
{
pNew=new Node();
pNew->pNext=NULL;
do
{
cout<<"Please input the ID and name"<<endl;
cin>>pNew->ID>>pNew->Name;
if((pNew->Name).size()>10)
cout<<"输出超过10个字符,请重新输入"<<endl;
}while((pNew->Name).size()>10);

if(0==i)
{
pHead=pNew;
pTail=pNew;
}
else
{
pTail->pNext=pNew;
pTail=pNew;
//pNew=pNew->pNext;
}
}

return pHead;
}

void OutPut(Node *pHead)//输出链表所有元素
{
Node* pNew;
pNew=pHead;
for(;pNew!=NULL;)
{
cout<<pNew->ID<<" "<<pNew->Name<<endl;
pNew=pNew->pNext;
}
}
int main()
{
int i=1;
Node* p=CrateList(i);
OutPut(p);
return 0;
};

Chevin 2009-02-26
  • 打赏
  • 举报
回复
多创建一个指向最后一个节点的尾指针Node * tail;行不行呢?
让它指向最后一个Node对象,这样比较好操作吧!
smdszgzh 2009-02-26
  • 打赏
  • 举报
回复
在循环里new 也不行!
yupengchen951124 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 waizqfor 的回复:]
C/C++ code
pNew=new Node();



NEW完了 没有delete啊 LZ加一个
[/Quote]

人家是创建一个NODE啊,还没到delete的时候呢
  • 打赏
  • 举报
回复
汗,楼主的create链表函数想一次接收n个节点,但是你又在循环体外面new的,肯定不可以啊.看着好乱,你先把思路清一下.
M_S_D_N 2009-02-26
  • 打赏
  • 举报
回复
Node* CrateList(int n)//创建n个元素的链表
只new了一次,却想创建n个Node,必定崩掉

cin>>pNew->ID>>pNew->Name;............
waizqfor 2009-02-26
  • 打赏
  • 举报
回复

pNew=new Node();

NEW完了 没有delete啊 LZ加一个

64,651

社区成员

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

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