| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 947 人关注过本帖
标题:哪位对链表了解的进来看下
只看楼主 加入收藏
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
结帖率:96.15%
收藏
已结贴  问题点数:40 回复次数:17 
哪位对链表了解的进来看下
这主要是用链表进行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 编辑 ]
搜索更多相关主题的帖子: 链表 
2010-10-17 10:58
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:0 
沙发

清风拂暮(木)
2010-10-17 10:58
sunmingchun
Rank: 4
来 自:安徽-滁州
等 级:业余侠客
帖 子:198
专家分:277
注 册:2010-4-2
收藏
得分:5 
楼主你的问题太对了吧!真的不好改,有时间好好看看你的程序。
2010-10-17 11:13
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:0 
不一定要你改 你说下那原因就可以了

清风拂暮(木)
2010-10-17 11:35
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:35 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#define NULL 0
typedef struct Polyfactor                       //*构造多项式因子*//
{
    int coeff;                        //*多项式因子的系数*//
    int power;                       //*多项式因子的幂次数*//
    struct Polyfactor *next;       //*用于指向下一个多项式因子的指针*//
};
#define LENTH sizeof(struct  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)
        {
            p1=(struct Polyfactor*)malloc(LENTH);
            if(p1==NULL)
                exit(0);
            head=p1;
            p2=p1;
        }
        else
        {       
            p1=(struct Polyfactor*)malloc(LENTH);
            if(p1==NULL)
                exit(0);
            else
                p2->next=p1;
            p2=p1;
            p2->coeff=m;
            p2->power=n;
        }
    }
}                            //*多项式的输入*//

int 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);

   
}
你的addpolyn函数,delpolyn函数呢

If You Want Something, Go Get It, Period.
2010-10-17 11:58
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:0 
好多低级错误啊!

If You Want Something, Go Get It, Period.
2010-10-17 11:58
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:0 
struct Polyfactor *addpolyn(struct Polyfactor *head,struct Polyfactor *p1,struct Polyfactor *p2,struct Polyfactor *p3)    //*对链表进行插入,其中p3指向P1的前驱*//
{
    if(p1.power<p2.power)
    {
        p2.next=p1.next;
        p1.next=p2;
    }
    else
    {
        p2.next=p1;
        p3.next=p2;
    }
    return head;
 
 }
struct Polyfactor *delpolyn(struct Polyfactor *head,struct Polyfactor *p1,struct Polyfactor *p3)               //*对链表进行删除,其中P3指向要删除点的前驱*//
{
    p3.next=p1.next;                                     //*把要删除点的后继赋值给删除点的前驱的后继*//
    return head;
}

清风拂暮(木)
2010-10-17 16:43
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:0 
已经补上了再来看看吧

清风拂暮(木)
2010-10-17 16:44
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:0 
刚试了你改之后的程序还是有很多问题
而且多了一些之没有的

清风拂暮(木)
2010-10-17 17:01
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:0 
真的无从改起!先不谈你的算法是否错误!哎!!!!好多错误,除了很多简单的错误还有一些很致命的错误,指针访问是用-> ,而你用的 . ,还有什么类名本来是大写开头的,居然有的地方是小写,更让人郁闷的是你的构造链表,没输入m,n你就开始用while判断,而判断语句呢!居然写出了while(0<=n&&n>=9&&0<=m&&m>=9),这条语句,my god,先是掉括号,你不知道&&优先级很高的啊,你不加括号先执行n&&n,这是什么,还有
0<=n有这种写法吗????? 更多的还有内存的分配问题!真的无法看!!!
难道这就是你写的吗?是不是有失水准点!

我帮你写下!

If You Want Something, Go Get It, Period.
2010-10-17 17:27
快速回复:哪位对链表了解的进来看下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014378 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved