实验11-2-5 链表拼接(20 分)

实验11-2-5 链表拼接(20 分)

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

1
2
3
4
struct ListNode {
int data;
struct ListNode *next;
};

函数接口定义:

1
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
#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
1 3 5 7 -1
2 4 6 -1

输出样例:

1
1 2 3 4 5 6 7

实验11-2-4 删除单链表偶数节点(20 分)

实验11-2-4 删除单链表偶数节点(20 分)

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

1
2
3
4
struct ListNode {
int data;
struct ListNode *next;
};

函数接口定义:

1
2
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
#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
1 2 2 3 4 5 6 7 -1

输出样例:

1
1 3 5 7

实验11-2-3 逆序数据建立链表(20 分)

实验11-2-3 逆序数据建立链表(20 分)

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

函数接口定义:

1
struct ListNode *createlist();

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

1
2
3
4
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
#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
1 2 3 4 5 6 7 -1

输出样例:

1
7 6 5 4 3 2 1

实验11-2-2 学生成绩链表处理(20 分)

实验11-2-2 学生成绩链表处理(20 分)

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

函数接口定义:

1
2
struct stud_node *createlist();
struct stud_node *deletelist( struct stud_node *head, int min_score );

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

1
2
3
4
5
6
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
#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 %d\n", 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
80

输出样例:

1
2
2 wang 80
4 zhao 85

实验11-2-1 建立学生信息链表(20 分)

实验11-2-1 建立学生信息链表(20 分)

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

函数接口定义:

1
void input();

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

1
2
3
4
5
6
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
#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 %d\n", p->num, p->name, p->score);

return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

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

输出样例:

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

实验11-1-9 藏尾诗(20 分)

实验11-1-9 藏尾诗(20 分)

本题要求编写一个解密藏尾诗的程序。

输入格式:

输入为一首中文藏尾诗,一共四句。每句一行,但句子不一定是等长的,最短一个汉字,最长九个汉字。注意:一个汉字占两个字节。

输出格式:

取出每句的最后一个汉字并连接在一起形成一个字符串并输出。同时在末尾输入一个换行符。

输入样例:

1
2
3
4
悠悠田园风
然而心难平
兰花轻涌浪
兰香愈幽静

输出样例:

1
风平浪静

实验11-1-8 查找子串(20 分)

实验11-1-8 查找子串(20 分)

本题要求实现一个字符串查找的简单函数。

函数接口定义:

1
char *search( char *s, char *t );

函数search在字符串s中查找子串t,返回子串t在s中的首地址。若未找到,则返回NULL。

裁判测试程序样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#define MAXS 30

char *search(char *s, char *t);
void ReadString( char s[] ); /* 裁判提供,细节不表 */

int main()
{
char s[MAXS], t[MAXS], *pos;

ReadString(s);
ReadString(t);
pos = search(s, t);
if ( pos != NULL )
printf("%d\n", pos - s);
else
printf("-1\n");

return 0;
}

/* 你的代码将被嵌在这里 */

输入样例1:

1
2
The C Programming Language
ram

输出样例1:

1
10

输入样例2:

1
2
The C Programming Language
bored

输出样例2:

1
-1

实验11-1-7 藏头诗(15 分)

实验11-1-7 藏头诗(15 分)

本题要求编写一个解密藏头诗的程序。

输入格式:

输入为一首中文藏头诗,一共四句,每句一行。注意:一个汉字占两个字节。

输出格式:

取出每句的第一个汉字并连接在一起形成一个字符串并输出。同时在末尾输入一个换行符。

输入样例:

1
2
3
4
一叶轻舟向东流
帆稍轻握杨柳手
风纤碧波微起舞
顺水任从雅客流

输出样例:

1
一帆风顺

实验11-1-6 指定位置输出字符串(20 分)

实验11-1-6 指定位置输出字符串(20 分)

本题要求实现一个函数,对给定的一个字符串和两个字符,打印出给定字符串中从与第一个字符匹配的位置开始到与第二个字符匹配的位置之间的所有字符。

函数接口定义:

1
char *match( char *s, char ch1, char ch2 );

函数match应打印s中从ch1ch2之间的所有字符,并且返回ch1的地址。

裁判测试程序样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

#define MAXS 10

char *match( char *s, char ch1, char ch2 );

int main()
{
char str[MAXS], ch_start, ch_end, *p;

scanf("%s\n", str);
scanf("%c %c", &ch_start, &ch_end);
p = match(str, ch_start, ch_end);
printf("%s\n", p);

return 0;
}

/* 你的代码将被嵌在这里 */

输入样例1:

1
2
program
r g

输出样例1:

1
2
rog
rogram

输入样例2:

1
2
program
z o

输出样例2:

1
2
(空行)
(空行)

输入样例3:

1
2
program
g z

输出样例3:

1
2
gram
gram

实验11-1-5 字符串的连接(15 分)

实验11-1-5 字符串的连接(15 分)

本题要求实现一个函数,将两个字符串连接起来。

函数接口定义:

1
char *str_cat( char *s, char *t );

函数str_cat应将字符串t复制到字符串s的末端,并且返回字符串s的首地址。

裁判测试程序样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <string.h>

#define MAXS 10

char *str_cat( char *s, char *t );

int main()
{
char *p;
char str1[MAXS+MAXS] = {'\0'}, str2[MAXS] = {'\0'};

scanf("%s%s", str1, str2);
p = str_cat(str1, str2);
printf("%s\n%s\n", p, str1);

return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

1
2
abc
def

输出样例:

1
2
abcdef
abcdef
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×