I have this assignment where i am supposed to use a linked list to store polynomials and perform basic mathematical functions on them. I wrote the code to accept and print the polynomial. Eclipse doesnt show any error, the programs runs and accept the coefficients properly, but after that it simply stops working and a dialog box pops up saying "polynomial.exe has stopped working" Here is the code:
#include<iostream>
using namespace std;
class term
{
private:
int coef;
int pow;
term *next;
friend class polynomial;
public:
term(int a,int b)
{
coef=a;
pow=b;
next=NULL;
}
};
class polynomial
{
private:
term *head;
public:
polynomial()
{
head=NULL;
}
void ini()
{
int deg;
cout<<"Enter Degree of Polynomial";
cin>>deg;
head=create(deg);
}
term *create(int deg)
{
int coeff;
cout<<"Enter Coefficient for the term x^"<<deg;
cin>>coeff;
term q=term(coeff,deg);
if(deg==0)
{
q.next=NULL;
}
else
{
q.next=create(deg-1);
}
return &q;
}
void pri()
{
term *temp;
for(temp=head;temp->next!=NULL;temp=temp->next)
{
cout<<temp->coef;
}
}
};
int main()
{
polynomial a=polynomial();
a.ini();
a.pri();
return 0;
}
Can someone tell me why this is happening?