65,011
社区成员
发帖
与我相关
我的任务
分享
//本程序是为了建一个链表!
//并完成相关操作
#include<iostream>
#include<string>
using namespace std;
struct student
{
string name;
student *next;
};
void creat(student *first)//建立链表,并输入数据
{
student *current;
int a;
first=current=new student;
cout<<"输入姓名:\n";
cin>>current->name;
cout<<"继续输入(yes==1,no==0)"<<'\n';
cin>>a;
while(a)
{
current=current->next=new student;
cin>>current->name;
cout<<"继续输入(yes==1,no==0)"<<'\n';
cin>>a;
}
current->next=0;
cout<<"输入完成!\n";
}
void print(student *first)//输出
{
student *current;
current=first;
while(current!=0)
{
cout<<current->name<<'\n';
current=current->next;
}
}
void destroy(student *first)//销毁
{
student *current;
while(first!=0)
{
current=first->next;
delete first;
first=current;
}
cout<<"删除成功\n";
}
int main()
{
student *first=NULL;//为什么要将first赋值为NULL,可不可以不赋值啊?
creat(first);
print(first);//通过测试,我发现first为空,为什么?也就是说,first=0
destroy(first);
return 0;
}