请大家帮个忙,关于c++中结构指针的问题

zcl198715 zcl198715 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
本程序是为了建一个链表!
并完成相关操作
#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;
}
希望大家能给些帮助,不胜感谢!
...全文

相关推荐

评论
收藏 已收藏
分享
返回顶部
zcl198715
C++ 语言 65,214 成员 250,514 内容