请大家帮个忙,关于c++中结构指针的问题
本程序是为了建一个链表!
并完成相关操作
#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;
}
希望大家能给些帮助,不胜感谢!
并完成相关操作
#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;
}
希望大家能给些帮助,不胜感谢!
...全文
相关推荐
基于C#调用c++Dll结构体数组指针的问题详解
下面小编就为大家分享一篇基于C#调用c++Dll结构体数组指针的问题详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
C++结构体参数与结构体指针参数区别Demo
C++结构体参数与结构体指针参数区别Demo(资源包括C++源程序和编译好的exe文件)
C++在函数中给类的指针变量赋值问题
在函数中给C++指针变量赋值,分析了指针变量的特性,并追踪了给指针变量赋值的过程。
C++中指向结构体变量的指针
定义: 结构体变量的指针就是该变来那个所占据的内存段的起始地址。可以设一个指针变量,来指向一个结构体变量,此时该指针变量的值是结构体变量的起始地址。 设p是指向结构体变量的数组,则可以通过以下的方式,调用指向的那个结构体中的成员: (1)结构体变量.成员名。如,stu.num。 (2)(*p).成员名。如,(*p).num。 (3)p->成员名。如,p->num。 代码如下:#include<iostream>#include<string>using namespace std;struct Candidate{ string name; int count;};int main(){ Ca
数据结构 C++ 指针2
数据结构 C++ 指针的应用例子集合2.


