链表问题

smdszgzh 2009-01-01 04:30:19

#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};

class list
{
public:
Node *head;
public:
list()
{
head=NULL;
}
list Create(list &L);
void Output(list &L);
int Insert(list &L,int i,Node e);
Node Erase(list &L,int i,Node e);
};
list Create(list &L)
{
L.head=new Node;
int x;
do
{
cout<<"Please input the number(-999 quit)"<<endl;
cin>>x;
Node *p=new Node;
p->data=x;
p->next=L.head->next;
L.head->next=p;
} while (x!=-999);
cout<<"成功建立链表"<<endl;
return L;
}

void Output(list &L)
{
while(L.head->next!=NULL)
{
cout<<L.head->data<<" ";
L.head->next=L.head->next->next;
}
cout<<endl;
}
int main()
{
list L;
Create(L);
Output(L);
return 0;
}

程序运行有问题
...全文
57 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
九桔猫 2009-01-01
  • 打赏
  • 举报
回复
路过,观望中
baihacker 2009-01-01
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};

class list
{
public:
Node *head;
public:
list()
{
head=NULL;
}
list Create(list &L);
void Output(list &L);
int Insert(list &L,int i,Node e);
Node Erase(list &L,int i,Node e);
};
list Create(list &L)
{
L.head=new Node;
L.head->next = NULL;//这里
int x;
do
{
cout<<"Please input the number(-999 quit)"<<endl;
cin>>x;
Node *p=new Node;
p->data=x;
p->next=L.head->next;
L.head->next=p;
} while (x!=-999);
cout<<"成功建立链表"<<endl;
return L;
}

void Output(list &L)
{
while(L.head->next!=NULL)
{
cout<<L.head->next->data<<" ";//这里
L.head->next=L.head->next->next;
}
cout<<endl;
}
int main()
{
list L;
Create(L);
Output(L);
return 0;
}
//和预期结果一样.如果不想要那个-999,自己改改
d:\>a
Please input the number(-999 quit)
1
Please input the number(-999 quit)
2
Please input the number(-999 quit)
3
Please input the number(-999 quit)
4
Please input the number(-999 quit)
5
Please input the number(-999 quit)
6
Please input the number(-999 quit)
7
Please input the number(-999 quit)
8
Please input the number(-999 quit)
9
Please input the number(-999 quit)
-999
成功建立链表
-999 9 8 7 6 5 4 3 2 1

65,211

社区成员

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

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