| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 947 人关注过本帖
标题:哪位对链表了解的进来看下
只看楼主 加入收藏
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:0 
程序代码:
#include <iostream>
using namespace std;
class Term
{
private:
    int coef;
    int exp;
    Term *link;
    friend ostream & operator << (ostream & ,const Term &);
    friend class Polynominal;
public:
    Term(int c,int e):coef(c),exp(e)
    {  link=0;  }
    Term(int c,int e,Term* nxt):coef(c),exp(e)
    {  link=nxt;  }
    Term* InsertAfter(int c,int e)
    {
        link=new Term(c,e,link);
        return link;
    }

};
ostream &operator << (ostream & out,const Term& val)
{
    if(val.coef==0)
        return out;
    out<<val.coef;
    switch(val.exp)
    {
    case 0:break;
    case 1:out<<"X";break;
    default:out<<"X^"<<val.exp;break;
    }
    return out;
}
class Polynominal
{
private:
    Term* thelist;
    int n;
    friend ostream & operator <<(ostream&, const Polynominal &);
    friend istream & operator >>(istream&,Polynominal &);
    friend Polynominal & operator +(Polynominal &, Polynominal &);
    friend Polynominal & operator * (Polynominal &a ,Polynominal &b);
public:
    Polynominal()
    {
        thelist=new Term(0,-1);
        thelist->link=thelist;
        n=0;
    }

    // 合并次数相同的项, 只需在加法中调用
    void simplify()
    {
        Term* target, *iter, *post_iter;
        target = thelist->link;
        while(target->exp != -1)
        {
            post_iter = target;
            iter = target->link;
            while(iter->exp != -1)
            {
                if(iter->exp == target->exp)
                {
                    target->coef += iter->coef;
                    post_iter->link = iter->link;
                    delete iter;
                    iter = post_iter->link;
                }
                else
                {
                    iter = iter->link;
                    post_iter = post_iter->link;
                }
            }
            target = target->link;
        }
    }


    void AddTerms(istream& in)
    {
        Term* q=thelist;
        int c,e;
        for(;;)
        {
            cout<<"Input a term(coef,exp):\n";
            cin>>c>>e;
            if(e<0) break;
            q=q->InsertAfter(c,e);
            n++;
        }
    }
    void Output(ostream& out) const
    {
        int first=1;
        Term *p=thelist->link;
        cout<<"The Polynominal is :\n";
        for(;p!=thelist; p=p->link)
        {
            if(!first&&(p->coef>0)) out<<"+";
            first=0;
            out<<*p;
        }
        cout<<endl;
    }
    void PolyAdd(Polynominal& r)
    {
        Term *q,*q1=thelist,*p;
        p=r.thelist->link;
        q=q1->link;
        while(p->exp>=0)
        {
            while(p->exp< q->exp)
            {
                q1=q;q=q->link;
            }
            if(p->exp==q->exp)
            {
                q->coef=q->coef+p->coef;
                if(q->coef==0)
                {
                    q1->link=q->link;delete q;
                    q=q1->link;
                }
                else
                {
                    q1=q;q=q->link;
                }
            }
            else
            {
                n++;
                q1=q1->InsertAfter(p->coef,p->exp);
            }
            p=p->link;
        }
        //=======================================================
        simplify();
        //=======================================================
    }
   
};
ostream& operator << (ostream& out,const Polynominal &x)
{
    x.Output(out);
    return out;
}
istream& operator >> (istream& in,Polynominal &x)
{
    x.AddTerms(in);
    return in;
}
Polynominal& operator + (Polynominal &a ,Polynominal &b)
{
    a.PolyAdd(b);
    return a;
}

int main()
{
    Polynominal p,q;
    cin>>p;
    cout<<p;
    cin>>q;
    cout<<q;
    q+p;
    cout<<q;
}

If You Want Something, Go Get It, Period.
2010-10-17 17:31
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:0 
果然改了之后就少了很多了

清风拂暮(木)
2010-10-17 17:35
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:0 
你说少很多,指的是什么?

If You Want Something, Go Get It, Period.
2010-10-17 17:37
清风拂晓
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
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:0 
输入m的时候没什么问题,但要输入n的时候就弹出了个已停止工作的对话框,是怎么回事?

清风拂暮(木)
2010-10-17 18:25
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:0 
看来你没仔细看我上面写的话!!!你m,n输都没输就去判断了

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



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

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