单链表实现对自定义的结构体学生进行反转并且输出

/*单链表实现对学生链表的创建,输出以及逆置*/
#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;
}

附:运行截图 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值