我是靠谱客的博主 老迟到往事,这篇文章主要介绍集美大学 - 2840 - 实验11-2 - 函数题实验11-2-1 链表 建立学生信息链表实验11-2-2 链表 学生成绩链表处理实验11-2-3 链表 逆序数据建立链表实验11-2-4 链表 删除单链表偶数节点实验11-2-5 链表 链表拼接实验11-2-6 链表 奇数值结点链表实验11-2-7 链表 统计专业人数实验11-2-8 链表 单链表结点删除实验11-2-9 链表 链表逆置,现在分享给大家,希望可以做个参考。

实验11-2-1 链表 建立学生信息链表

本题要求实现一个将输入的学生成绩组织成单向链表的简单函数。

函数接口定义:

复制代码
1
2
void input();

该函数利用scanf从输入中获取学生的信息,并将其组织成单向链表。链表节点结构定义如下:

复制代码
1
2
3
4
5
6
7
struct stud_node { int num; /*学号*/ char name[20]; /*姓名*/ int score; /*成绩*/ struct stud_node *next; /*指向下个结点的指针*/ };

单向链表的头尾指针保存在全局变量headtail中。

输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。

裁判测试程序样例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h> #include <stdlib.h> #include <string.h> struct stud_node { int num; char name[20]; int score; struct stud_node *next; }; struct stud_node *head, *tail; void input(); int main() { struct stud_node *p; head = tail = NULL; input(); for ( p = head; p != NULL; p = p->next ) printf("%d %s %dn", p->num, p->name, p->score); return 0; } /* 你的代码将被嵌在这里 */

输入样例:

复制代码
1
2
3
4
5
6
1 zhang 78 2 wang 80 3 li 75 4 zhao 85 0

输出样例:

复制代码
1
2
3
4
5
1 zhang 78 2 wang 80 3 li 75 4 zhao 85
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void input() { struct stud_node *p; int num; scanf("%d", &num); while (num != 0) { p = (struct stud_node *) malloc(sizeof(struct stud_node)); p->num = num; scanf("%s%d", p->name, &p->score); p->next = NULL; if (head == NULL) head = p; else tail->next = p; tail = p; scanf("%d", &num); } }

实验11-2-2 链表 学生成绩链表处理

本题要求实现两个函数,一个将输入的学生成绩组织成单向链表;另一个将成绩低于某分数线的学生结点从链表中删除。

函数接口定义:

复制代码
1
2
3
struct stud_node *createlist(); struct stud_node *deletelist( struct stud_node *head, int min_score );

函数createlist利用scanf从输入中获取学生的信息,将其组织成单向链表,并返回链表头指针。链表节点结构定义如下:

复制代码
1
2
3
4
5
6
7
struct stud_node { int num; /*学号*/ char name[20]; /*姓名*/ int score; /*成绩*/ struct stud_node *next; /*指向下个结点的指针*/ };

输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。

函数deletelist从以head为头指针的链表中删除成绩低于min_score的学生,并返回结果链表的头指针。

裁判测试程序样例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h> #include <stdlib.h> struct stud_node { int num; char name[20]; int score; struct stud_node *next; }; struct stud_node *createlist(); struct stud_node *deletelist( struct stud_node *head, int min_score ); int main() { int min_score; struct stud_node *p, *head = NULL; head = createlist(); scanf("%d", &min_score); head = deletelist(head, min_score); for ( p = head; p != NULL; p = p->next ) printf("%d %s %dn", p->num, p->name, p->score); return 0; } /* 你的代码将被嵌在这里 */

输入样例:

复制代码
1
2
3
4
5
6
7
1 zhang 78 2 wang 80 3 li 75 4 zhao 85 0 80

输出样例:

复制代码
1
2
3
2 wang 80 4 zhao 85
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
struct stud_node *createlist() { struct stud_node *head = NULL, *rear = head, *p; int num; scanf("%d", &num); while (num != 0) { p = (struct stud_node *) malloc(sizeof(struct stud_node)); p->num = num; scanf("%s %d", p->name, &p->score); if (head == NULL) head = p; else rear->next = p; rear = p; p->next = NULL; scanf("%d", &num); } return head; } struct stud_node *deletelist(struct stud_node *head, int min_score) { struct stud_node *p = NULL, *q = NULL, *x = NULL, *y = NULL; if (head == NULL) return NULL; p = head; while (p->score < min_score) { p = p->next; if (p == NULL) return p; } head = p; q = p->next; if (q == NULL) return p; while (q != NULL) { if (q->score < min_score) { x = q; q = q->next; free(x); p->next = q; } else { p = q; q = p->next; } } return head; }

