Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiplying two Polynomials in a cpp LinkedList program
    primarykey
    data
    text
    <p>I'm doing a Polynomials program in C++ that we're supposed to be implementing with a singly-linked list. And yes, this is a homework. I've worked out most of the program and I'm just stuck with my multiplication operator overloading. This is my operator* function:</p> <pre><code>LinkedList operator*(const LinkedList&amp; a, const LinkedList&amp; b) { LinkedList product; Node* nodeA = a.head; Node* nodeB = b.head; int coeff, powr; if (nodeA == NULL &amp;&amp; nodeB == NULL) return product; else if (nodeA == NULL &amp;&amp; nodeB != NULL) return b; else if (nodeA != NULL &amp;&amp; nodeB == NULL) return a; else { while (nodeA != NULL) { while (nodeB != NULL) { coeff = nodeA-&gt;getCoeff() * nodeB-&gt;getCoeff(); powr = nodeA-&gt;getPow() + nodeB-&gt;getPow(); product.addElement(coeff, powr); nodeB = nodeB-&gt;getNext(); } nodeB = b.head; nodeA = nodeA-&gt;getNext(); } } return product; } </code></pre> <p>For reference, I just add a new element to the end of the Linked List for now. </p> <p>Here is my AddElement function:</p> <pre><code>void LinkedList::addElement(int coeff, int powr) { Node *newNode = new Node(); // Set the Node's data newNode-&gt;setPowAndCoefficient(coeff, powr); newNode-&gt;setNextNode(NULL); Node *temp = head; if (temp != NULL) { // Go to the last element of the list while (temp-&gt;getNext() != NULL) { temp = temp-&gt;getNext(); } // temp is now the last element and its next element is null // Set temp's next node to be the newNode temp-&gt;setNextNode(newNode); } else head = newNode; } </code></pre> <p>Node is just my class with private data members coefficent, power and a pointer to the next node. LinkedList is my main class that includes a private Node* head member and public operator overloaded functions and several constructors. The constructor used here is the default constructor in which I just set the head to NULL. </p> <p>I put some cout statments after the second while loop and I multiplied two polynomials to test my multiply function out. </p> <p>So in this case, I have this code in my main.cpp file:</p> <pre><code>LinkedList poly1, poly2, result; // The first polynomial: 3x^3 + 7x^2 - 7 poly1.addElement(3, 3); poly1.addElement(7, 2); poly1.addElement(-7, 0); cout &lt;&lt; "Polynomial A: " &lt;&lt; poly1 &lt;&lt; endl; // The second polynomial: -5x^5 - 14x^3 + 7x^2 + 14 poly2.addElement(-5, 14); poly2.addElement(-14, 3); poly2.addElement(7, 2); poly2.addElement(14, 0); cout &lt;&lt; "Polynomial B: " &lt;&lt; poly2 &lt;&lt; endl; </code></pre> <p>Also, the &lt;&lt; overloaded operator works fine and displays the linked list fine. The problem is when I try to do this:</p> <pre><code>result = poly1 * poly2; </code></pre> <p>I get a segmentation fault. And I don't know why. As I've said, I put cout statements inside the first while loop and this is what I get when I do poly1 * poly2:</p> <pre><code> -15x^17 - 42x^6 + 21x^5 + 42x^3 - 35x^16 - 98x^5 + 49x^4 + 98x^2 + 35x^14 + 98x^3 - 49x^2 - 98 [1] 39009 segmentation fault ~/Desktop/run </code></pre> <p>Yeah, it's pretty ugly, but this is before I add all these stuff together. But anyways, it's essentially right. I just get a segmentation fault after it evaluates the last constant.</p> <p>I have no idea why it's doing this and it only does this for the multiply operator. Other stuff works fine though. I probably have a bug somewhere and I've looked for it these past few hours but I don't know what I've done wrong. Can someone please help? </p> <p>Thanks. </p> <p>My node class:</p> <pre><code>class Node { private: int power; int coefficient; Node *next; public: Node(); // in implementation: coeff = 0, power = 0, next = NULL; Node(const int coeff, const int powr = 1); void setPowAndCoefficient(const int coeff, const int powr); inline int getPow() const { return power; }; inline int getCoeff() const { return coefficient; }; inline void setNextNode(Node *aNode) { next = aNode; }; inline Node *getNext() const { return next; }; }; Node::Node() { coefficient = 0; power = 1; next = NULL; } Node::Node(const int coeff, const int powr) { coefficient = coeff; power = powr; next = NULL; } void Node::setPowAndCoefficient(const int coeff, const int powr) { coefficient = coeff; power = powr; next = NULL; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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