使用Structs创建链接列表 - C ++

时间:2012-03-11 16:29:04

标签: class pointers struct linked-list

我正在编写一个程序,它可以读取输入文件并将读取的数据存储在由“链接列表”链接的节点中。但是,我收到了一些错误:

  1. 在构造函数List::List()中,*((List*)this)->List::list[0] = 0
  2. 中的'operator ='不匹配
  3. 在构造函数Polynomial::Polynomial()中:*((Polynomial*)this)->Polynomial::poly = (operator new(400u), (<statement>), ...)
  4. 中的'operator ='不匹配

    我有一种感觉:我尝试通过数组访问某个节点是我出错的地方,但是,我无法理解它。

    以下是代码:

    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    enum result{success, failure};                          
    
    struct Node
     {
    
    double coefficient;                         
    int power;                              
    
    Node();                 
    Node(double coef, int pwr);     
    };
    
    struct List
    {
    Node *list[100];
    
    //Default constructor
    List();
    };
    
    Node::Node()
    {
    coefficient = 0;
    power = 0;
    }
    
    List::List()
    {
    *list[0] = NULL;
    }
    
    Node::Node(double coef, int pwr)
    {
    coefficient = coef;
    power = pwr;
    }
    
    
      class Polynomial
      {
       public:
        Polynomial();                       
        result multiply(Polynomial &p, Polynomial &q);
        result add(Polynomial p, Polynomial &q);
        void initialize(ifstream &file);
        void simplify(Polynomial &var);
        void print_poly();
        ~Polynomial();
    
    private:
        List *poly;                         //Store the pointer links in an array
        Node first_node;
        int val;
    };
    
     Polynomial::Polynomial()
    {
    *poly = new List();
    }
    
    Polynomial::void initialize(ifstream &file)
    {
    int y[20];
    double x[20];
    int i = 0, j = 0;
    
    //Read from the file
    file >> x[j];
    file >> y[j];
    
    first_node(x[j], y[j++]);                       //Create the first node with coef, and pwr
    *poly->list[i] = &first_node;                       //Link to the fist node
    
    //Creat a linked list
    while(y[j] != 0)
    {
        file >> x[j];
        file >> y[j];
        *poly->list[++i] = new Node(x[j], y[j++]);
    }
    
    val = i+1;                              //Keeps track of the number of nodes
    }
    
    
    Polynomail::result multiply(Polynomial &p, Polynomial &q)
    {
    int i, j, k = 0;
    
    for(i = 0; i < p.val; i++)
    {
        for(j = 0; j < q.val; j++)
        {
            *poly->list[k] = new Node(0, 0);
            *poly->list[k].coefficient = (p.poly->list[i].coefficient)*(q.poly->list[j].coefficient);
            *poly->list[k++].power = (p.poly->list[i].power)+(q.poly->list[j].power);
        }
    }
    
    val = k+1;                              //Store the nunber of nodes 
    return success;
    }
    
    Polynomial::void simplify(Polynomial &var)
    {
    int i, j, k = 0;
    
    //Create a copy of the polynomial
    for(j = 0; j < var.val; j++)
    {
        *poly->list[j] = new Node(0, 0);
        *poly->list[j].coefficient = var.poly->list[j].coefficient;
        *poly->list[j].power = var.poly->list[j].power;
    }
    
    //Iterate through the nodes to find entries which have the same power and add them, otherwise do nothing    
    for(k = 0; k < var.val; k++)
    {
        for(i = k; i < var.val;)
        {
            if(*poly->list[k].power == var.poly->list[++i].power)
            {
                if(*poly->list.power[0] == 0)
                {
                    NULL;   
                }
                else 
                {
                    *poly->list[k].coefficient = *poly->list[k].coefficient + var.poly->list[i].ceofficient;
                    var.poly->list[i] = Node(0, 0);
                }
            }
        }
    }
    }
    
    Polynomial::void print_pol()
    {
    int i = 0;
    for(i = 0; i < temp.val; i++)
    {
        cout << "Coefficient: " << temp.poly->list[i].coefficient << ", and " << "Power: " << temp.poly->list[i].power << endl;
    }
    }
    

1 个答案:

答案 0 :(得分:0)

问题是错误的解除引用。第34行应该是

 list[0] = NULL;   // remove the *

您尝试将值NULL分配给Node类型的变量,但您可能意味着Node的指针。 第63行也是如此。

此外,第66行可能是b:

 void Polynomial::initialize(ifstream &file) // start with return type