实验11-2-3 链表 逆序数据建立链表

本题要求实现一个函数,按输入数据的逆序建立一个链表。

函数接口定义:

复制代码
1
2
struct ListNode *createlist();

函数createlist利用scanf从输入中获取一系列正整数,当读到−1时表示输入结束。按输入数据的逆序建立一个链表,并返回链表头指针。链表节点结构定义如下:

复制代码
1
2
3
4
5
struct ListNode { int data; struct ListNode *next; };

裁判测试程序样例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *createlist(); int main() { struct ListNode *p, *head = NULL; head = createlist(); for ( p = head; p != NULL; p = p->next ) printf("%d ", p->data); printf("n"); return 0; } /* 你的代码将被嵌在这里 */

输入样例:

复制代码
1
2
1 2 3 4 5 6 7 -1

输出样例:

复制代码
1
2
7 6 5 4 3 2 1
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct ListNode *createlist() { struct ListNode *head = NULL, *p, *x; int num; scanf("%d", &num); if (num == -1) return NULL; while (num != -1) { p = (struct ListNode *) malloc(sizeof(struct ListNode)); p->data = num; p->next = NULL; if (head == NULL) { head = p; } else { p->next = head; head = p; } scanf("%d", &num); } return head; }

实验11-2-4 链表 删除单链表偶数节点

本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下:

复制代码
1
2
3
4
5
struct ListNode { int data; struct ListNode *next; };

函数接口定义:

复制代码
1
2
3
struct ListNode *createlist(); struct ListNode *deleteeven( struct ListNode *head );

函数createlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。

函数deleteeven将单链表head中偶数值的结点删除,返回结果链表的头指针。

裁判测试程序样例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *createlist(); struct ListNode *deleteeven( struct ListNode *head ); void printlist( struct ListNode *head ) { struct ListNode *p = head; while (p) { printf("%d ", p->data); p = p->next; } printf("n"); } int main() { struct ListNode *head; head = createlist(); head = deleteeven(head); printlist(head); return 0; } /* 你的代码将被嵌在这里 */

输入样例:

复制代码
1
2
1 2 2 3 4 5 6 7 -1

输出样例:

复制代码
1
2
1 3 5 7
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
struct ListNode *createlist() { struct ListNode *head = NULL, *rear = head, *p; int num; scanf("%d", &num); if (num == -1) return NULL; while (num != -1) { p = (struct ListNode *) malloc(sizeof(struct ListNode)); p->data = num; p->next = NULL; if (head == NULL) head = p; else rear->next = p; rear = p; scanf("%d", &num); } return head; } struct ListNode *deleteeven(struct ListNode *head) { struct ListNode *p, *q, *x; if (head == NULL) return NULL; while (head->data % 2 == 0) { head = head->next; if (head == NULL) return NULL; } p = head; q = p->next; while (q != NULL) { if ((q->data) % 2 == 0) { x = q; q = q->next; free(x); p->next = q; } else { p = q; q = p->next; } } return head; }

实验11-2-5 链表 链表拼接

本题要求实现一个合并两个有序链表的简单函数。链表结点定义如下:

复制代码
1
2
3
4
5
struct ListNode { int data; struct ListNode *next; };

函数接口定义:

复制代码
1
2
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2);

其中list1list2是用户传入的两个按data升序链接的链表的头指针;函数mergelists将两个链表合并成一个按data升序链接的链表,并返回结果链表的头指针。

裁判测试程序样例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *createlist(); /*裁判实现,细节不表*/ struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2); void printlist( struct ListNode *head ) { struct ListNode *p = head; while (p) { printf("%d ", p->data); p = p->next; } printf("n"); } int main() { struct ListNode *list1, *list2; list1 = createlist(); list2 = createlist(); list1 = mergelists(list1, list2); printlist(list1); return 0; } /* 你的代码将被嵌在这里 */

输入样例:

复制代码
1
2
3
1 3 5 7 -1 2 4 6 -1

输出样例:

