/*单链表实现对学生链表的创建,输出以及逆置*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct student{
int no;//学号
float score;//成绩(若以-1结束,就结束创建)
struct student *next;
};
struct student *create_LinkList(){
int no; float score;
struct student *head=NULL,*cur;
struct student *p;
while(1){
scanf("%d%f",&no,&score);
if(score==-1)
return head;
else{
p=(struct student *)malloc(sizeof(struct student));
p->no=no;
p->score=score;
p->next=NULL;
if(head){
cur->next=p;//得想成一般情况来赋值
cur=p;
}else{//头指针为空
head=p;//p为头指针
cur=head;
}
}
}
return head;
}
struct student *print(struct student *head){
struct student *p=head;
while(p){
printf("%d\n%f\n",p->no,p->score);
p=p->next;
}
return head;
}
struct student * reverse(struct student *head){
struct student *pre=NULL,*cur=head,*post;
while(cur){
post=cur->next;
cur->next=pre;
pre=cur;
cur=post;
}
return pre; //返回新的头节点
}
int main(){
struct student *L;
L=create_LinkList();
print(L);
printf("\n");
L=reverse(L);
print(L);
return 0;
}
附:运行截图