习题7一 单选题1.设有如下定义,则表达式sizeof(y)的值是【 】。
struct data {long *ln; char c;struct data *last,*next; }y; A.7 B.9 C.13 D.17 【答案】C【解析】指针变量在内存中占用4个字节,所以sizeof(y)的值是4+1+4+4=13 2.设有以下程序段,则表达式的值不为100的是【 】。
struct st{int a;int *b;}; void main(){int m1[]={10,100},m2[]={100,200}; struct st *p,x[]={99,m1,100,m2}; p=x; ... }A.*(++p->b)B.(++p)->aC.++p->aD.(++p)->b 【答案】D【解析】根据题意,p 指向x 数组,x[0]的b 成员指向m1数组,x[1]的b 成员指向m2数组,如图7-1所示。
选项A 中p->b 指向m1[0],通过“++”运算p->b 指向m1[1],所以选项A 的表达式的值为100。
选项B 中++p 使p 指向x[1],x[1]的a 成员的值是100。
选项C 中p 指向x[0],x[0]的a 成员做“++”运算,结果为100。
选项D 中++p 使p 指向x[1],x[1]的b 成员的值是指针,所以选择选项D 。
m1m2p图7-13.设有以下定义,且如图7-2所示,指针head 和p 指向链表的第一个结点,指针q 指向新的结点,则不能把新结点插入在第一个结点之前的语句是【 】。
headpq图7-2struct node{int a;struct node *next;}*head,*p,*q;A.head=q,q->next=p;B.head=q,q->next=head;C.q->next=head,head=q;D.head=q,head->next=p;【答案】B4.设有如下结构体说明和变量定义,且如图7-3所示,指针p指向变量one,指针q指向变量two,则不能将结点two接到结点one之后的语句是【】。
p图7- 3struct node{int n;struct node *next;}one,two,*p=&one,*q=&two;A.p.next=&two;B.(*p).next=q;C.one.next=q;D.p->next=&two;【答案】A【解析】p是指向结构体的指针,因此选项D是规范的引用方式。
5.设有以下定义,且如图7-4所示建立了链表,指针p、q分别指向相邻的两个结点,下列语句中【】不能将p所指向的结点删除。
head......图7- 4struct node{int a;struct node *link;}*head,*p,*q;A.q->link=p->link;B.p=p->link,q->link=p;C.(*p).link=(*q).link;D.p=(*p).link,(*q).link=p;【答案】C6.以下选项中,能正确地将x定义为结构体变量的是【】。
A.structB.typedef struct st{int i; {int i;float j; float j;}x; }x;C.struct stD.typedef st{int i; {int i;float j; float j;} }st x; st x;【答案】A【解析】选项C语法错误。
7.若有如下定义,则sizeof(struct no)的值是【】。
struct no{int n1;float n2;union nu{char u1[6];double u2;}n3;};A.12B.14C.16D.10【答案】C【解析】4+4+max{6,8}=16。
8.设有如下定义,则下列叙述中正确的是【】。
typedef struct{int s1;float s2;char s3[80];}STU;A.STU是结构体变量名B.typedef struct是结构体类型名C.STU是结构体类型名D.struct是结构体类型名【答案】C9.设有如下定义,则引用共用体中h成员的正确形式为【】。
union un{int h;char c[10];};struct st{int a[2];union un h;}s={{1,2},3},*p=&s;A.p.un.hB.(*p).h.hC.p->st.un.hD.s.un.h【答案】B10.以下各选项欲为float定义一个新的类型名,其中正确的是【】。
A.typedef float w1;B.typedef w2 float;C.typedef float=w3;D.typedef w4=float;【答案】A二填空题1.设有以下定义,则变量s在内存中占字节。
struct st{char num[5];int age;float score;}s;【答案】13【解析】1×5+4+4=13。
2.以下程序用以输出结构体变量bt所占内存单元的字节数,请填上适当内容。
struct ps{double i;char arr[20];};void main(){struct ps bt;printf("bt size : %d\n", );}【答案】sizeof(struct ps)3.若定义了struct{int d,m,n;}a,*b=&a;,可用a.d引用结构体成员,请写出引用结构体成员a.d 的其他两种形式【1】,【2】。
【答案】【1】(*b).d 【2】b->d4.设有以下结构体类型的定义,请将结构体数组xy的定义补充完整。
struct ST{char num[10];int m;struct ST *last,*next;};xy[10];【答案】struct ST5.以下是定义链表中结点的数据类型,请将定义补充完整。
struct node{char name[20];int score;next;};【答案】struct node*6.设有以下定义,且建立了链表,如图7-5所示,指针p指向链表尾结点,指针q指向新结点,用语句可实现将新结点连接到链表尾部。
struct node{int a;struct node *link;}*head,*p,*q;【答案】(*p).link=q; (或p->link=q;)head...图7- 57.下列程序是将从键盘输入的一组字符作为结点的内容建立一个单向链表。
要求输出链表内容时与输入时的顺序相反。
填空将程序补充完整。
#include "stdio.h"#include "stdlib.h"struct node{char d;struct node *link;};void main(){struct node *head,*p;char c;head=NULL;while((c=getchar())!='\n'){p=(struct node *)malloc(sizeof(struct node));p->d=c;p->link= 【1】;head= 【2】;}p=head;while(p->link!=NULL){printf("%c->",p->d);p= 【3】;}printf("%c\n",p->d);}【答案】【1】head 【2】p 【3】p->link【解析】第一个空应该填入新结点插入链表后它的下一个结点的地址。
由于题目要求输出链表内容时与输入时的顺序相反,而且单向链表只能从头到尾输出,所以每次新结点必须作为第一个结点插入到链表,并使从前的第一个结点作为它的后续结点,这样做的结果恰好使生成的链表的顺序与输入顺序相反,因此第一个空填head。
第二个空的内容应实现头指针指向新结点,所以填p。
第三个空是在输出结点内容的循环中,输出一个结点后,指针需要后移,所以应填p->link。
8.下列函数的功能是在单向链表中查找最大值所在结点的地址。
填空将函数补充完整。
struct node{int data;struct node *next;};struct node *found(struct node *head){struct node *p,*q;int max;p=head;max=p->data;q=p;while(p->next!=NULL){ 【1】;if(p->data>max){max=p->data; 【2】;}}return q;}【答案】【1】p=p->next 【2】q=p【解析】在循环查找之前p和q都指向了第一个结点,并将第一个结点的数据假设为最大值。
在循环查找的过程中,p指针需要顺着链表不断后移,所以第一个空应填p=p->next。
q指针的作用是指向当前的最大值,这一点也可以从return q;看出,所以第二个空应填q=p。
9.设有以下定义和语句,请在printf语句中填上能够正确输出的变量及相应的格式说明。
union{int n;double x;}num;num.n=10;num.x=10.5;printf(" 【1】", 【2】);【答案】【1】%lf 【2】num.x10.下面欲为结构体类型struct st定义一个新的类型名STUDENT,请将定义补充完整。
struct st{char num[10],name[20],sex;float score;};typedef STUDENT;【答案】struct st三阅读程序,写出结果1. #include "stdio.h"struct st{int a,b;struct st *next;} x[3];void main(){int i;struct st *p;for(i=0;i<3;i++){x[i].a=i+1,x[i].b=i+2;x[i].next=&x[i+1];}x[2].next=x;for(p=x,i=0;i<3;i++){printf("%d",p->a);p=p->next;printf("%d",p->b);p=p->next;}}【答案】133224【解析】x数组是一个结构体数组,通过第一个for循环以及其后的一个语句建立了一个环状链表。