要求编程实现两个一元多项式相加,用链表表示多项式,并把相加的结果用链表输出,但是我的输出结果不太对

运行结果和代码如下:

#ifndef LinkList_H
#define LinkList_H
#include
using namespace std;


template
struct Node
{
    T coef;//系数
    T ind;//指数
    Node*next; 
};

template
class Linklist
{
    public:
        Linklist();
        ~Linklist();
        void create();
        void printlist();
        void add(Linklist r,Linklist t);
    private:
        Node*first;
};
#endif 
```c++
#include"one letter polynomial2.h"

    template
    Linklist::Linklist()
    {
        first=new Node;
        first->next=NULL;
    }
        
    
    template
    void Linklist::create()
    {
        first=new Node;
        first->next=NULL;
        int a,b;
        int n;
        cin>>n;
        for(int i=0;i*s=NULL;
            s=new Node;
            s->coef =a;
            s->ind=b;
            cin>>s->coef>>s->ind;
            s->next=first->next;
            first->next=s;
        }
        

    }
    
    template
    Linklist::~Linklist()
    {
    
    }
    
    template
    void Linklist::printlist()
    {
        Node *p;
        p=first->next;
        if(p!=NULL)
        {
            cout<coef<<" "<ind<next;
        };
    }
    
    template
    void Linklist::add(Linklist a,Linklist b)
    {
        Node *p,*q;
        Node *s;
        Node *r;
        Linklist c;//新建链表c 
        r=c.first;//r指向c的头结点
        p=a.first->next;//p指向a的第一个结点
        q=b.first->next;//q指向b的第一个结点
         while(q&&p)
         {
             if(p->ind >q->ind )
             {
                 s=new Node;
                 s->coef =p->coef ;
                 s->ind =p->ind ;
                 r->next=s;
                 r=s;
                 p=p->next;
             }
             else if(p->ind ==q->ind )
             {
                 if(p->coef+q->coef==0 )
                 {
                     p=p->next ;
                     q=q->next ;
                 }
                 else
                 {
                     s=new Node;
                     s->ind =p->ind ;
                     s->coef =p->coef +q->coef ;
                     r->next=s;
                     r=s;
                     p=p->next ;
                     q=q->next ;
                 }
             }
             else
             {
                 s=new Node;
                 s->coef =q->coef ;
                 s->ind =q->ind ;
                 r->next=s;
                 r=s;
                 q=q->next;
             }
         }
         r->next=NULL;
         if(p!=NULL) r->next =p;
         if(q!=NULL) r->next =q;
         c.printlist(); 
    
    }
    
```c++
#include 
#include"one letter polynomial.cpp"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    Linklist <int>a,b;
    a.create();
    b.create();
    a.add(a,b);
    return 0;
}


运行结果如下所示:

img

img

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^