7-3 成绩大于等于某值的学生信息输出 (10分)

7-3 成绩大于等于某值的学生信息输出 (10分)

带头结点的做法。

输入若干个学生信息(包括学号、姓名和成绩),输入学号为0时输入结束,建立一个单向链表,再输入一个成绩值,将成绩大于等于该值的学生信息输出。

提示:

定义函数struct stud_node *Creat_Stu_Doc()完成创建链表

定义函数struct stud_node DeleteDoc(struct stud_node head,int min_score)将分数低于min_score的结点删除

定义函数void Ptrint_Stu_Doc(struct stud_node *head)打印链表

输入输出示例:括号内为说明,无需输入输出

思路就是用带头结点的尾插法建立链表,然后删除节点,最后输出。

struct stud_node *Creat_Stu_Doc()函数比较简单,就不予多解释了

struct stud_node DeleteDoc(struct stud_node head,int min_score)函数
记得改成下面这样,因为接受的返回值类型需要 *DeleteDoc类型
struct stud_node *DeleteDoc(struct stud_node *head,int min_score)
而且这个函数需要3个辅助节点 pre指向*p的前驱节点,若p所指向的值小于
min_score,则删除p(此时借用q来实现删除操作)。若p不小于min_score,让
pre和p后移。

void Ptrint_Stu_Doc(struct stud_node *head)打印链表也没啥好说的哈哈

输入样例:

1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0
80

输出样例:`

2 wang 80
4 zhao 85
#include <stdio.h>
#include <stdlib.h>
struct stud_node{
	int num;
    char name[20];
    int score;
    struct stud_node *next;
};
struct stud_node *Creat_Stu_Doc();
struct stud_node *DeleteDoc(struct stud_node *head,int min_score);
void Ptrint_Stu_Doc(struct stud_node *head);
int main(){
	struct stud_node *head;
	head = Creat_Stu_Doc();
	int x;
	scanf("%d",&x);
	head = DeleteDoc(head,x);
	Ptrint_Stu_Doc(head);
	return 0;
} 
struct stud_node *Creat_Stu_Doc(){
	struct stud_node *head,*p,*tail;
	int x;
	head = (struct stud_node*)malloc(sizeof(struct stud_node));
	head->next = NULL;//千万记得给头结点初始化后继节点为NULL 
	tail = head;
	scanf("%d",&x);
	while(x != 0){
		p = (struct stud_node*)malloc(sizeof(struct stud_node));
		p->next = NULL; 
		p->num = x;
		scanf("%s%d",p->name,&p->score);
		tail->next = p;//给尾插啊!!!毕竟尾插法 
		tail = p;
		scanf("%d",&x);
	}
	tail->next = NULL;
	return head;
}
struct stud_node *DeleteDoc(struct stud_node *head,int min_score){
	struct stud_node *p,*q,*pre;
	//pre为p的前驱节点
	p = head->next;
	pre = head;
	while(p != NULL){
		if(p->score < min_score){
			q = p;
			p = p->next;
			pre->next = p;
			free(q);
		}
		else{
			pre = p;
			p = p->next;
		}
	}
	return head;
}
void Ptrint_Stu_Doc(struct stud_node *head){
	struct stud_node *p;
	for(p = head->next; p != NULL; p = p->next){
		printf("%d %s %d\n",p->num,p->name,p->score);
	}
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值