浙大版《C语言程序设计实验与习题指导(第4版)》题目集 I don’t have permission to test the following questions,I try to get it right myself,It is not guaranteed to be delivered to the platform.
实验11-2-1 建立学生信息链表
本题要求实现一个将输入的学生成绩组织成单向链表的简单函数.
函数接口定义:
void input();
该函数利用scanf从输入中获取学生的信息,并将其组织成单向链表.链表节点结构定义如下:
struct stud_node {int num; /*学号*/char name[20]; /*姓名*/int score; /*成绩*/struct stud_node *next; /*指向下个结点的指针*/};
单向链表的头尾指针保存在全局变量head和tail中.
输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束.
裁判测试程序样例:
#include #include #include 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 zhang 78 2 wang 80 3 li 75 4 zhao 85 0 输出样例: 1 zhang 78 2 wang 80 3 li 75 4 zhao 85
void input(){struct stud_node *p;int flag=0;while(1){p = (struct stud_node*)malloc(sizeof(struct stud_node));scanf("%d",&p->num);if(p->num==0) break;scanf("%s %d",p->name ,&p->score );if(!flag){head = p;tail = p;p->next = NULL;flag = 1;}else{tail->next = p;tail = p;}}free(p);}
实验11-2-3 逆序数据建立链表
函数接口定义:
struct ListNode *createlist();
函数createlist利用scanf从输入中获取一系列正整数,当读到−1时表示输入结束.按输入数据的逆序建立一个链表,并返回链表头指针.链表节点结构定义如下:
struct ListNode {int data;struct ListNode *next;};
裁判测试程序样例:
#include #include 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 3 4 5 6 7 -1 输出样例: 7 6 5 4 3 2 1
struct ListNode *createlist(){struct ListNode *p,*head,*last;int a[10001];int i,num;head = NULL;last = head;for(i=0;;i++){scanf("%d",&num);if(num == -1) break;a[i] = num;}i--;for(i;i>=0;i--){p = (struct ListNode*)malloc(sizeof(struct ListNode));p->data = a[i];if(!head){head = p;last = p;p->next = NULL;}else{last->next = p;last = p;p->next = NULL;}}//我每个pAre in use can not writefree,Otherwise there will be a big problem // free(p);return head;}
实验11-2-4 删除单链表偶数节点
本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除.链表结点定义如下:
struct ListNode {int data;struct ListNode *next;};
函数接口定义:
struct ListNode *createlist();struct ListNode *deleteeven( struct ListNode *head );
函数createlist从标准输入读入一系列正整数,按照读入顺序建立单链表.当读到−1时表示输入结束,函数应返回指向单链表头结点的指针.
函数deleteeven将单链表head中偶数值的结点删除,返回结果链表的头指针.
裁判测试程序样例:
#include #include 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 2 3 4 5 6 7 -1 输出样例: 1 3 5 7
struct ListNode *createlist(){struct ListNode *head,*tail,*p;int num;head = NULL;tail = head;while(1){p = (struct ListNode*)malloc(sizeof(struct ListNode));scanf("%d",&num);if(num == -1) break;p->data = num;if(!head){head = p;tail = p;p->next = NULL;}else{tail->next = p;tail = p;p->next = NULL;}}return head;}struct ListNode *deleteeven( struct ListNode *head ){struct ListNode *p,*q;while(head!= NULL&&head->data%2==0){p = head;head = head->next;free(p);}if(!head) return NULL;p = head;q = p->next ;while(q!=NULL){if(q->data %2==0){p->next = q->next ;free(q);q = p->next ;}else{p = q ;q = p->next ;}}return head;}
实验11-2-5 链表拼接
本题要求实现一个合并两个有序链表的简单函数.链表结点定义如下:
struct ListNode {int data;struct ListNode *next;};
函数接口定义:
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2);
其中list1和list2是用户传入的两个按data升序链接的链表的头指针;函数mergelists将两个链表合并成一个按data升序链接的链表,并返回结果链表的头指针.
裁判测试程序样例:
#include #include 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 3 5 7 -1 2 4 6 -1 输出样例: 1 2 3 4 5 6 7 I don’t have platform testing permissions,In order to be able to compile,I even wrote it myselfcreatelist函数:
/*裁判实现,细节不表*/struct ListNode *createlist(){struct ListNode *p,*head,*tail;int num;head = NULL;tail = head;while(1){p = (struct ListNode*)malloc(sizeof(struct ListNode));scanf("%d",&num);if(num == -1) break;p->data = num;if(!head){head = p;tail = p;p->next = NULL;}else{tail->next = p;tail = p;p->next = NULL;}}return head;}
/* 你的代码将被嵌在这里 */struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2){struct ListNode *head,*last;head = NULL;last = head;if(!list1 && !list2) return NULL;if(list1->data > list2->data ){head = list2;last = list2;list2 = list2->next ;}else{head = list1;last = list1;list1 = list1->next ;}while(list1!=NULL&&list2!=NULL){if(list1->data > list2->data){last->next = list2;list2 = list2->next ;//Just here I forgotlast跟上去,debug半天 last = last->next ;}else{last->next = list1;list1 = list1->next ;//这里也是 last = last->next ;}}for(;list2;list2=list2->next ){last->next = list2;last = list2;}for(;list1;list1=list1->next ){last->next = list1;last = list1;}last->next = NULL;return head;}
实验11-2-7 统计专业人数
本题要求实现一个函数,统计学生学号链表中专业为计算机的学生人数.链表结点定义如下:
struct ListNode {char code[8];struct ListNode *next;};
这里学生的学号共7位数字,其中第2、3位是专业编号.计算机专业的编号为02.
函数接口定义:
int countcs( struct ListNode *head );
其中head是用户传入的学生学号链表的头指针;函数countcs统计并返回head链表中专业为计算机的学生人数.
裁判测试程序样例:
#include #include #include struct ListNode {char code[8];struct ListNode *next;};struct ListNode *createlist(); /*裁判实现,细节不表*/int countcs( struct ListNode *head );int main(){struct ListNode *head;head = createlist();printf("%d\n", countcs(head));return 0;}/* 你的代码将被嵌在这里 */
输入样例: 1021202 2022310 8102134 1030912 3110203 4021205
输出样例: 3 没错,I wrote it myself againcreatelist:
/*裁判实现,细节不表*/struct ListNode *createlist(){struct ListNode *p,*head,*tail;head = NULL;tail = head;while(1){p = (struct ListNode*)malloc(sizeof(struct ListNode));scanf("%s",p->code );if(strcmp(p->code,"#")==0) break;if(!head){head = p;tail = p;p->next = NULL;}else{tail->next = p;//sleepy,Forgot the tail followed just now tail = p;p->next = NULL;}}return head;}
/* 你的代码将被嵌在这里 */int countcs( struct ListNode *head ){int cnt=0;while(head){if(head->code[1]=='0'&&head->code[2]=='2'){cnt++;}head = head->next ;}return cnt;}
实验11-2-9 链表逆置
本题要求实现一个函数,将给定单向链表逆置,即表头置为表尾,表尾置为表头.链表结点定义如下:
struct ListNode {int data;struct ListNode *next;};
函数接口定义:
struct ListNode *reverse( struct ListNode *head );
其中head是用户传入的链表的头指针;函数reverse将链表head逆置,并返回结果链表的头指针.
裁判测试程序样例:
#include #include 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 3 4 5 6 -1 输出样例: 6 5 4 3 2 1 createlist:
/*裁判实现,细节不表*/struct ListNode *createlist(){struct ListNode *p,*head,*tail;int num;head = NULL;tail = head;while(1){p = (struct ListNode*)malloc(sizeof(struct ListNode));scanf("%d",&num);if(num==-1) break;p->data = num;if(!head){head = p;tail = p;p->next = NULL;}else{tail->next = p;tail = p;p->next = NULL;}}free(p);return head;}
/* 你的代码将被嵌在这里 */struct ListNode *reverse( struct ListNode *head ){struct ListNode *p=head,*tou,*last,*q;int a[10001],i;tou = NULL;last = tou;for(i=0;;i++){//注意这里是判断p空,Can't tell what the value inside is-1 if(p==NULL) break;a[i] = p->data ;p = p->next ;}i--;for(i;i>=0;i--){q = (struct ListNode*)malloc(sizeof(struct ListNode));q->data = a[i];if(!tou){tou = q;last = q;q->next = NULL;}else{last->next = q;last = q;q->next = NULL;}}return tou;}
ssn:Currently watching online,There is no dumber method than I use.The repetition rate of the exam directly pasted is also extremely low
实验11-2-2 学生成绩链表处理
本题要求实现两个函数,一个将输入的学生成绩组织成单向链表;另一个将成绩低于某分数线的学生结点从链表中删除.
函数接口定义:
struct stud_node *createlist();struct stud_node *deletelist( struct stud_node *head, int min_score );
函数createlist利用scanf从输入中获取学生的信息,将其组织成单向链表,并返回链表头指针.链表节点结构定义如下:
struct stud_node {int num; /*学号*/char name[20]; /*姓名*/int score; /*成绩*/struct stud_node *next; /*指向下个结点的指针*/};
输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束.
函数deletelist从以head为头指针的链表中删除成绩低于min_score的学生,并返回结果链表的头指针.
裁判测试程序样例:
#include #include 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 zhang 78 2 wang 80 3 li 75 4 zhao 85 0 80 输出样例: 2 wang 80 4 zhao 85
struct stud_node *createlist(){struct stud_node *p,*head,*last;head = NULL;last = head;while(1){p = (struct stud_node*)malloc(sizeof(struct stud_node));scanf("%d",&p->num);if(p->num == 0) break;scanf("%s %d",p->name ,&p->score );if(!head){head = p;last = p;p->next = NULL;}else{last->next = p;last = p;p->next = NULL;}}free(p);return head;}struct stud_node *deletelist( struct stud_node *head, int min_score ){struct stud_node *p,*pp;if(!head) return NULL;while(head&&head->score <min_score){p = head;head = head->next ;free(p);}p = head;pp = p->next ;while(pp){if(pp->score < min_score){p->next = pp->next ;pp = p->next ;}else{p = p->next ;pp = pp->next ;}}return head;}