解题思路:还是前面讲的链表的经典操作
注意事项:链表的使用,注意删除节点
参考代码:
#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二级辅导-求偶数和 (C语言代码)浏览:628 |
简单的a+b (C语言代码)浏览:578 |
C语言训练-求素数问题 (C语言代码)浏览:768 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:1324 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:621 |
【排队买票】 (C语言代码)浏览:935 |
C语言程序设计教程(第三版)课后习题6.5 (C语言代码)浏览:612 |
C语言程序设计教程(第三版)课后习题8.6 (C语言代码)浏览:589 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:1476 |
【明明的随机数】 (C语言代码)浏览:833 |