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

清风拂暮(木)
2010-10-17 11:35
清风拂晓
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
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:0 
果然改了之后就少了很多了

清风拂暮(木)
2010-10-17 17:35
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:0 
你的我会看下 我又改了下没显示错误 但一运行就会提示已停止(我自己的那个代码):
//*两个多项式的输入必须依照幂次数的升序依次输入*//
#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;       //*用于指向下一个多项式因子的指针*//
};              

struct Polyfactor *createpolyn(struct Polyfactor *head)     //*建立链表存储多项式*//
{
 struct Polyfactor *p1=NULL;                 //*设置两个指针用于创建链表*//
 struct Polyfactor *p2=NULL;
 int m=0,n=0;
 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;
     }
 }                            //*多项式的输入*//
 return head;
}
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;
}
void main()
{
    struct Polyfactor *p,*p1,*p2,*p3=NULL;
    struct Polyfactor *head1=NULL,*head2=NULL;
    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=p1->next;
        p2=p2->next;
    }while(p1&&p2);
    while(p2)
        head1=addpolyn(head1,p1,p2,p3);
    p=head1;
    do
    {
        printf("%dX^%d",p->coeff,p->power);
        printf("+");
        p=p->next;
    }while(p);
}

[ 本帖最后由 清风拂晓 于 2010-10-17 18:23 编辑 ]

清风拂暮(木)
2010-10-17 17:42
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:0 
第一次用链表 所以发现很多问题。。。看来是实践少了点

清风拂暮(木)
2010-10-17 17:43
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:0 
你那个程序也是用C写的?

清风拂暮(木)
2010-10-17 17:44
快速回复:哪位对链表了解的进来看下
数据加载中...
 
   



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

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