原题链接:蓝桥杯算法训练VIP-链表数据求和操作
解题思路:还是前面讲的链表的经典操作
注意事项:链表的使用,注意删除节点
参考代码:
#includeusing namespace std; typedef struct ListPlural{ int real; int imaginary; ListPlural *next; }listdata; //创建头节点 listdata *creatListHead(){ listdata * head = new listdata; if(head == NULL){ return NULL; } head -> next = NULL; return head; } //增加数据 void addData(listdata *head, const int &real, const int &imaginary){ listdata *data = new listdata; listdata * temp; data -> real = real; data -> imaginary = imaginary; for(temp = head; temp -> next != NULL; temp = temp -> next){} temp -> next = data; data -> next = NULL; } //链表的遍历 void listTraverse(listdata *head){ listdata *temp; int real = 0, imaginary = 0; for(temp = head -> next; temp != NULL; temp = temp -> next){ real += temp -> real; imaginary += temp -> imaginary; } cout << real << "+" << imaginary << "i" << endl; } //链表的删除 void listDelete(listdata *head){ listdata *temp = head -> next; while(head-> next -> next != NULL){ temp = head->next; head -> next = head -> next->next; delete temp; } temp = head -> next; delete temp; delete head; } using namespace std; //任务函数 int main() { listdata *head; int real, imaginary; head = creatListHead(); for(int i = 1; i > real,cin >> imaginary; addData(head, real, imaginary); } listTraverse(head); listDelete(head); return 0; }
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复