复制代码
1
2
1 2 3 4 5 6 7
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2) { struct ListNode *fuzhu, *p, *head; int i = 0, j, cnt = 0, a[1000] = {0}, e; if (list1 == NULL) return list2; head = list1; fuzhu = list1; while (fuzhu != NULL) { if (fuzhu->next == NULL) break; fuzhu = fuzhu->next; } fuzhu->next = list2; p = head; while (p != NULL) { a[i++] = p->data; cnt++; p = p->next; } for (i = 0; i < cnt; i++) { for (j = 1; j < cnt - i; j++) { if (a[j - 1] >= a[j]) { e = a[j]; a[j] = a[j - 1]; a[j - 1] = e; } } } i = 0; struct ListNode *head1 = NULL, *rear = head1; while (i < cnt) { p = (struct ListNode *) malloc(sizeof(struct ListNode)); p->data = a[i++]; if (head1 == NULL) head1 = p; else rear->next = p; rear = p; p->next = NULL; } return head1; }

实验11-2-6 链表 奇数值结点链表

本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中奇数值的结点重新组成一个新的链表。链表结点定义如下:

复制代码
1
2
3
4
5
struct ListNode { int data; ListNode *next; };

函数接口定义:

复制代码
1
2
3
struct ListNode *readlist(); struct ListNode *getodd( struct ListNode **L );

函数readlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。

函数getodd将单链表L中奇数值的结点分离出来,重新组成一个新的链表。返回指向新链表头结点的指针,同时将L中存储的地址改为删除了奇数值结点后的链表的头结点地址(所以要传入L的指针)。

裁判测试程序样例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *readlist(); struct ListNode *getodd( struct ListNode **L ); void printlist( struct ListNode *L ) { struct ListNode *p = L; while (p) { printf("%d ", p->data); p = p->next; } printf("n"); } int main() { struct ListNode *L, *Odd; L = readlist(); Odd = getodd(&L); printlist(Odd); printlist(L); return 0; } /* 你的代码将被嵌在这里 */

输入样例:

复制代码
1
2
1 2 2 3 4 5 6 7 -1

输出样例:

复制代码
1
2
3
1 3 5 7 2 2 4 6
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
struct ListNode* readlist() { struct ListNode* head=NULL, * rear = head, * p; int num; scanf("%d", &num); while (num != -1) { p = (struct ListNode*)malloc(sizeof(struct ListNode)); p->data = num; if (head == NULL) head = p; else rear->next = p; rear = p; rear->next = NULL; scanf("%d", &num); } return head; } struct ListNode* getodd(struct ListNode** L){ struct ListNode* p, * str1, * str2, * head = NULL, * rear = head,*fuzhu,*q; str1 = *L; if (str1 == NULL) return NULL; while (str1->data%2!=0) { p = (struct ListNode *) malloc(sizeof(struct ListNode)); p->data = str1->data; if (head == NULL) head = p; else rear->next = p; rear = p; rear->next = NULL; str1 = str1->next; if (str1 == NULL) { *L = NULL; return head; } } q = str1; str2 = str1->next; if (str2 == NULL) { *L = str1; } while (str2 != NULL) { if (str2->data % 2 != 0) { p = (struct ListNode *) malloc(sizeof(struct ListNode)); p->data = str2->data; if (head == NULL) head = p; else rear->next = p; rear = p; rear->next = NULL; fuzhu = str2; str2 = str2->next; str1->next = str2; free(fuzhu); } else { str1 = str2; str2 = str2->next; } } *L = q; return head; }

实验11-2-7 链表 统计专业人数

本题要求实现一个函数,统计学生学号链表中专业为计算机的学生人数。链表结点定义如下:

复制代码
1
2
3
4
5
struct ListNode { char code[8]; struct ListNode *next; };

这里学生的学号共7位数字,其中第2、3位是专业编号。计算机专业的编号为02。

函数接口定义:

复制代码
1
2
int countcs( struct ListNode *head );

其中head是用户传入的学生学号链表的头指针;函数countcs统计并返回head链表中专业为计算机的学生人数。

裁判测试程序样例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h> #include <stdlib.h> #include <string.h> struct ListNode { char code[8]; struct ListNode *next; }; struct ListNode *createlist(); /*裁判实现,细节不表*/ int countcs( struct ListNode *head ); int main() { struct ListNode *head; head = createlist(); printf("%dn", countcs(head)); return 0; } /* 你的代码将被嵌在这里 */

输入样例:

复制代码
1
2
3
4
5
6
7
8
1021202 2022310 8102134 1030912 3110203 4021205 #

输出样例:

