Note that there are some explanatory texts on larger screens.

plurals
  1. POpolynomial linked list with two structs and its problems
    primarykey
    data
    text
    <pre><code>#include &lt;iostream&gt; 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 &amp;a,const polynomial &amp;b); friend const polynomial operator *(const polynomial &amp;a,const polynomial &amp;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-&gt;coeff=c; aterm-&gt;degree=d; list q = new Node; q-&gt;term = aterm; q-&gt;link = p; return q; } void printPoly (polynomial a) { cout&lt;&lt;"["; if(a.poly == NULL) cout&lt;&lt;"]"; else while(a.poly != NULL) { cout&lt;&lt;"("&lt;&lt;a.poly-&gt;term-&gt;coeff&lt;&lt;" X "&lt;&lt;a.poly-&gt;term-&gt;degree; a.poly=a.poly-&gt;link ; } cout&lt;&lt;endl; } </code></pre> <p>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.</p> <p>I have two problems with the code:</p> <ol> <li><p>An empty polynomial which is NULL but in the constructor my condition statement doesn't find it out.</p></li> <li><p>Why my print method doesn't work.</p></li> </ol> <p>I have this problem in the print method</p> <blockquote> <p>Unhandled exception at 0x00c1166b in polynomial.exe: 0xC0000005: Access violation reading location 0xcccccccc.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload