《C语言及程序设计》程序阅读——链表初步

    xiaoxiao2025-11-19  29

    返回:贺老师课程教学链接

    (1)程序下面的阅读,写出其输出结果(建议画出内存中存储的数据,使链表直观地表示出来 )

    #include <stdio.h> #include <stdlib.h> struct NODE { int num; struct NODE *next; }; int main() { struct NODE *p,*q,*r; p=(struct NODE*)malloc(sizeof(struct NODE)); q=(struct NODE*)malloc(sizeof(struct NODE)); r=(struct NODE*)malloc(sizeof(struct NODE)); p->num=10; q->num=20; r->num=30; p->next=q; q->next=r; printf("%d\n",p->num+q->next->num); return 0; }

    (2)程序下面的阅读,写出其输出结果(建议画出内存中存储的数据,使链表直观地表示出来 )

    #include <stdio.h> #include <stdlib.h> int main() { struct node { int x; struct node *next; } *p1,*p2=NULL; int a[7]= {16,9,-5,28,1,0,-3},i,s=0; for(i=0; i<7; i++) s+=a[i]; s/=7; for(i=0; i<7; i++) { if(a[i]>s) { p1=(struct node*) malloc(sizeof(struct node)); p1->x=a[i]; p1->next=p2; p2=p1; } } while(p2!=NULL) { printf("%d\n",p2->x); p2=p2->next; } return 0; }

    (3)运行下面的程序,输入games videos music shopping eating,输出是什么?

    #include <stdio.h> #include <malloc.h> #define N 5 typedef struct node { char str[20]; struct node *link; } stud; int main() { int number; stud *head=NULL; number=N; stud *p,*s; int i; head=(stud *)malloc(sizeof(stud)); head->str[0]='\0'; head->link=NULL; p=head; for(i=0; i<number; i++) { s= (stud *) malloc(sizeof(stud)); p->link=s; scanf("%s",s->str); s->link=NULL; p=s; } p=head->link; i=1; while(p!=NULL) { printf("%d: %s\n", i++, p->str); p=p->link; } return 0; } 相关资源:复旦大学C语言程序设计解答——链表部分2
    最新回复(0)