| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 715 人关注过本帖
标题:请高手指教!链表应用的中小问题
只看楼主 加入收藏
janevans
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-12-12
结帖率:100%
收藏
 问题点数:0 回复次数:3 
请高手指教!链表应用的中小问题
#include<iostream.h>
#include<process.h>


struct NODE{
float coef;//系数
int exponent;//指数
NODE* next;
};
typedef NODE* head_ptr;






class POLYNIMIAL{
public:
    POLYNIMIAL();
    POLYNIMIAL(POLYNIMIAL&);
    ~POLYNIMIAL();
    void insert(float ,int );
    void display();
    POLYNIMIAL operator =(const POLYNIMIAL&);
    friend POLYNIMIAL operator + (const POLYNIMIAL&,const POLYNIMIAL&);
   
private:
    NODE* seek_exponent(int n)
    {
        if (head->next==NULL){
            return NULL;
            
        }else{
        NODE* ptr=head->next;
        while(ptr!=NULL){
            if(ptr->exponent==n)
                break;
            ptr=ptr->next;
        }
        return ptr;
        }
    }
           
    head_ptr head;
};



POLYNIMIAL::POLYNIMIAL(){
    head=new NODE;
    head->next=NULL;
}


POLYNIMIAL::POLYNIMIAL(POLYNIMIAL& p){
     head=p.head;
}
POLYNIMIAL::~POLYNIMIAL(){
    NODE*ptr ;
    while(head!=NULL){
       ptr=head;
       head=head->next;
       delete ptr;
}
       
}


void POLYNIMIAL::insert(float c,int e)
{   
   
    if (seek_exponent(e)!=NULL){
        NODE* temp=seek_exponent(e);
        temp->coef+=c;
    
    }else{

    NODE* element;
    element=new NODE;
    element->coef=c;
    element->exponent=e;
    if (head->next==NULL)
    {
        head->next=element;
        element->next=NULL;
        
    }else{
        NODE*temp=head->next;
        while(temp->next!=NULL && temp->next->exponent<e )
            temp=temp->next;
        element->next=temp->next;
        temp->next=element;
    }
    }
}


void POLYNIMIAL::display()
{
    NODE*temp;
    temp=head->next;
    while(temp !=NULL)
    {
        cout<<temp->coef<<"x^"<<temp->exponent<<"+";
        temp=temp->next ;
    }
    cout<<endl;
}


POLYNIMIAL operator + (const POLYNIMIAL& p1,const POLYNIMIAL& p2)//应该是这个函数的问题,请高手指教一下怎么重载这个运算符
{  
    
    POLYNIMIAL temp;
    temp.head=p1.head;
    NODE* pt2;
    
    pt2=(p2.head)->next ;
    while(pt2!=NULL){

        float c=pt2->coef ;
        int e=pt2->exponent ;
        temp.insert (c,e);
        pt2=pt2->next ;
        
        
    }
return temp;
}


POLYNIMIAL POLYNIMIAL::operator = (const POLYNIMIAL& a){
    head=a.head ;
return *this;
}

void main()
{
   POLYNIMIAL p,p2;
   p.insert(1,2);
   p.insert (2,4);
   p.insert (3,8);
   p.insert (2,2);
   p2.insert(1,2);
   p2.insert (2,4);
   p2.insert (3,8);
  
   p2.display ();//这两个函数都运行成功。屏幕有显示
   p.display ();
    POLYNIMIAL p1;
   p1=p+p2;//这个有问题,不知道为什么。
}


刚看了下链表,试一下用链表实现多项式的相加。结果编译链接都没错。就是运行时出错,程序终止。
搜索更多相关主题的帖子: 链表 POLYNIMIAL NODE exponent 
2008-06-09 01:02
beyond0702
Rank: 1
来 自: 桂 林
等 级:新手上路
帖 子:219
专家分:0
注 册:2007-11-17
收藏
得分:0 
POLYNIMIAL POLYNIMIAL::operator = (const POLYNIMIAL& a)问题应该在这里,p+p2 返回的临时对象 你用 &a  来引用它并做为参数(可是临时对象马上就要被析构了),而之后你还调用 =,赋给 p1;试一下这里不用 引用
{
    head=a.head ;
return *this;
}

蝴 蝶 颤 动 了 翅 膀 !!!
2008-06-09 02:58
janevans
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-12-12
收藏
得分:0 
按引用调用是为了减少空间,不用建立新的副本。去掉了& 也不行。问题应该是出在了析构函数里面。系统调用+函数的时候。析构了临时对象,而临时对象是没有分配空间。所以delete错了。
2008-06-09 10:50
d9com123
Rank: 1
等 级:新手上路
帖 子:21
专家分:2
注 册:2007-9-25
收藏
得分:0 
不懂LZ写的copy构造函数,operator +, operator =中的head=p.head;  temp.head=p1.head;
head=a.head ;
这几句的意思LZ是否清楚?!
个人建议,重写下这三个函数呀。
2008-06-09 20:51
快速回复:请高手指教!链表应用的中小问题
数据加载中...
 
   



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

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