解题思路:
注意事项:
参考代码:
#include <stdio.h> #include <malloc.h> typedef struct node { int real; int unreal; struct node *next; //指向后继结点 }Lnode; void create(Lnode *&L) { Lnode *s,*r; int i; L = (Lnode *)malloc(sizeof(Lnode)); //创建头结点 r = L; for(i = 0; i < 10; i++) { s = (Lnode *)malloc(sizeof(Lnode)); //分配地址空间,建立指针变量 scanf("%d %d",&s->real, &s->unreal); //赋值输入 r->next = s; //*s插入到 *r后面 r = s; //指针右移 } r->next = NULL; } void sum(Lnode *&L, int &sumreal, int &sumunreal) { Lnode *s, *r; int i; r = L; for(i = 0; i < 10; i++) { s = r->next; sumreal += s->real; sumunreal += s->unreal; r = s; } printf("%d+%di",sumreal, sumunreal); } void DestroyList(Lnode *&L) { Lnode *pre = L, *p = L->next; while(p != NULL) { free(pre); pre = p; p = pre->next; } free(pre); } int main() { Lnode *L; create(L); int sumreal = 0, sumunreal = 0; sum(L, sumreal, sumunreal); //求和 DestroyList(L); return 0; }
0.0分
0 人评分