【问题标题】:polynomial linked list with two structs and its problems具有两个结构的多项式链表及其问题
【发布时间】:2011-04-07 00:12:14
【问题描述】:
#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;
}

此代码使用链表来存储多项式。 一种结构用于多项式次数和系数;另一个结构是为了创建一个节点来创建一个链表。

我的代码有两个问题:

  1. 一个为 NULL 的空多项式,但在构造函数中我的条件语句没有找到它。

  2. 为什么我的打印方法不起作用。

我的打印方法有这个问题

polynomial.exe 中 0x00c1166b 处的未处理异常:0xC0000005:访问冲突读取位置 0xcccccccc。

【问题讨论】:

  • 1. “编译器不理解 poly 是 NULL”是什么意思?编译器在做什么,为什么你认为它是错误的? 2.您的打印方法在什么情况下不起作用?它有什么作用?你希望它会做什么?
  • 当我调试它时,构造中的条件(if (poly==NULL))被忽略了
  • 1. “被忽略”是什么意思?你怎么知道它被忽略了? 2. 说到忽略,你忽略了我的第二个问题。
  • 在两个参数的polynomial 构造函数中,你认为poly 的值应该是什么?为什么?
  • 对不起。我没看到。首先-我的意思是我的 poly 是空的,但它似乎不为空。我应该使用什么条件语句来确定它是否为 NULL。第二 - 我想打印 poly: Unhandled exception at 0x00c1166b in polynomial.exe: 0xC0000005: Access violation reading location 0xcccccccc.

标签: c++


【解决方案1】:

poly == NULL 不是true 的原因是poly 未初始化。初始化poly(NULL)只发生在其他构造函数中,没有用到。

可能最好去掉list 类型以支持std::list(实际上,将list 作为标识符与using namespace std; 一起使用是一个非常糟糕的主意。

class polynomial
{
private:
    list< Term > poly;

现在poly 是默认构造的,所以poly-&gt;empty()true,您无需执行任何操作。

对于cons,您可以致电list::push_backlist::insert;列表的一般连接是list::splice。要遍历列表,请在 list&lt; Term &gt;::iterator 类型的对象上使用 ++ 运算符。

【讨论】:

  • @Ben:大声笑,你今天对我很好。我想过建议std::,但偏向于避免弄乱他们的风格……
  • 我更关心“内容列表”而不是使用限定名称。
  • 是的,我不会抓到那个的。嗯……对于还不熟悉库使用哪些名称的人来说,最好从使用 std:: 而不是 using 开始。
  • 我初始化了 poly,我的一些问题得到了解决。你的休息对我来说很先进。抱歉,我无法得到它们。我的坏
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2014-11-29
相关资源
最近更新 更多