正在加载图片...
student*next; struct student *head. s: void create(/生成单链表 int i; struct student*rear; head=NULLs Ou"请输入"<N<"个学生的学号、姓名和成绩"cend i=0;N;i++) new student;∥申请结构点空间 in>s->no>>s->name>>s> score;∥读入学生数据 if(head==NULL) head /将新插入结点为第一结点 rear>next=s;∥其余结点插入在表尾指针rear后 s→>next=NULL; rear-s ∥移动尾指针 void show0∥显示单链表 struct student*p cout<"lnt学号t姓名分数n"; cout<<t p=head; ∥指针取链表首地址 while(p) cout<<"<p→>no<<"t<p>name<"t'p>score<<endl; p-p 击t 指针移到下一个结构体元素 void maino create;/生成单表 showO;∥显示单链表元素 6在单链表中插入一个结点 void inslink( student*st/插入结点 struct student*p, * q if (head==NULL) head是全局变量student *next; } ; struct student *head,*s; void create() //生成单链表 { int i; struct student *rear; head=NULL; cout<<"请输入"<<N<<" 个学生的学号、姓名和成绩"<<endl; for (i=0;i<N;i++) { s=new student; //申请结构点空间 cin>>s->no>>s->name>>s->score; //读入学生数据 if(head==NULL) head=s; //将新插入结点为第一结点 else rear->next=s; //其余结点插入在表尾指针 rear 后 s->next=NULL; rear=s; //移动尾指针 } } void show() //显示单链表 { struct student *p; cout<<"\n\t 学号\t 姓名\t 分数\n"; cout<<"\t---------------------\n"; p=head; //指针取链表首地址 while(p) { cout<<'\t'<<p->no<<'\t'<<p->name<<'\t'<<p->score<<endl; p=p->next; //指针移到下一个结构体元素 } } void main() { create(); //生成单表 show(); //显示单链表元素 } 6 在单链表中插入一个结点 void inslink(student *st) //插入结点 { struct student *p,*q; if (head==NULL) //head 是全局变量
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有