解题思路:还是前面讲的链表的经典操作

注意事项:链表的使用,注意删除节点

参考代码:

#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 人评分

  评论区

  • «
  • »