习题9 自定义数据类型

1. (10分) 1 定义Student结构体

题目描述
定义结构体struct Student数据类型,其成员包括num,name[20],sex,addr[20],定义该结构体变量初始化
{89031,“Li Lin”,‘M’,“123 Beijing Road”},在main中输出该变量的各成员。

输入描述

输出描述
输出结构体变量的各成员数据

输入样例

输出样例
NO:89031
name:Li Lin
sex:M
address:123 Beijing Road

用户代码

#include<stdio.h>
int main()
{
	struct Student
	{
		int num;
		char name[20];
		char sex;
		char addr[17];
	};
	struct Student stu={89031,"Li Lin",'M',"123 Beijing Road"};
	printf("NO:%d\nname:%s\nsex:%c\naddress:%s\n",stu.num,stu.name,stu.sex,stu.addr);
	return 0;
}

2. (20分) 2 建立单向动态链表并输出

题目描述
写一个建立单向动态链表的create函数,从键盘输入n个学生的数据(num,score),约定学号不会为0,如果学号为0表示建立链表的过程完成,并写一个print函数输出链表,输出格式:
“%ld\t%5.1f\n”
表头输出格式:printf(“num\tscore”)

输入描述
从键盘输入n个学生的数据,完成动态链表的建立。

输出描述
输出链表

输入样例
1 99
2 88
3 77
4 66
5 55
0

输出样例
Now,These 5 records are:
num score
1 99.0
2 88.0
3 77.0
4 66.0
5 55.0

用户代码

#include<stdio.h>
#include<stdlib.h>
#define STU struct Student
   struct Student
	{
		int num;
	    float score;
		struct Student *next;
	};
int main()
{
	int n=0;
	STU *p1,*p2,*head;
	head=p1=p2=(STU *)malloc(sizeof(STU));
	scanf("%d%f",&p1->num,&p1->score);
	while(p1->num!=0)
	{
		
		if(n)
		{
			p2->next=p1;
			p2=p1;
		}
		p1=(STU *)malloc(sizeof(STU));
		scanf("%d%f",&p1->num,&p1->score);
		n++;
	}
	p2->next=NULL;
	free(p1);
	p1=p2=head;
	printf("Now,These %d records are:\nnum\tscore\n",n);
	while(p1!=NULL)
	{
		printf("%ld\t%5.1f\n",p1->num,p1->score);
		p1=p1->next;
	}
	return 0;
}

最后保存时间: 2019-12-21 12:31:12

本题得分:20分(2/2)
查看最新评判结果

3. (40分) 3 删除链表中指定学生数据

题目描述
写一个建立单向动态链表的函数,从键盘输入n个学生的数据(num,score),约定学号不会为0,如果学号为0表示建立链表的过程完成,然后从键盘输入一个要删除学生的学号,并输出删除后的链表结果("%ld\t%5.1f\n")。
写三个函数creat,print,del各实现其功能。
注意:如果要删除的学号在链表中不存在,则先输出:("%d not been found!\n",num); 然后再输出链表中的结点数据

输入描述
从键盘输入n个学生的数据,建立n个学生的动态链表
从键盘输入要删除的学生数据

输出描述
输出删除后链表结果

输入样例
1 99
2 88
3 77
4 66
5 55
0
3

输出样例
delete:3
Now,These 4 records are:
num score
1 99.0
2 88.0
4 66.0
5 55.0

用户代码


#include<stdio.h>
#include<stdlib.h>
#define STU struct Student
   struct Student
	{
		int num;
	    float score;
		struct Student *next;
	};
int main()
{
	int n=0,num;
	STU *p1,*p2,*head;
	STU *del(STU* head,int num);
	head=p1=p2=(STU *)malloc(sizeof(STU));
	scanf("%d%f",&p1->num,&p1->score);
	while(p1->num!=0)
	{
		
		if(n)
		{
			p2->next=p1;
			p2=p1;
		}
			n++;
		p1=(STU *)malloc(sizeof(STU));
		scanf("%d",&p1->num);
		if(p1->num==0)
			break;
		scanf("%f",&p1->score);
	}
	p2->next=NULL;
	free(p1);
	scanf("%d",&num);

	if(num<1||num>5)
	{
		printf("%d not been found!\n",num);
	    printf("Now,These %d records are:\nnum\tscore\n",n);
	}
	else
	{
		printf("delete:%d",num);
		printf("Now,These %d records are:\nnum\tscore\n",n-1);
	    head=del(head,num);
	}
	p1=p2=head;
	while(p1!=NULL)
	{
		printf("%ld\t%5.1f\n",p1->num,p1->score);
		p1=p1->next;
	}
	return 0;
}
STU *del(STU* head,int num)
{
	STU *p1,*p2;
	p1=p2=head;
	while(p1->num!=num&&p1!=NULL)
	{
		p2=p1;
		p1=p1->next;
	}
	if(p1->num==num&&num!=1)
	    p2->next=p1->next;
	else if(p1->num==num&&num==1)
        head=head->next;
	return head;
} 

4. (30分) 4 动态链表插入学生数据

题目描述
写一个建立单向动态链表的函数,从键盘输入n个学生的数据(num,score),约定学号不会为0,如果学号为0表示建立链表的过程完成,然后从键盘输入一个要插入学生的数据,并输出插入后的链表结果,输出格式:("%ld\t%5.1f\n")。
写三个函数create, print, insert各实现其功能。
注意:插入后的链表学号依旧按由小到大排列。

输入描述
从键盘输入n个学生的数据,完成建立链表
从键盘输入要插入的1个学生数据

输出描述
输出插入数据后链表

输入样例
1 99
2 88
3 77
4 66
5 55
0
9 90

输出样例
Now,These 6 records are:
num score
1 99.0
2 88.0
3 77.0
4 66.0
5 55.0
9 90.0

用户代码

#include<stdio.h>
#include<stdlib.h>
#define STU struct Student
   struct Student
	{
		int num;
	    float score;
		struct Student *next;
	};
int main()
{
	int n=0;
	STU *p1,*p2,*head,*p3;
	STU *insert(STU *head,STU *p3);
	head=p1=p2=(STU *)malloc(sizeof(STU));
	scanf("%d%f",&p1->num,&p1->score);
	while(p1->num!=0)
	{
		
		if(n)
		{
			p2->next=p1;
			p2=p1;
		}
			n++;
		p1=(STU *)malloc(sizeof(STU));
		scanf("%d",&p1->num);
		if(p1->num==0)
			break;
		scanf("%f",&p1->score);
	}
	p2->next=NULL;
	free(p1);
	p3=(STU *)malloc(sizeof(STU));
	p3->next=NULL;
	scanf("%d%f",&p3->num,&p3->score);
	head=insert(head,p3);
	p1=p2=head;
	printf("Now,These %d records are:\n",n+1);
	printf("num\tscore\n");
	while(p1!=NULL)
	{
		printf("%ld\t%5.1f\n",p1->num,p1->score);
		p1=p1->next;
	}
	return 0;
}
STU *insert(STU* head,STU *p3)
{
	STU *p1,*p2;
	p1=p2=head;
	while(p1!=NULL&&p1->num<p3->num)
	{
		p2=p1;
		p1=p1->next;
	}
	if(p1!=NULL)
	{
		p3->next=p1;
		if(p3->num>p2->num)
		p2->next=p3;
		else
			head=p3;
	}
	else if(p1==NULL)
		p2->next=p3;
	
	return head;
}
发布了37 篇原创文章 · 获赞 10 · 访问量 738

猜你喜欢

转载自blog.csdn.net/qq_43608850/article/details/104320793