C语言习题09_自定义类型
习题09(01)定义结构体
题目描述
定义结构体 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>
struct student
{
unsigned int num;
char name[20];
char sex;
char addr[20];
} mer = {89031, "Li Lin", 'M', "123 Beijing Road"};
int main(void)
{
printf("NO:%d\nname:%s\nsex:%c\naddress:%s\n", mer.num, mer.name, mer.sex, mer.addr);
return 0;
}
习题09(02)单向动态链表
题目描述
写一个建立单向动态链表的 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 <malloc.h>
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat()
{
struct student *head;
struct student *p1, *p2;
n = 0;
p1 = p2 = (struct student *)malloc(LEN);
scanf("%ld %f", &p1->num, &p1->score);
head = NULL;
while (p1->num != 0)
{
n = n + 1;
if (n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct student *)malloc(LEN);
scanf("%ld", &p1->num);
if (p1->num == 0)
break;
scanf("%f", &p1->score);
}
p2->next = NULL;
return (head);
}
void print(struct student *head)
{
struct student *p;
printf("\nNow,These %d records are:\n", n);
printf("num\tscore\n");
p = head;
if (head != NULL)
do
{
printf("%ld\t%5.1f\n", p->num, p->score);
p = p->next;
} while (p != NULL);
}
int main()
{
struct student *head;
head = creat();
print(head);
return 0;
}
习题09(03)删除链表中指定学生数据
题目描述
写一个建立单向动态链表的函数,从键盘输入 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 <malloc.h>
#define LEN sizeof(struct student)
struct student *del(struct student *head, long num);
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat()
{
struct student *head;
struct student *p1, *p2;
n = 0;
p1 = p2 = (struct student *)malloc(LEN);
scanf("%ld %f", &p1->num, &p1->score);
head = NULL;
while (p1->num != 0)
{
n = n + 1;
if (n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct student *)malloc(LEN);
scanf("%ld", &p1->num);
if (p1->num == 0)
break;
scanf("%f", &p1->score);
}
p2->next = NULL;
scanf("%ld", &p1->num);
del(head, p1->num);
return (head);
}
void print(struct student *head)
{
struct student *p;
printf("\nNow,These %d records are:\n", n);
printf("num\tscore\n");
p = head;
if (head != NULL)
do
{
printf("%ld\t%5.1f\n", p->num, p->score);
p = p->next;
} while (p != NULL);
}
int main()
{
struct student *head;
head = creat();
print(head);
return 0;
}
struct student *del(struct student *head, long num)
{
struct student *p1, *p2;
if (head == NULL)
{
printf("\nlist null!\n");
return (head);
}
p1 = head;
while (num != p1->num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (num == p1->num)
{
if (p1 == head)
head = p1->next;
else p2->next = p1->next;
printf("delete:%ld\n", num);
n = n - 1;
}
else
printf("%ld not been found!\n", num);
return (head);
}
习题09(04)-动态链表插入学生数据
题目描述
写一个建立单向动态链表的函数,从键盘输入 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 <malloc.h>
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
int main()
{
struct student *creat();
struct student *insert(struct student *, struct student *);
void print(struct student *);
struct student *head, stu;
head = creat();
scanf("%ld%f", &stu.num, &stu.score);
head = insert(head, &stu);
print(head);
return 0;
}
struct student *creat()
{
struct student *head;
struct student *p1, *p2;
n = 0;
p1 = p2 = (struct student *)malloc(LEN);
scanf("%ld%f", &p1->num, &p1->score);
head = NULL;
while (p1->num != 0)
{
n = n + 1;
if (n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct student *)malloc(LEN);
scanf("%ld", &p1->num);
if (p1->num == 0)
break;
scanf("%f", &p1->score);
}
p2->next = NULL;
return (head);
}
struct student *insert(struct student *head, struct student *stud)
{
struct student *p0, *p1, *p2;
p1 = head;
p0 = stud;
if (head == NULL)
{
head = p0;
p0->next = NULL;
}
else
{
while ((p0->num > p1->num) && (p1->next != NULL))
{
p2 = p1;
p1 = p1->next;
}
if (p0->num <= p1->num)
{
if (head == p1)
head = p0;
else
p2->next = p0;
p0->next = p1;
}
else
{
p1->next = p0;
p0->next = NULL;
}
}
n = n + 1;
return (head);
}
void print(struct student *head)
{
struct student *p;
printf("\nNow,These %d records are:\n", n);
printf("num\tscore\n");
p = head;
if (head != NULL)
do
{
printf("%ld\t%5.1f\n", p->num, p->score);
p = p->next;
} while (p != NULL);
}