linked list printing using while loop and combining like terms
        Posted  
        
            by 
                C Z
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by C Z
        
        
        
        Published on 2012-10-09T03:35:04Z
        Indexed on 
            2012/10/09
            3:36 UTC
        
        
        Read the original article
        Hit count: 151
        
c++
i have a problem printing out my linked list. My program asks the user to enter many different coefficients and degrees and makes it a polynomial and using mergesort it sorts it and then prints it, now i want to combine like terms and i have a problem doing so.
thats part of my function that i don't know what is wrong with it:
  Term* p;
  p=poly;
  if (p==0)
    cout<<"---empty list---";
  while(p !=0)
    if (p->coef==(p->next)->coef){
      cout<<(p->deg)+((p->next)->deg)<<"x^"<<(p->coef)<<endl;
      p=p->next;}
    if (p->coef !=(p->next)->coef){
      cout<<p->deg<<"x"<<p->coef<<"+";
      p=p->next;}
  cout<<endl;
}
and thats my struct:
struct Term {
    int deg;
    float coef;
    Term *next;
};
typedef Term* Poly;
© Stack Overflow or respective owner