简体   繁体   English

有两个结构的多项式链表及其问题

[英]polynomial linked list with two structs and its problems

#include <iostream>
using namespace std;
struct Term;
struct Node;

typedef Term* termPtr;
typedef Node* list;

list cons(int degree,int coeff, list p);

struct Term
{
    int degree;
    int coeff;
};

struct Node
{
    termPtr term;
    list link;
};
class polynomial
{
private:
    list poly;
static const int VARIABLE_X='X';
char variable;
public:
    polynomial():poly(NULL),variable(VARIABLE_X){};
    polynomial(int coef,int deg);
    polynomial insert (termPtr t,list p);
    int degree() const;
    int coeff(int n) const;
    void setPrintVariable (char x){variable=x;}
    char getPrintVariable()const { return variable;}
    friend const polynomial readPoly();
    friend void printPoly(polynomial a);
    void deletePoly();
    friend const polynomial operator +(const polynomial &a,const polynomial &b);
    friend const polynomial operator *(const polynomial &a,const polynomial &b);
 };

 polynomial::polynomial(int c,int d)
 {
if(poly == NULL)//compiler doesnt understand this part
    poly = cons(c,d,poly);
    else // i put my cons here just to make the poly
            poly=cons(c,d,poly);

}
list cons(int c,int d, list p)
{
    termPtr aterm = new Term;
    aterm->coeff=c;
    aterm->degree=d;
    list q = new Node;
    q->term = aterm;
    q->link = p;
    return q;
} 

void printPoly (polynomial a)
{
cout<<"[";
if(a.poly == NULL)
    cout<<"]";
else
    while(a.poly != NULL)
    {
        cout<<"("<<a.poly->term->coeff<<" X "<<a.poly->term->degree;
        a.poly=a.poly->link ; 
    }
cout<<endl;
}

This code is using linked lists to store polynomials. 此代码使用链接列表存储多项式。 One struct is for the polynomial degree and coeff; 一种结构是多项式的度数和系数。 another struct is for making a Node in order to create a linked list. 另一个结构是用于创建节点以创建链接列表。

I have two problems with the code: 我的代码有两个问题:

  1. An empty polynomial which is NULL but in the constructor my condition statement doesn't find it out. 一个空多项式,它为NULL,但是在构造函数中,我的condition语句找不到它。

  2. Why my print method doesn't work. 为什么我的打印方法不起作用。

I have this problem in the print method 我在打印方法中有此问题

Unhandled exception at 0x00c1166b in polynomial.exe: 0xC0000005: Access violation reading location 0xcccccccc. polynomial.exe中0x00c1166b处未处理的异常:0xC0000005:访问冲突读取位置0xcccccccc。

The reason poly == NULL is not true is that poly is uninitialized. poly == NULLtrue的原因是poly未初始化。 The initialization poly(NULL) only occurs in the other constructor, which is not used. 初始化poly(NULL)仅在其他构造函数中发生,而未使用。

Probably it is best to eliminate the list type in favor of std::list (actually, it is a very bad idea to use list as an identifier in conjunction with using namespace std; ). 可能最好是用std::list消除list类型(实际上,结合使用list作为标识符和using namespace std;是一个非常糟糕的主意)。

class polynomial
{
private:
    list< Term > poly;

Now poly is default-constructed so poly->empty() is true , and you don't have to do anything. 现在poly是默认构造的,因此poly->empty()true ,您无需执行任何操作。

For cons you can call list::push_back or list::insert ; 对于cons您可以调用list::push_backlist::insert ; general catenation of lists is list::splice . 列表的一般分类是list::splice To iterate over the list use the ++ operator on an object of type list< Term >::iterator . 要遍历列表,请在list< Term >::iterator类型的对象上使用++运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM