Hello,

i created a program for polynomial addition which adds 2 polynomials by way of link list. But when i create 2 linklist and add them everything goes right and then when i press 4 for exit i keep getting "NULL pointer assignment" i have deleted all the memory that i created with new but still why??Please help me. Here is the source code

Code:#include <iostream.h> #include <stdio.h> class poly { struct node { float co; int power; struct node *next; }*first; public: poly() { first=NULL; } void getdata(); void putdata(); void add(poly ); void del(); }; void poly::del() { struct node *q; while(first!=NULL) { q=first; first=first->next; delete q; } } void poly::add(poly b) { struct node *q=first,*t=b.first,*ans=NULL,*m,*newnode; while(q!=NULL && t!=NULL) { newnode=new node; newnode->next=NULL; if(q->power>t->power) { newnode->co=q->co; newnode->power=q->power; q=q->next; } else if(q->power<t->power) { newnode->co=t->co; newnode->power=t->power; t=t->next; } else { newnode->co=t->co+q->co; newnode->power=t->power; t=t->next; q=q->next; } if(ans==NULL) ans=newnode; else m->next=newnode; m=newnode; } while(q!=NULL) { newnode=new node; newnode->co=q->co; newnode->power=q->power; newnode->next=NULL; q=q->next; } while(t!=NULL) { newnode=new node; newnode->co=t->co; newnode->power=t->power; newnode->next=NULL; t=t->next; } q=ans; cout<<endl; while(q!=NULL) { cout<<q->co<<" X"<<q->power<<" +"; q=q->next; } cout<<"\b"; cout<<NULL; while(ans!=NULL) { q=ans; ans=ans->next; delete q; } } void poly::getdata() { struct node *q=first,*newnode; float tco; int tpower; while(first!=NULL) { first=first->next; delete q; } cout<<"\n\nEnter Co-ordinate (Enter -1 To Exit) :"; cin>>tco; while(tco!=-1) { cout<<"\n\nEnter Power :"; cin>>tpower; newnode=new node; newnode->co=tco; newnode->power=tpower; newnode->next=NULL; if(first==NULL || newnode->power>first->power) { newnode->next=first; first=newnode; } else { q=first; while(q!=NULL) { if(q->power>=newnode->power && (q->next->power<newnode->power || q->next==NULL)) { newnode->next=q->next; q->next=newnode; break; } q=q->next; } } cout<<"\n\nEnter Co-ordinate (Enter -1 To Exit) :"; cin>>tco; } } void poly::putdata() { struct node *q=first; while(q!=NULL) { cout<<q->co<<"X"<<q->power<<" +"; q=q->next; } cout<<"\b"; putch(NULL); } int main(void) { poly a,b; int ch; do { cout<<"\n\nAddition Of 2 Polynomials"; cout<<"\n\n1 :Enter 1st Polynomial"; cout<<"\n\n2 :Enter 2nd Polynomial"; cout<<"\n\n3 :Addition Of 2 Polynomials"; cout<<"\n\n4 :Exit"; cout<<"\n\nEnter Your Choice :"; cin>>ch; switch(ch) { case 1: a.getdata(); a.putdata(); break; case 2: b.getdata(); b.putdata(); break; case 3: a.add(b); break; case 4: cout<<"\n\nBye-Bye"; break; default: cout<<"\n\nWrong Choice"; } }while(ch!=4); a.del(); b.del(); return 0; }