【9-2】(2)程序填空:建立学生链表,并对3个学生的成绩求和。填完完成本题。#include struct STU{int score;struct STU *next;};int main(){struct STU s[3]={{90,'\0'},{85,'\0'},{95,'\0'}};struct STU *p=s,*q;int sum;s[0].next=【1】;s[1].next=s+2;q=p->next;sum=p->score;sum+=【2】;sum+= 【3】;printf("sum=%d\n",sum);return 0;}
【10-2】程序改错:用程序读出上题的文件x.txt中的所有数据,输出到显示器上,并求他们的和。修改程序中的错误后运行程序,完成本题。【答案:1060】#include int main(){int i,sum=0;/**********【1】FOUND**********/flie *px;/**********【2】FOUND**********/px=fopen("d:\\x.txt","w");while(!feof(px)){/**********【3】FOUND**********/fscanf(px,"%d",i);sum=sum+i;}printf("\nsum=%d\n",sum);fclose (px);return 0;}
程序填空:创建一个包含5个节点的链表,data项为5个整数,并输出链表。#include #include typedef struct list{int data;struct list *next;}LIST;int main(){LIST *ptr,*head;int num,i;ptr=(LIST*)malloc(sizeof(LIST));head=【1】;printf("请输入5个整数,以空格分隔:\n");for(i=0;i<=4;i++){scanf("%d",&num);ptr->data=num;ptr->next=(LIST*)malloc(sizeof(LIST));if(i==4)【2】 ;else ptr=ptr->next;}ptr=【3】;while(ptr!=NULL){printf("The value is==>%d\n",ptr->data);ptr=【4】;}return 0;}
程序填空:以下程序中pw指向一个单链表,函数display显示输出该单链表中所有节点的字符串。#include struct Node{char sub[3];struct Node *next;}w={"AA"},x={"BB"},y={"CC"};void display(struct Node *h){struct Node *p;p=h;while(【1】){printf("%s\n",p->sub);p=【2】;}}int main(){struct Node *pw=&w;w.next=&x;x.next=&y;y.next=【3】;display(pw);return 0;}