哪位对链表了解的进来看下
这主要是用链表进行2个多项式的加减//*两个多项式的输入必须依照幂次数的升序依次输入*//
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LENTH sizeof(struct polyfactor)
#define NULL 0
typedef struct Polyfactor //*构造多项式因子*//
{
int coeff; //*多项式因子的系数*//
int power; //*多项式因子的幂次数*//
struct polyfactor *next; //*用于指向下一个多项式因子的指针*//
}Polyfactor;
struct Polyfactor *createpolyn(struct Polyfactor *head) //*建立链表存储多项式*//
{
struct Polyfactor *p1=NULL; //*设置两个指针用于创建链表*//
struct Polyfactor *p2=NULL;
int m,n;
while(0<=n&&n>=9&&0<=m&&m>=9) //*当输入不是数字时多项式输入结束*//
{
printf("请输入多项式因子的系数:");
scanf("%d\n",m);
printf("请输入多项式因子的幂次数:");
scanf("%d\n",n);
if(head=NULL)
{
if(p1=((struct Polyfactor *)malloc(LENTH))==NULL) exit(0);
head=p1;
p2=p1;
}
else
if(p1=((struct Polyfactor *)malloc(LENTH))==NULL) exit(0);
else
p2.next=p1;
p2=p1;
p2.coeff=m;
p2.power=n;
} //*多项式的输入*//
下面是主函数
void main()
{
struct Polyfactor *p,*p1,*p2,*p3=NULL;
struct Polyfactor *head1,*head2;
struct Polyfactor *createpolyn(struct Polyfactor *head1);
struct Polyfactor *addpolyn(struct Polyfactor *head,struct Polyfactor *p1,struct Polyfactor *p2,struct Polyfactor *p3);
struct Polyfactor *delpolyn(struct Polyfactor *head,struct Polyfactor *p1,struct Polyfactor *p3);
head1=p1=createpolyn(head1);
head2=p2=createpolyn(head2);
do
{
if(p1.power==p2.power)
if(p1.coeff==p2.coeff)
head1=delpolyn(head1,p1,p3);
else
p1.coeff+=p2.coeff;
else
head1=addpolyn(head1,p1,p2,p3);
p3=p1;
p1++;
p2++;
}while(p1&&p2);
while(p2)
head1=addpolyn(head1,p1,p2,p3);
p=head1;
do
{
printf("%dX^%d",p.coeff,p.power);
printf("+");
p++;
}while(p);
}
F:\vc6.0\MSDev98\Bin\123.cpp(27) : error C2027: use of undefined type 'polyfactor'
F:\vc6.0\MSDev98\Bin\123.cpp(11) : see declaration of 'polyfactor'
F:\vc6.0\MSDev98\Bin\123.cpp(27) : error C2440: '=' : cannot convert from 'bool' to 'struct Polyfactor *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
F:\vc6.0\MSDev98\Bin\123.cpp(32) : error C2027: use of undefined type 'polyfactor'
F:\vc6.0\MSDev98\Bin\123.cpp(11) : see declaration of 'polyfactor'
F:\vc6.0\MSDev98\Bin\123.cpp(32) : error C2440: '=' : cannot convert from 'bool' to 'struct Polyfactor *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
F:\vc6.0\MSDev98\Bin\123.cpp(46) : error C2181: illegal else without matching if
F:\vc6.0\MSDev98\Bin\123.cpp(47) : error C2228: left of '.next' must have class/struct/union type
F:\vc6.0\MSDev98\Bin\123.cpp(84) : error C2228: left of '.coeff' must have class/struct/union type
F:\vc6.0\MSDev98\Bin\123.cpp(84) : error C2228: left of '.power' must have class/struct/union type
能帮我看看是怎么回事?还有一些函数的说明没发上来 没什么影响
[ 本帖最后由 清风拂晓 于 2010-10-17 11:04 编辑 ]