任务41:链表、结构体的应用 《编程语言-C语言》45分钟专题训练
|
2份
|
30页
|
148人阅读
|
1人下载
内容正文:
湖南省职教高考《编程语言—C语言》45分钟专题训练专辑,共43份试卷。试卷命题与高考紧密相关,精选具有代表性和典型性的高考常考题。旨在帮助学生系统地复习和巩固计算机类理论知识,熟悉高考题型和命题规律,提高解题能力和应试技巧,为高考取得优异成绩打下坚实的基础,本训练卷适用于即将参加计算机类高考的学生。与本专辑配套的还有湖南省职教高考《计算机应用基础》《计算机组装与维护》《计算机网络技术》45分钟专题训练专辑,欢迎同学和老师们下载使用。
《编程语言—C语言》
项目八: 结构体、共用体、链表
第41卷: 链表、结构体的应用
时间:45分钟 总分:100分
班级___________ 姓名__________ 学号__________ 成绩________
一、选择题(共26小题,每小题2分,共52分)
1.设有说明:int *p;p=【1】malloc(sizeof(int));,若要使得p指向一个int型的动态存储单元,则【1】处应该填入( )
A.int B.(int *) C.int * D.(*int)
2.设有一个名为“Year”的结构体,其中定义了两个整型成员变量“time”和“day”,下列用于访问“Year”结构体变量“time”成员变量的是( )
A.Year->time B.Year.time C.Year[time] D.&Year.time
3.设已建立如下图所示的链表,指针p,s分别指向图中所示的结点,则下列选项中,不能将指针s所指的结点插入到链表末尾的语句段是( )
A.s->next=NULL;p=p->next;p->next=s;
B.p=p->next;s->next=p->next;p->next=s;
C.p=p->next;s->next=p;p->next=s;
D.p=(*p).next;(*s).next=(*p).next;(*p).next=s;
4.在C语言中,线性表采用链式存储结构时,要求内存中可用存储单元的地址( )
A.可以是连续或不连续的地址 B.必须是连续的地址
C.部分地址必须是连续的 D.必须是不连续的地址
5.在一个单链表中,若在p所指向的结点之后插入s所指向的结点,下列选项中可以实现的语句组是( )
A.s->next=p;p->next=s;
B.s->next=p->next;p->next=s;
C.s->next=p->next;p=s;
D.p->next=s;s->next=p;
6.在C语言中,链接存储的存储结构所占存储空间( )
A.分为两部分,一部分存放结点值,另一部分存放表示结点间关系的指针
B.全部存放结点值
C.全部存储表示结点间关系的指针
D.分为两部分,一部分存放结点值,一部分存放结点所占单元数量
7.若要在q和p直接插入结点s,可以实现该功能的语句组是( )
A.s->next=p->next;p->next=s; B.p->next=s->next;s->next=p;
C.q->next=s;s->next=p; D.p->next=s;s->next=q;
8.若要在将s所指结点插入到p所指节点之后,正确的语句组是( )
A.s->next=p+1;p->next=s;
B.(*p).next=s;(*s).next=(*p).next;
C.s->next=p->next;p->next=s->next;
D.s->next=p->next;p->next=s;
9.循环链表的主要优点是( )
A.不再需要头指针
B.已知某个结点的位置后,能够容易找到它的直接前趋
C.在进行插入、删除运算时,能更好的保证链表断开
D.从表中任一结点出发都能扫描到整个链表
10.设已建立下列所示的动态链表结构,且指针p1和p2均已执行下图中所示结点,则下列选项中,可以将p2所指结点从链表中删除并释放该结点的语句组是( )
A.(*p1).next=(*p2).next;free(p1);
B.p1=p2;free(p2);
C.p1->next=p2->next;free(p2);
D.p1=p2->next;free(p2);
11.设下列所使用的指针均已正确定义,指针p1和p2分别指向两个无头结点的非空单循环链表中的尾结点,要将两个链表链接成一个新的单循环链表,应该执行的操作是( )
A.p1->next=p2->next;p2->next=p1->next;
B.p2->next=p1->next;p1->next=p2->next;
C.p=p2->next;p1->next=p;p2->next=p1->next;
D.p=p1->next;p1->next=p2->next;p2->next=p;
12.变量root有如下图所示的存储结构,其中sp是指向字符串的指针域,next是指向该结构的指针域,data用以存放整型数,请选择填空,完成下列结构类型说明和变量root定义。( )
struct stu{char *sp;struct stu 【1】;int data;}root;
A. *next B.next C.**next D.data
13.设有定义:struct ss{int info;struct ss *link;}x,y,z;,且已建立如下图所示的链表结构,那么删除结点y的赋值语句是( )
A.x.link=y.link; B.x.info=y.info;
C.y.link=z.link; D.x.link=z.link;
14.设有定义:struct no{int data;struct node next;}*p;,若要使以下语句调用malloc函数,使指针p指向一个具有struct no类型的动态存储空间,请选择填空。( )
p=(struct no *)mallloc(【1】);
A.sizeof(struct) B.sizeof(struct no)
C.strlen(struct) D.sizeof(struct no *)
15.下列程序段用来构成一个简单的单向链表,请选择填空。( )
struct st{int x,y;float rate;【1】;}a,b;
a.x=0;a.y=0;a.rate=0;a.p=&b;b.x=0;b.y=0;b.rate=0;b.p=NULL;
A.struct st p B.struct st *p
C.struct p D.struct st *b;
16.设有定义和语句:struct node{char ch;struct node *pn;}a,b,c,*s1,*s2;
a.next=&b;s1=&a;s2=&c;,则下列选项中能将结点c插入到结点a,b之间的语句组是( )
A.s1.pn=s2;s2.pn=s1.pn;
B.a.pn=c;c.pn=b;
C.(*s1).pn=s2;(*s2).pn=&b;
D.s1->pn=&c;s2->pn=s1->pn;
17.设有以下程序段,其中,形参h1和h2分别指向两个不同链表的第一个结点,链表最后结点的next域中已经置0,则此函数的功能是( )
void fun(struct st h1,struct st h2)
{struct st *t;t=h1;
while(t->next!='\0') t=t->next;
t->next=h2;}
A.将链表h2接到链表h1后
B.将链表h1接到链表h2后
C.找到链表h1的最后一个结点并由指针返回
D.将链表h1拆分成两个链表
18.已知head指向单链表的第一个结点,以下函数del的功能是完成从单向链表中删除值为num的第一个结点。请为第【1】空选择填空。( )
struct student{ int info;struct student *link;};
struct student *del(struct student *head,int num)
{struct student *p1,*p2;if(head==NULL)printf("
list null!
");
else{ p1=head;
while(【1】){ p2=p1;p1=p1->link;}
if(num==p1->info)
{ if(p1==head)【2】;
else 【3】;printf("delete:%d
",num);}
else printf("%d not been found!
",num);}return(head);}
A. num!=p1->info||p1->link!=NULL B. num!=p1->info&&p1->link==NULL
C. num!=p1->info&&p1->link!=NULL D. num==p1->info&&p1->link!=NULL
19. 已知head指向单链表的第一个结点,以下函数del的功能是完成从单向链表中删除值为num的第一个结点。请为第【2】空选择填空。( )
struct student{ int info;struct student *link;};
struct student *del(struct student *head,int num)
{struct student *p1,*p2;if(head==NULL)printf("
list null!
");
else{ p1=head;
while(【1】){ p2=p1;p1=p1->link;}
if(num==p1->info)
{ if(p1==head)【2】;
else 【3】;printf("delete:%d
",num);}
else printf("%d not been found!
",num);}return(head);}
A. *head=p1->link B. head=p2->link
C. head=p1 D.head=p1->link
20. 已知head指向单链表的第一个结点,以下函数del的功能是完成从单向链表中删除值为num的第一个结点。请为第【3】空选择填空。( )
struct student{ int info;struct student *link;};
struct student *del(struct student *head,int num)
{struct student *p1,*p2;if(head==NULL)printf("
list null!
");
else{ p1=head;
while(【1】){ p2=p1;p1=p1->link;}
if(num==p1->info)
{ if(p1==head)【2】;
else 【3】;printf("delete:%d
",num);}
else printf("%d not been found!
",num);}return(head);}
A. p2=p1->link B. p2->link=p1
C. p1->link=p2->link D. p2->link=p1->link
21.下列程序段的功能是统计链表中结点的个数,其中first为指向第一个结点的指针(该链表不带头结点)。请为第【1】空选择填空。( )
struct link{char data;struct link *next;};
…… struct link *p,*first;int c=0;p=first;
while(【1】){c++;【2】;}
A.p==NULL B.p!=NULL C.!(p!=NULL) D.p=NULL
22.下列程序段的功能是统计链表中结点的个数,其中first为指向第一个结点的指针(该链表不带头结点)。请为第【2】空选择填空。( )
struct link{char data;struct link *next;};
…… struct link *p,*first;int c=0;p=first;
while(【1】){c++;【2】;}
A.p=p->next; B.p=p->next C.p->next->next D.p->next=p
23.已知head指向一个带头结点的单向链表,链表中的每个结点包含数据域(data)和指针域(next),数据域为整型,以下程序段的功能是求出链表中所有链结点数据域的和的值,并作为函数值返回。请为第【1】空选择填空。( )
struct link{int data;struct link *next;};
int sum(【1】)
{struct link *p;int s=0;p=head->next;
while(p){【2】;p=p->next;}return s;}
main()
{struct link *head;
……
printf("%d",sum(head));getch();}
A. int *head B. struct link head
C. link *head D.struct link *head
24.已知head指向一个带头结点的单向链表,链表中的每个结点包含数据域(data)和指针域(next),数据域为整型,以下程序段的功能是求出链表中所有链结点数据域的和的值,并作为函数值返回。请为第【2】空选择填空。( )
struct link{int data;struct link *next;};
int sum(【1】)
{struct link *p;int s=0;p=head->next;
while(p){【2】;p=p->next;}return s;}
main()
{struct link *head;
……
printf("%d",sum(head));getch();}
A. s=p->data B. s+=p->data
C. s+=p->next D.s+=data
25.已知head指向单链表的第一个结点,以下程序调用函数print的功能是输出这一单向链表。请为第【1】空选择填空。( )
struct student{int info;struct student *link;};
void print(struct student *head)
{ struct student *p;p=head;
if(head!=NULL)
do{printf("%d",p->info);【1】;}while(【2】);}
main( ){ struct student *head;……print(head);……}
A. p=p->next B. p->link C. p=p->link D. head=p->link
26.已知head指向单链表的第一个结点,以下程序调用函数print的功能是输出这一单向链表。请为第【2】空选择填空。( )
struct student{int info;struct student *link;};
void print(struct student *head)
{ struct student *p;p=head;
if(head!=NULL)
do{printf("%d",p->info);【1】;}while(【2】);}
main( ){ struct student *head;……print(head);……}
A. p==NULL B. !(p!=NULL) C. p!=1 D. p!=NULL
二、阅读程序,修改程序中的错误,不得增行或删行,也不得更改程序结构。请指出错误代码所在的行号,并给出该行修改后的程序代码。每处3分,共9分)
27.下列函数的功能是:函数del实现删除动态链表中指定的结点。函数中存在三措错误,请改正。
L1 struct student
L2 {int num;
L3 float score;
L4 struct student *next;};
L5 int n;
L6 struct student *del(struct student *head,long num)
L7 {struct student *p1,*p2;
L8 if(head==NULL)
L9 {printf("
list null!
");
L10 return head;}
L11 p1=head;
L12 while(num!=p1->num||p1->next!=NULL)
L13 {p2=p1;
L14 p1=p1->next;}
L15 if(num!=p1->num)
L16 {if(p1==head) head=p1->next;
L17 else p2->next=p1->next;
L18 printf("delete:%d
",num);
L19 n=n-1;}
L20 else printf("%d not been found!
",num);
L21 return (*head);}
三、程序填空。按照题目要求,将正确的内容填入相应位置,使得程序完整(每空3分,共39分)
28.下列程序段的功能是:13个人围成一圈,从第1个人开始顺序报号1、2、3,凡是报到3的人退出圈子。找出最后留在圈子里的人原来的序号。请填空。
#include <stdio.h>
#include<conio.h>
#define N 13
struct person
{int number;
int nextp;}link[N+1];
main()
{int i,count,h;
for(i=1;i<=N;i++)
{if(i==N) link[i].nextp=1;
else【1】;
link[i].number=i;}
count=0;
h=N;
printf("sequence that person leave the circle:
");
while(【2】)
{i=0;
while(i!=3)
{h=link[h].nextp;
if(【3】) i++;}
printf("%4d",link[h].number);
link[h].number=0;
【4】;}
printf("
The last one is:");
for(i=1;i<=N;i++)
if(link[i].number)
printf("%3d",link[i].number);
printf("
");getch();}
29. 下列程序的功能是:通过函数creat建立一个有3名学生数据的单向动态链表,输入数据后,通过函数print将链表中各结点的数据依次输出。请填空。
#include <stdio.h>
#include<conio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{int num;
float score;
struct student *next;};
int n;
struct student *creat()
{struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=【1】;
scanf("%d,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1) head=p1;
else【2】;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%d,%f",&p1->num,&p1->score);}
p2->next=NULL;
return head;}
void print(struct student *head)
{struct student *p;
printf("
Now,These %d records are:
",n);
p=head;
if(【3】)
do{printf("%d %5.1f
",p->num,p->score);
p=p->next;
}while(p!=NULL);}
main()
{【4】;
head=creat();
print(head);
getch();}
30.下列程序的功能是:通过调用函数fun(),将单向链表逆置。若原链表从头至尾的结点数据域依次为1、2、3、4、5;逆置后,从头至尾的结点数据域依次为5、4、3、2、1。请填空。
#include <stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
#define N 5
typedef struct node{int data;【1】;}NODE;
NODE *fun(NODE *h)
{NODE *p,*q,*r;
p=h;
if(p==NULL) return NULL;
q=p->next;
p->next=NULL;
while(【2】)
{r=q->next;【3】;
p=q;q=r;}
return p;}
NODE *creatlist(int a[ ])
{NODE *h,*p,*q;
int i;
h=NULL;
for(i=0;i<N;i++)
{【4】;
q->data=a[i];
q->next=NULL;
if(【5】)
{h=p=q;}
else {p->next=q;p=q;}}
return h;}
void outlist(NODE *h)
{NODE *p;p=h;
if(p==NULL) printf("The list is NULL!
");
else {printf("Head");
do
{printf("->%d",p->data);
p=p->next;}while(p!=NULL);
printf("->End
");}}
main()
{NODE *head;
int a[N]={1,2,3,4,5};
head=creatlist(a);
printf("
The original list:
");
outlist(head);
head=fun(head);
printf("
The list after inverting:
");
outlist(head);
getch();}
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
$$
湖南省职教高考《编程语言—C语言》45分钟专题训练专辑,共43份试卷。试卷命题与高考紧密相关,精选具有代表性和典型性的高考常考题。旨在帮助学生系统地复习和巩固计算机类理论知识,熟悉高考题型和命题规律,提高解题能力和应试技巧,为高考取得优异成绩打下坚实的基础,本训练卷适用于即将参加计算机类高考的学生。与本专辑配套的还有湖南省职教高考《计算机应用基础》《计算机组装与维护》《计算机网络技术》45分钟专题训练专辑,欢迎同学和老师们下载使用。
《编程语言—C语言》
项目八: 结构体、共用体、链表
第41卷: 链表、结构体的应用
时间:45分钟 总分:100分
班级___________ 姓名__________ 学号__________ 成绩________
一、选择题(共26小题,每小题2分,共52分)
1.设有说明:int *p;p=【1】malloc(sizeof(int));,若要使得p指向一个int型的动态存储单元,则【1】处应该填入( )
A.int B.(int *) C.int * D.(*int)
【答案】B
2.设有一个名为“Year”的结构体,其中定义了两个整型成员变量“time”和“day”,下列用于访问“Year”结构体变量“time”成员变量的是( )
A.Year->time B.Year.time C.Year[time] D.&Year.time
【答案】B
3.设已建立如下图所示的链表,指针p,s分别指向图中所示的结点,则下列选项中,不能将指针s所指的结点插入到链表末尾的语句段是( )
A.s->next=NULL;p=p->next;p->next=s;
B.p=p->next;s->next=p->next;p->next=s;
C.p=p->next;s->next=p;p->next=s;
D.p=(*p).next;(*s).next=(*p).next;(*p).next=s;
【答案】C
4.在C语言中,线性表采用链式存储结构时,要求内存中可用存储单元的地址( )
A.可以是连续或不连续的地址 B.必须是连续的地址
C.部分地址必须是连续的 D.必须是不连续的地址
【答案】A
5.在一个单链表中,若在p所指向的结点之后插入s所指向的结点,下列选项中可以实现的语句组是( )
A.s->next=p;p->next=s;
B.s->next=p->next;p->next=s;
C.s->next=p->next;p=s;
D.p->next=s;s->next=p;
【答案】B
6.在C语言中,链接存储的存储结构所占存储空间( )
A.分为两部分,一部分存放结点值,另一部分存放表示结点间关系的指针
B.全部存放结点值
C.全部存储表示结点间关系的指针
D.分为两部分,一部分存放结点值,一部分存放结点所占单元数量
【答案】A
7.若要在q和p直接插入结点s,可以实现该功能的语句组是( )
A.s->next=p->next;p->next=s; B.p->next=s->next;s->next=p;
C.q->next=s;s->next=p; D.p->next=s;s->next=q;
【答案】C
8.若要在将s所指结点插入到p所指节点之后,正确的语句组是( )
A.s->next=p+1;p->next=s;
B.(*p).next=s;(*s).next=(*p).next;
C.s->next=p->next;p->next=s->next;
D.s->next=p->next;p->next=s;
【答案】D
9.循环链表的主要优点是( )
A.不再需要头指针
B.已知某个结点的位置后,能够容易找到它的直接前趋
C.在进行插入、删除运算时,能更好的保证链表断开
D.从表中任一结点出发都能扫描到整个链表
【答案】D
10.设已建立下列所示的动态链表结构,且指针p1和p2均已执行下图中所示结点,则下列选项中,可以将p2所指结点从链表中删除并释放该结点的语句组是( )
A.(*p1).next=(*p2).next;free(p1);
B.p1=p2;free(p2);
C.p1->next=p2->next;free(p2);
D.p1=p2->next;free(p2);
【答案】C
11.设下列所使用的指针均已正确定义,指针p1和p2分别指向两个无头结点的非空单循环链表中的尾结点,要将两个链表链接成一个新的单循环链表,应该执行的操作是( )
A.p1->next=p2->next;p2->next=p1->next;
B.p2->next=p1->next;p1->next=p2->next;
C.p=p2->next;p1->next=p;p2->next=p1->next;
D.p=p1->next;p1->next=p2->next;p2->next=p;
【答案】D
12.变量root有如下图所示的存储结构,其中sp是指向字符串的指针域,next是指向该结构的指针域,data用以存放整型数,请选择填空,完成下列结构类型说明和变量root定义。( )
struct stu{char *sp;struct stu 【1】;int data;}root;
A. *next B.next C.**next D.data
【答案】A
13.设有定义:struct ss{int info;struct ss *link;}x,y,z;,且已建立如下图所示的链表结构,那么删除结点y的赋值语句是( )
A.x.link=y.link; B.x.info=y.info;
C.y.link=z.link; D.x.link=z.link;
【答案】A
14.设有定义:struct no{int data;struct node next;}*p;,若要使以下语句调用malloc函数,使指针p指向一个具有struct no类型的动态存储空间,请选择填空。( )
p=(struct no *)mallloc(【1】);
A.sizeof(struct) B.sizeof(struct no)
C.strlen(struct) D.sizeof(struct no *)
【答案】B
15.下列程序段用来构成一个简单的单向链表,请选择填空。( )
struct st{int x,y;float rate;【1】;}a,b;
a.x=0;a.y=0;a.rate=0;a.p=&b;b.x=0;b.y=0;b.rate=0;b.p=NULL;
A.struct st p B.struct st *p
C.struct p D.struct st *b;
【答案】B
16.设有定义和语句:struct node{char ch;struct node *pn;}a,b,c,*s1,*s2;
a.next=&b;s1=&a;s2=&c;,则下列选项中能将结点c插入到结点a,b之间的语句组是( )
A.s1.pn=s2;s2.pn=s1.pn;
B.a.pn=c;c.pn=b;
C.(*s1).pn=s2;(*s2).pn=&b;
D.s1->pn=&c;s2->pn=s1->pn;
【答案】C
17.设有以下程序段,其中,形参h1和h2分别指向两个不同链表的第一个结点,链表最后结点的next域中已经置0,则此函数的功能是( )
void fun(struct st h1,struct st h2)
{struct st *t;t=h1;
while(t->next!='\0') t=t->next;
t->next=h2;}
A.将链表h2接到链表h1后
B.将链表h1接到链表h2后
C.找到链表h1的最后一个结点并由指针返回
D.将链表h1拆分成两个链表
【答案】A
18.已知head指向单链表的第一个结点,以下函数del的功能是完成从单向链表中删除值为num的第一个结点。请为第【1】空选择填空。( )
struct student{ int info;struct student *link;};
struct student *del(struct student *head,int num)
{struct student *p1,*p2;if(head==NULL)printf("
list null!
");
else{ p1=head;
while(【1】){ p2=p1;p1=p1->link;}
if(num==p1->info)
{ if(p1==head)【2】;
else 【3】;printf("delete:%d
",num);}
else printf("%d not been found!
",num);}return(head);}
A. num!=p1->info||p1->link!=NULL B. num!=p1->info&&p1->link==NULL
C. num!=p1->info&&p1->link!=NULL D. num==p1->info&&p1->link!=NULL
【答案】C
19. 已知head指向单链表的第一个结点,以下函数del的功能是完成从单向链表中删除值为num的第一个结点。请为第【2】空选择填空。( )
struct student{ int info;struct student *link;};
struct student *del(struct student *head,int num)
{struct student *p1,*p2;if(head==NULL)printf("
list null!
");
else{ p1=head;
while(【1】){ p2=p1;p1=p1->link;}
if(num==p1->info)
{ if(p1==head)【2】;
else 【3】;printf("delete:%d
",num);}
else printf("%d not been found!
",num);}return(head);}
A. *head=p1->link B. head=p2->link
C. head=p1 D.head=p1->link
【答案】D
20. 已知head指向单链表的第一个结点,以下函数del的功能是完成从单向链表中删除值为num的第一个结点。请为第【3】空选择填空。( )
struct student{ int info;struct student *link;};
struct student *del(struct student *head,int num)
{struct student *p1,*p2;if(head==NULL)printf("
list null!
");
else{ p1=head;
while(【1】){ p2=p1;p1=p1->link;}
if(num==p1->info)
{ if(p1==head)【2】;
else 【3】;printf("delete:%d
",num);}
else printf("%d not been found!
",num);}return(head);}
A. p2=p1->link B. p2->link=p1
C. p1->link=p2->link D. p2->link=p1->link
【答案】D
21.下列程序段的功能是统计链表中结点的个数,其中first为指向第一个结点的指针(该链表不带头结点)。请为第【1】空选择填空。( )
struct link{char data;struct link *next;};
…… struct link *p,*first;int c=0;p=first;
while(【1】){c++;【2】;}
A.p==NULL B.p!=NULL C.!(p!=NULL) D.p=NULL
【答案】B
22.下列程序段的功能是统计链表中结点的个数,其中first为指向第一个结点的指针(该链表不带头结点)。请为第【2】空选择填空。( )
struct link{char data;struct link *next;};
…… struct link *p,*first;int c=0;p=first;
while(【1】){c++;【2】;}
A.p=p->next; B.p=p->next C.p->next->next D.p->next=p
【答案】A
23.已知head指向一个带头结点的单向链表,链表中的每个结点包含数据域(data)和指针域(next),数据域为整型,以下程序段的功能是求出链表中所有链结点数据域的和的值,并作为函数值返回。请为第【1】空选择填空。( )
struct link{int data;struct link *next;};
int sum(【1】)
{struct link *p;int s=0;p=head->next;
while(p){【2】;p=p->next;}return s;}
main()
{struct link *head;
……
printf("%d",sum(head));getch();}
A. int *head B. struct link head
C. link *head D.struct link *head
【答案】D
24.已知head指向一个带头结点的单向链表,链表中的每个结点包含数据域(data)和指针域(next),数据域为整型,以下程序段的功能是求出链表中所有链结点数据域的和的值,并作为函数值返回。请为第【2】空选择填空。( )
struct link{int data;struct link *next;};
int sum(【1】)
{struct link *p;int s=0;p=head->next;
while(p){【2】;p=p->next;}return s;}
main()
{struct link *head;
……
printf("%d",sum(head));getch();}
A. s=p->data B. s+=p->data
C. s+=p->next D.s+=data
【答案】B
25.已知head指向单链表的第一个结点,以下程序调用函数print的功能是输出这一单向链表。请为第【1】空选择填空。( )
struct student{int info;struct student *link;};
void print(struct student *head)
{ struct student *p;p=head;
if(head!=NULL)
do{printf("%d",p->info);【1】;}while(【2】);}
main( ){ struct student *head;……print(head);……}
A. p=p->next B. p->link C. p=p->link D. head=p->link
【答案】C
26.已知head指向单链表的第一个结点,以下程序调用函数print的功能是输出这一单向链表。请为第【2】空选择填空。( )
struct student{int info;struct student *link;};
void print(struct student *head)
{ struct student *p;p=head;
if(head!=NULL)
do{printf("%d",p->info);【1】;}while(【2】);}
main( ){ struct student *head;……print(head);……}
A. p==NULL B. !(p!=NULL) C. p!=1 D. p!=NULL
【答案】D
二、阅读程序,修改程序中的错误,不得增行或删行,也不得更改程序结构。请指出错误代码所在的行号,并给出该行修改后的程序代码。每处3分,共9分)
27.下列函数的功能是:函数del实现删除动态链表中指定的结点。函数中存在三措错误,请改正。
L1 struct student
L2 {int num;
L3 float score;
L4 struct student *next;};
L5 int n;
L6 struct student *del(struct student *head,long num)
L7 {struct student *p1,*p2;
L8 if(head==NULL)
L9 {printf("
list null!
");
L10 return head;}
L11 p1=head;
L12 while(num!=p1->num||p1->next!=NULL)
L13 {p2=p1;
L14 p1=p1->next;}
L15 if(num!=p1->num)
L16 {if(p1==head) head=p1->next;
L17 else p2->next=p1->next;
L18 printf("delete:%d
",num);
L19 n=n-1;}
L20 else printf("%d not been found!
",num);
L21 return (*head);}
答案:
L12 while(num!=p1->num&&p1->next!=NULL)
L15 if(num==p1->num)
L21 return head;}
三、程序填空。按照题目要求,将正确的内容填入相应位置,使得程序完整(每空3分,共39分)
28.下列程序段的功能是:13个人围成一圈,从第1个人开始顺序报号1、2、3,凡是报到3的人退出圈子。找出最后留在圈子里的人原来的序号。请填空。
#include <stdio.h>
#include<conio.h>
#define N 13
struct person
{int number;
int nextp;}link[N+1];
main()
{int i,count,h;
for(i=1;i<=N;i++)
{if(i==N) link[i].nextp=1;
else【1】;
link[i].number=i;}
count=0;
h=N;
printf("sequence that person leave the circle:
");
while(【2】)
{i=0;
while(i!=3)
{h=link[h].nextp;
if(【3】) i++;}
printf("%4d",link[h].number);
link[h].number=0;
【4】;}
printf("
The last one is:");
for(i=1;i<=N;i++)
if(link[i].number)
printf("%3d",link[i].number);
printf("
");getch();}
答案:①link[i].nextp=i+1 ②count<N-1
③link[h].number ④count++
29. 下列程序的功能是:通过函数creat建立一个有3名学生数据的单向动态链表,输入数据后,通过函数print将链表中各结点的数据依次输出。请填空。
#include <stdio.h>
#include<conio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{int num;
float score;
struct student *next;};
int n;
struct student *creat()
{struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=【1】;
scanf("%d,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1) head=p1;
else【2】;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%d,%f",&p1->num,&p1->score);}
p2->next=NULL;
return head;}
void print(struct student *head)
{struct student *p;
printf("
Now,These %d records are:
",n);
p=head;
if(【3】)
do{printf("%d %5.1f
",p->num,p->score);
p=p->next;
}while(p!=NULL);}
main()
{【4】;
head=creat();
print(head);
getch();}
答案:①(struct student *)malloc(LEN) ②p2->next=p1
③head!=NULL ④struct student *head
30.下列程序的功能是:通过调用函数fun(),将单向链表逆置。若原链表从头至尾的结点数据域依次为1、2、3、4、5;逆置后,从头至尾的结点数据域依次为5、4、3、2、1。请填空。
#include <stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
#define N 5
typedef struct node{int data;【1】;}NODE;
NODE *fun(NODE *h)
{NODE *p,*q,*r;
p=h;
if(p==NULL) return NULL;
q=p->next;
p->next=NULL;
while(【2】)
{r=q->next;【3】;
p=q;q=r;}
return p;}
NODE *creatlist(int a[ ])
{NODE *h,*p,*q;
int i;
h=NULL;
for(i=0;i<N;i++)
{【4】;
q->data=a[i];
q->next=NULL;
if(【5】)
{h=p=q;}
else {p->next=q;p=q;}}
return h;}
void outlist(NODE *h)
{NODE *p;p=h;
if(p==NULL) printf("The list is NULL!
");
else {printf("Head");
do
{printf("->%d",p->data);
p=p->next;}while(p!=NULL);
printf("->End
");}}
main()
{NODE *head;
int a[N]={1,2,3,4,5};
head=creatlist(a);
printf("
The original list:
");
outlist(head);
head=fun(head);
printf("
The list after inverting:
");
outlist(head);
getch();}
答案:①struct node *next ②q ③q->next=p
④q=(NODE *)malloc(sizeof(NODE)) ⑤h==NULL
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
$$