第三讲 结构体习题 联系客服

发布时间 : 星期五 文章第三讲 结构体习题更新完毕开始阅读6c559d0e7cd184254b353547

. .

sum(head); . . .

}

sum( (1) )

{ struct link *p; int s=0;

p=head->next;

while(p) {s+= (2) ; p= (3) ; } return(s);

}

4.已知head指向单链表的第一个结点,以下函数完成往降序单向链表中插入一个结点,

插入后链表仍有序。请填空。**

# include

struct student

{int info; struct student *link; };

struct student *insert(struct student * head, struct student * stud) { struct student * p0, * p1, * p2; p1=head; p0=stud; if(head= =NULL) {head=p0; p0->link=NULL;}

else while(p0->infoinfo)&&(p1->link!=NULL)) {p2=p1; p1=p1->link; }

if(p0->info>=p1->info) { if(head= =p1) { (1) ; head=p0; } else {p2->link=p0; (2) ; }

}

else {p1->link=p0; (3) ; } return(head); }

四、读程序写结果题

1.以下程序的运行结果是 。*

struct n {int x; char c; }; main( )

{ struct n a={10, ’x’}; func(a); printf ( “%d,%c”, a.x, a.c); } func(struct n b)

{b.x=20; b.c=’y’; }

2.以下程序的运行结果是 。* main( )

{ struct EXAMPLE { struct {int x; int y; }in int a; int b; }e;

e.a=1; e.b=2; e.in.x=e.a * e.b; e.in.y=e.a + e.b; printf(“%d,%d”, e.in.x, e.in.y); } 3.以下程序的运行结果是 。** main( )

{ static struct s1 {char c[4], *s; }s1={“abc”,”def”};

static struct s2 {char *cp; struct s1 ss1; }s2={“ghi”, {“jkl”, “mno”}}; printf(“%c,%c\\n”, s1.c[0], *s1.s);

printf(“%s,%s\\n”, s1.c, s1.s);

printf(“%s,%s\\n”, s2.cp, s2.ss1.s); printf(“%s,%s\\n”, ++s2.cp, ++s2.ss1.s); }

4.以下程序的运行结果是 。* struct s{ int a; float b; char *c; } main( )

{static struct s x={19,83.5,”zhang”}; struct s *px=&x; printf(“%d %.1f %s\\n”, x.a, x.b,x.c); printf(“%d %.1f %s\\n”, px->a, (*px).b,px->c); printf(“%c %s\\n”, *px->c-1, &px->c[1]); }

5.以下程序的运行结果是 。* struct stru {int x; char c; };

main( )

{ struct stru a={10, ’x’},*p=&a; func (p); printf ( “%d,%c”, a.x, a.c); } func (struct stru *b)

{b->x=20; b->c=’y’; }

6.以下程序的执行结果是 。* #include

struct stu { int num;

char name[10]; int age;

};

void fun(struct stu *p) {

printf(\ }

void main(void) {

struct stu students[3]={ {9803,\

fun(students+2); }

五、编程题

1.试利用结构体类型编制一程序,实现输入一个学生的数学期中和期末成绩,然后计算并输出其平均成绩。* 2.试利用指向结构体的指针编制一程序,实现输入三个学生的学号、数学期中和期末成绩,然后计算其平均成绩并输出成绩表。* 3.请编程建立一个带有头结点的单向链表,链表结点中的数据通过键盘输入,当输入数据为-1时,表示输入结束。(链表头结点的data域不放数据,表空的条件是 ph->next = =NULL)。**

{9801,\

{9802,\

4.已知head指向一个带头结点的单向链表,链表中每个结点包含字符型数据域(data)和指针域(next)。请编写函数实现在值为a的结点前插入值为key的结点,若没有值为a的结点,则插在链表最后。**

第三讲 答案

一、选择题

1~5 D D C D C 6~10 A A D 11~15 B D B A D 二、填空题 1. 2 3 2. 12 6.0 3. (double *) 4. 22

三、程序填空题

1.(1)p<=person+2 (2)old=p->age (3)q->name, q->age 2.(1)p!=NULL (2)c++

(3)p->next

3.(1)struct link *head (2)p->data (3)p->next

4.(1)p0->link=head (2)p0->link=p1 (3)p0->link=NULL 四、读程序写结果

1. 10,x 2. 2,3

3. a,dabc,defghi,mnohi,no 4. 19 83.5 zhang19 83.5 zhangy hang 5. 20,y

6. Zhao

二、编程题

1.main()

{struct study {int mid;

int end; int average;

}math;

scanf(“%d %d”,&math.mid,&math.end);

B B math.average=(math.mid+math.end)/2; printf(“average=%d\\n”,math.average); }

2.struct stu {int num; int mid; int end; int ave; }s[3]; main( )

{struct stu *p;

for(p=s;p

{scanf(“%d %d %d”,&(p->num),&(p->mid),&(p->end)); p->ave=(p->mid+p->end)/2; }

for(p=s;p

printf(“%d %d %d %d\\n”,p->num,p->mid,p->end,p->ave); }

3. #include

struct list {int data; struct list *next; }; struct list *creatlist( )

{struct list *p,*q,*ph; int a;

ph=(struct list *)malloc(sizeof(struct list)); p=q=ph;

printf(“Input an integer number,enter –1 to the end:\\n”); scanf(“%d”,&a);

while(a!=-1)

{p=(struct list *)malloc(sizeof(struct list)); p->data=a; q->next=p; q=p;

scanf(“%d”,&a); }

p->next=’\\0’; return(ph); }

main( )

{struct list *head; head=creatlist(); }

4.typedef char datatype; typedef struct node {datatype data;

struct node *next;

}linklist;

INSERT1(linklist *head,datatype a,datatype key) {linklist *s,*p,*q;

s=malloc(sizeof(linklist)); s->data=key;

q=head; p=head->next;

if(p= =NULL)

{s->next=p; q->next=s; return; } while((p->data!=a)&&(p->next!=NULL)) {q=p; p=p->next; } if(p->data= =a)

{s->next=p; q->next=s; } else