双结构多项式链表及其问题
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;
}
这段代码使用链表来存储多项式。一个结构用于多项式度和系数;另一个结构体是用来创建一个Node来创建一个链表。
我的代码有两个问题:
一个空的多项式,它是NULL,但在构造函数中我的条件语句没有找到它
为什么我的打印方法不工作
我在打印方法中有这个问题
在多项式.exe中的0x00c1166b处未处理异常:0xC0000005:访问违反读取位置0xcccccccc.
poly == NULL
不是true
的原因是poly
未初始化。初始化poly(NULL)
只发生在另一个构造函数中,它没有被使用。
可能最好是消除list
类型,而使用std::list
(实际上,将list
与using namespace std;
结合使用是一个非常糟糕的主意)。
class polynomial
{
private:
list< Term > poly;
现在poly
是默认构造的,所以poly->empty()
是true
,你不需要做任何事情。
对于cons
,可以调用list::push_back
或list::insert
;列表的一般格式是list::splice
。要对列表进行迭代,可以在list< Term >::iterator
类型的对象上使用++
操作符。
相关文章:
- 链表指针问题
- 我对数据结构、双向链表有一些问题
- 在解决链表问题时创建一个额外的节点是一个好习惯吗?
- Sinlge 链表,C++,删除所有和搜索功能的问题
- C++ 从链表中删除项目时出现问题
- 如何修复我的链表读数不一致的问题?
- C++中链表实现的问题
- 双向链表问题 - 包含项目但不显示
- 嵌套的类成员变量不可访问.链表设置.V、下一个和新的问题
- 在链表中的某个点插入时出现问题,C++中的继承函数
- cpp 中的以下链表程序有什么问题?
- 单个链表,节点插入最低到最高节点值的问题
- 我在让我的代码输出整个链表时遇到问题.它只是输出我修改过的一些文本文件,而不是整个东西
- 双向链表问题(特别是复制构造函数和删除函数)
- 我在双链表上的此打印功能时遇到问题
- 我在显示链表时遇到问题
- 尝试在链表程序的末尾进行更简单的插入.小问题需要帮助
- 节点和链表问题
- c++理解问题-链表和堆栈
- C++语法问题(链表)