Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Poly wants a Linked list Purpose: linked lists, pointers, dynamic memory, operat

ID: 3907187 • Letter: P

Question

Poly wants a Linked list Purpose: linked lists, pointers, dynamic memory, operator overloading, exceptions Due: June 25 Description This program deals with polynomials. Recall a polynomial is: It is often the case that polynomial is missing many terms so it makes sense to store only the non zero entries in a linked list. For example: 3r1007 would only roquire 2 terms to Ibe stored in adlist iastead of 1001. You are to write a polymomial elass to store polynomials and to perform coanputations on them. define POLY.H struct TermNode int exp / exposent s int coef: I coefficient Term Node next class PolyType publie PelyType O: PolyType (int coef, int exp): // default constructor constructor makes a1 nede polynomial Is PolyType(eonst Poly Type & II copy constructor PolyType Oi I destruetor Poly Type operator-(const Poly Type &): /7 assigement Poly Type operator+ (const Poly Type & ceast : // retuTDS SUm of the parameter+self Poly Type operator. (const Poly Type&) censt: multiplication with self PolyType operator-(const Poly Type & coast;// subtraction friend ostream & operator

Explanation / Answer

here is your files : ------------->>>>>>>>>

poly.h : ---------->>>>>>

#ifndef POLY_H
#define POLY_H
#include<iostream>

using namespace std;

struct TermNode{
int exp;
int coef;
TermNode *next;
};

class PolyType{
public:
  PolyType();
  PolyType(int coef,int exp);
  PolyType(const PolyType &);
  ~PolyType();
  PolyType& operator=(const PolyType &);
  PolyType operator+(const PolyType &);
  PolyType operator-(const PolyType &);
  PolyType operator*(const PolyType &);
  friend ostream& operator<<(ostream &ost,const PolyType &pol){
   TermNode *cur = pol.polyPtr;
   int st = 0;
   while(cur != NULL){
    if(cur->coef < 0){
     ost<<" ";
    }else if(st != 0){
     cout<<" + ";
    }
    ost<<cur->coef<<"x^"<<cur->exp;
    st = 1;
    cur = cur->next;
   }
   
   return ost;
  }
private:
  void clear();
  TermNode *polyPtr;
};

#endif

poly.cpp : ------>>>>>>

#include "poly.h"

PolyType::PolyType(){
polyPtr = NULL;
}

PolyType::PolyType(int coef,int exp){
polyPtr = new TermNode;
polyPtr->coef = coef;
polyPtr->exp = exp;
polyPtr->next = NULL;
}

PolyType::~PolyType(){
clear();
}

PolyType::PolyType(const PolyType &pol){
*this = pol;
}

void PolyType::clear(){
if(polyPtr != NULL){
  TermNode *cur = polyPtr;
  TermNode *prev = NULL;
  while(cur != NULL){
   prev = cur;
   cur = cur->next;
   delete prev;
  }
  polyPtr = NULL;
}
}

PolyType& PolyType::operator=(const PolyType &pol){
if(this == &pol){
  return *this;
}
if(polyPtr != NULL){
  clear();
}
TermNode *cur = pol.polyPtr;
if(cur == NULL){
  return *this;
}
polyPtr = new TermNode;
polyPtr->coef = cur->coef;
polyPtr->exp = cur->exp;
polyPtr->next = NULL;
TermNode *cur1 = polyPtr;
cur = cur->next;
while(cur != NULL){
  cur1->next = new TermNode;
  cur1 = cur1->next;
  cur1->coef = cur->coef;
  cur1->exp = cur->exp;
  cur1->next = NULL;
  cur = cur->next;
}

return *this;
}

PolyType PolyType::operator+(const PolyType &pol){
if(polyPtr == NULL){
  PolyType p = pol;
  return p;
}
PolyType p;
p = *this;
TermNode *cur = p.polyPtr;
TermNode *cur1 = pol.polyPtr;
int st = 0;
while(cur1 != NULL){
  cur = p.polyPtr;
  st = 0;
  while(cur != NULL){
   if(cur1->exp == cur->exp){
    cur->coef = cur->coef + cur1->coef;
    st = 1;
   }
   cur = cur->next;
  }
  
  if(st == 0){
   cur = p.polyPtr;
   while(cur->next != NULL){
    cur = cur->next;
   }
   cur->next = new TermNode;
   cur->next->coef = cur1->coef;
   cur->next->exp = cur1->exp;
   cur->next->next = NULL;
  }
  cur1= cur1->next;
}

return p;
}

PolyType PolyType::operator-(const PolyType &pol){
if(polyPtr == NULL){
  PolyType p = pol;
  TermNode *cur = p.polyPtr;
  while(cur != NULL){
   cur->coef = -cur->coef;
   cur = cur->next;
  }
  return p;
}
PolyType p;
p = *this;
TermNode *cur = p.polyPtr;
TermNode *cur1 = pol.polyPtr;
int st = 0;
while(cur1 != NULL){
  cur = p.polyPtr;
  st = 0;
  while(cur != NULL){
   if(cur1->exp == cur->exp){
    cur->coef = cur->coef - cur1->coef;
    st = 1;
   }
   cur = cur->next;
  }
  
  if(st == 0){
   cur = p.polyPtr;
   while(cur->next != NULL){
    cur = cur->next;
   }
   cur->next = new TermNode;
   cur->next->coef = cur1->coef;
   cur->next->coef = -cur->next->coef;
   cur->next->exp = cur1->exp;
   cur->next->next = NULL;
  }
  cur1= cur1->next;
}

return p;
}

PolyType PolyType::operator*(const PolyType &pol){
if(polyPtr == NULL){
  PolyType p = pol;
  return p;
}
PolyType p;
p = *this;
TermNode *cur = p.polyPtr;
TermNode *cur1 = pol.polyPtr;
while(cur1 != NULL){
  cur = p.polyPtr;
  while(cur != NULL){
   cur->coef = cur->coef * cur1->coef;
   cur->exp = cur->exp + cur1->exp;
   cur = cur->next;
  }
  cur1= cur1->next;
}

return p;
}

polyTest.cpp : ------->>>>>>

#include "poly.cpp"

int main(){
PolyType p1;
PolyType p2(2,3);
PolyType p3(4,5);
cout<<"p2 = "<<p2<<endl;
cout<<"p3 = "<<p3<<endl;
p1 = p2 * p3;
cout<<"p2 * p3 = "<<(p1)<<endl;
cout<<"p2 + p3 = "<<(p2+p3)<<endl;
}