#include<stdio.h> #include<stdlib.h> #define N 8 typedef struct list { int data; struct list *next; }SLIST; SLIST *creatlist(int *); void outlist(SLIST *); int fun(SLIST *h) { SLIST *p;int s=0; p=h->next; while(p) { s+=p->data; p=p->next; } return s; } main() { SLIST *head; int a[N]={268,87,45,32,91,16,20,12}; head=creatlist(a);outlist(head); printf("\nsum=%d\n",fun(head)); } SLIST *creatlist(int a[]) { SLIST *h,*p,*q;int i; h=p=(SLIST *)malloc(sizeof(SLIST)); for(i=0;i<N;i++) { q=(SLIST *)malloc(sizeof(SLIST)); q->data=a[i];p->next=q;p=q; } p->next=NULL; return h; } void outlist(SLIST *h) { SLIST *p; p=h->next; if(p==NULL) printf("The list is NULL!\n"); else { printf("->%d",p->data); p=p->next; } while(p!=NULL); printf("->End\n"); }
没看懂 开学再看看
P164
时间: 2024-11-07 18:46:25