复制代码
1
2
3
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
int countcs(struct ListNode *head) { int cnt = 0; struct ListNode *p = head; while (p != NULL) { if (p->code[1] == '0' && p->code[2] == '2') { cnt++; } p = p->next; } return cnt; }

实验11-2-8 链表 单链表结点删除

本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中所有存储了某给定值的结点删除。链表结点定义如下:

复制代码
1
2
3
4
5
struct ListNode { int data; ListNode *next; };

函数接口定义:

复制代码
1
2
3
struct ListNode *readlist(); struct ListNode *deletem( struct ListNode *L, int m );

函数readlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。

函数deletem将单链表L中所有存储了m的结点删除。返回指向结果链表头结点的指针。

裁判测试程序样例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *readlist(); struct ListNode *deletem( struct ListNode *L, int m ); void printlist( struct ListNode *L ) { struct ListNode *p = L; while (p) { printf("%d ", p->data); p = p->next; } printf("n"); } int main() { int m; struct ListNode *L = readlist(); scanf("%d", &m); L = deletem(L, m); printlist(L); return 0; } /* 你的代码将被嵌在这里 */

输入样例:

复制代码
1
2
3
10 11 10 12 10 -1 10

输出样例:

复制代码
1
2
11 12
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
struct ListNode *readlist() { struct ListNode *head = NULL, *rear = head, *p; int num; scanf("%d", &num); while (num != -1) { p = (struct ListNode *) malloc(sizeof(struct ListNode)); p->data = num; if (head == NULL) head = p; else rear->next = p; rear = p; rear->next = NULL; scanf("%d", &num); } return head; } struct ListNode *deletem(struct ListNode *L, int m) { struct ListNode *str1, *str2, *p, *fuzhu; if (L == NULL) return L; str1 = L; while (str1->data == m) { str1 = str1->next; if (str1->next == NULL) return NULL; } p = str1; if (str1 == NULL) return str1; str2 = str1->next; if (str2 == NULL) return p; while (str2 != NULL) { if (str2->data == m) { fuzhu = str2; str2 = str2->next; free(fuzhu); str1->next = str2; } else { str1 = str2; str2 = str2->next; } } return p; }

实验11-2-9 链表 链表逆置

本题要求实现一个函数,将给定单向链表逆置,即表头置为表尾,表尾置为表头。链表结点定义如下:

复制代码
1
2
3
4
5
struct ListNode { int data; struct ListNode *next; };

函数接口定义:

复制代码
1
2
struct ListNode *reverse( struct ListNode *head );

其中head是用户传入的链表的头指针;函数reverse将链表head逆置,并返回结果链表的头指针。

裁判测试程序样例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h> #include <stdlib.h> struct ListNode { int data; struct ListNode *next; }; struct ListNode *createlist(); /*裁判实现,细节不表*/ struct ListNode *reverse( struct ListNode *head ); void printlist( struct ListNode *head ) { struct ListNode *p = head; while (p) { printf("%d ", p->data); p = p->next; } printf("n"); } int main() { struct ListNode *head; head = createlist(); head = reverse(head); printlist(head); return 0; } /* 你的代码将被嵌在这里 */

输入样例:

复制代码
1
2
1 2 3 4 5 6 -1

输出样例:

复制代码
1
2
6 5 4 3 2 1
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct ListNode *reverse(struct ListNode *head) { struct ListNode *p, *head1 = NULL, *rear = head1; int i = 0, cnt = 0, a[1000] = {0}; if (head == NULL) return NULL; while (head != NULL) { a[i++] = head->data; cnt++; head = head->next; } for (i = cnt - 1; i >= 0; i--) { p = (struct ListNode *) malloc(sizeof(struct ListNode)); p->data = a[i]; if (head1 == NULL) head1 = p; else rear->next = p; rear = p; rear->next = NULL; } return head1; }

最后

以上就是老迟到往事最近收集整理的关于集美大学 - 2840 - 实验11-2 - 函数题实验11-2-1 链表 建立学生信息链表实验11-2-2 链表 学生成绩链表处理实验11-2-3 链表 逆序数据建立链表实验11-2-4 链表 删除单链表偶数节点实验11-2-5 链表 链表拼接实验11-2-6 链表 奇数值结点链表实验11-2-7 链表 统计专业人数实验11-2-8 链表 单链表结点删除实验11-2-9 链表 链表逆置的全部内容,更多相关集美大学内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(81)

评论列表共有 0 条评论

立即
投稿
返回
顶部