<

时间:2011-06-18 22:30:36

标签: c++ linked-list

在main中,列表使用插入运算符打印出来,但我得到的只是一个空行。不知道为什么。是否即使使用set_coefficient,列表中也没有任何内容存储?任何其他批评都是受欢迎的。感谢

这是司机:

#include <iostream>
#include "polynomial.h"

using namespace std;

int main(){
Polynomial* poly = new Polynomial();    

poly->set_coefficient(3,2);

poly->set_coefficient(0,2);

poly->set_coefficient(3,1);

cout << "trying to print data" << endl;
cout << *poly << endl;    
return 0;   
}

这是标题:

#ifndef _POLYNOMIAL_H_
#define _POLYNOMIAL_H_

#include <iostream>

class Polynomial {

public:

struct PolyNode {
    int coefficient, degree;
    struct PolyNode* next;      
    PolyNode(int c, int d, PolyNode* n): coefficient(c),degree(d),next(n){}
};

PolyNode* firstTerm;
Polynomial(): firstTerm(0) {} 

struct PolyNode* get_first(){
    return firstTerm;
}


//makes the term with degree d have a coefficient of c
void set_coefficient(int c, int d);     

~Polynomial();  

friend std::ostream& operator<<(std::ostream& o, const Polynomial& p);          
};

#endif

以下是实施:

#include "polynomial.h"
#include <iostream>
#include <ostream>

using namespace std;

void Polynomial::set_coefficient(int c, int d){
PolyNode* start = firstTerm;
if(c != 0 && firstTerm == 0)    
    firstTerm = new PolyNode(c,d,NULL);
else{   
    cout << "Entered set_coefficient()" << endl;
    while(start->degree != d && start->next != NULL){
        cout << "Inside set_coefficient() while loop" << endl;          
        start = start->next;        
    }       
    if(c != 0 && start == 0)    
        start = new PolyNode(c,d,0);
    else if(c!= 0 && start != 0)
        start->coefficient = c;
    else if(c == 0){
        cout << "deleting a term" << endl;          
        delete start;
    }
}   
    cout << "Leaving set_coefficient()" << endl;
}

ostream& operator<<(ostream& o,const Polynomial& p){
Polynomial::PolyNode* start = p.firstTerm;  
for(unsigned int i = 0; start->next != 0; i++){
    o << "Term " << i << "'s coefficient is: " << start->coefficient << " degree is: " << start->degree << endl << flush;
    start = start->next;
}   
return o;
}

3 个答案:

答案 0 :(得分:1)

cout << "Entered set_coefficient()" << endl;
while(start->degree != d && start->next != NULL){
    cout << "Inside set_coefficient() while loop" << endl;          
    start = start->next;        
}       
if(c != 0 && start == 0)    
    start = new PolyNode(c,d,0);
else if(c!= 0 && start != 0)
    start->coefficient = c;
else if(c == 0){
    cout << "deleting a term" << endl;          
    delete start;
}

看起来你想要一个链表,但你既不创建也不删除链接(start->next),只分配内存。没有设置链接,就没有列表。

答案 1 :(得分:1)

     if(c != 0 && start == 0)
        start = new PolyNode(c,d,0);
    else if(c!= 0 && start != 0)
        start->coefficient = c;
    else if(c == 0){
        cout << "deleting a term" << endl;
        delete start;
    }

if (c != 0 && start == 0) // this situation may occur ? you have already judged surrounding if

else if (c == 0) {
    cout << "deleting a term" << endl;
    delete start;      // that may be cause linkedList detach.
}

答案 2 :(得分:1)

这里可能存在多个错误:&gt;

ostream& operator<<(ostream& o,const Polynomial& p){
Polynomial::PolyNode* start = p.firstTerm;  
for(unsigned int i = 0; start->next != 0; i++){

这不会打印最后一个PolyNode ...