解题思路:
注意事项:
参考代码:
#include <stdio.h> #include <malloc.h> #include <stdlib.h> typedef struct node { int real; int unreal; struct node *next; }Lnode; void createList(Lnode *&L, int n) { Lnode *s, *r; int i; L = (Lnode *)malloc(sizeof(Lnode)); //头结点 r = L; //指向头结点 for(i = 0; i < n; i++) { s = (Lnode *)malloc(sizeof(Lnode)); scanf("%d %d",&s->real,&s->unreal); r->next = s; r = s; } r->next = NULL; } void sum(Lnode *&L, int &realsum, int &unrealsum,int n) { Lnode *s, *r; int i; r = L; for(i = 0; i < n; i++) { s = (Lnode *)malloc(sizeof(Lnode)); s = r->next; realsum += s->real; unrealsum += s->unreal; r = s; } } void DestroyList(Lnode *&L) { Lnode *pre = L, *p = L->next; while(p != NULL) { free(pre); pre = p; p = pre->next; } free(pre); } int main() { int n; scanf("%d", &n); //输入几个复数 Lnode *L; createList(L,n); //创建链表 int realsum = 0, unrealsum = 0; sum(L,realsum, unrealsum,n); printf("%d+%di",realsum, unrealsum); DestroyList(L); return 0; }
0.0分
0 人评分
C语言训练-求矩阵的两对角线上的元素之和 (C语言代码)浏览:3472 |
printf基础练习2 (C语言代码)浏览:647 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:543 |
【绝对值排序】 (C语言代码)浏览:832 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:781 |
A+B for Input-Output Practice (III) (C语言代码)浏览:592 |
简单的a+b (C语言代码)浏览:641 |
WU-整数平均值 (C++代码)浏览:1307 |
Wu-求圆的面积 (C++代码)浏览:1994 |
2006年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:383 |