关于单链表的一些问题

wzmlove007 2009-04-21 10:40:23
大家看看,这是我写的一个程序,为什么总是有问题?我改了又改,但是还是有问题,谁能帮我解决下子

#include "stdafx.h"
#include<iostream.h>
struct Node
{
float ceof;
int exp;
Node *next;
};
Node *CreateList(Node *head,int n)
{
for(int i=0;i<n;i++)
{
cout<<"请输入系数和项数:";
Node *p=new Node;
cin>>p->ceof;
cin>>p->exp;
p->next=NULL;
p->next=head->next;
head->next=p;
}
return head;
}
Node *AddPoly(Node *a,Node *b)
{
Node *head=new Node;
Node *tail=new Node;
head=tail;
a=a->next;
b=b->next;
while(a!=NULL&&b!=NULL)
{
if(a->exp<b->exp)
{
tail->next=a;
tail=a;
a=a->next;
}
else if(a->exp>b->exp)
{
tail->next=b;
tail=b;
b=b->next;
}
else
{
float x=a->ceof+b->ceof;
if(x==0)
{
a=a->next;
b=b->next;
}
else
{
a->ceof=x;
tail->next=a;
tail=a;
a=a->next;
b=b->next;
}
}
}
return head;
}
void PrintList(Node *head)
{
head=head->next;
while(head!=NULL)
{
cout<<head->ceof<<"x^"<<head->exp<<"+";
head=head->next;
}
}
int main(int argc, char* argv[])
{
Node *p=new Node;
Node *q=new Node;
cout<<"请输入二项式的项数:";
int n;
cin>>n;
p=CreateList(p,n);
cout<<"请输入二项式的项数:";
int m;
cin>>m;
q=CreateList(q,m);
Node *sum;
sum=AddPoly(p,q);
PrintList(sum);
return 0;
}
...全文
85 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ericming200409 2009-04-22
  • 打赏
  • 举报
回复
正事 1楼说的对
josephwuxiaameng 2009-04-21
  • 打赏
  • 举报
回复
应该是在CreateList那里出了点问题吧。

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