#include <stdio.h> #include <stdlib.h> #include <assert.h> #define MALLOC (Node*)malloc(sizeof(Node));//为书写方便定义宏 typedef struct { int num; int score; }student; typedef struct NODE { struct NODE * link; student stu; }Node; Node* createnode(student *s)//创建节点 { Node* p; p = MALLOC; assert(p != NULL);//对内存申请是否成功断言 p->stu = *s; p->link = NULL; return p; } void freelink(Node* s)//释放内存 { if (s == NULL) return; Node * p=s, * q; while (p->link != NULL) { q = p->link; p->link = q->link; free(q);//释放第二个及之后的节点 } free(s);//释放第一个节点 } Node* createlink(int k)//创建链表 { assert(k!=0); Node* root, * past, * current; int i; student s = { 0,0 }; scanf("%d %d", &s.num, &s.score); root = createnode(&s); past = root; for (i = 0; i < k-1; i++) { scanf("%d %d", &s.num, &s.score); current = createnode(&s); past->link = current; past = current; } return root; } int main() { Node* root; root=createlink(10); Node* p=root;int r=0,i=0;//r为实部,i为虚部 while (p!= NULL) { r+=p->stu.num; i+=p->stu.score; p=p->link; } printf("%d+%di",r,i); freelink(root); return 0; }
0.0分
1 人评分
上车人数 (C语言代码)浏览:1176 |
C语言程序设计教程(第三版)课后习题8.1 (C语言代码)浏览:718 |
简单的a+b (C语言代码)浏览:548 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:574 |
WU-链表数据求和操作 (C++代码)浏览:1313 |
C语言程序设计教程(第三版)课后习题9.10 (C语言代码)浏览:552 |
母牛的故事 (C语言代码)浏览:549 |
愚蠢的摄影师 (C++代码)浏览:936 |
1050题解(结构体数组与结构体指针的使用)浏览:1107 |
Quadratic Equation (C语言代码)浏览:988 |