using namespace std;
struct node
{
int coeff;
int power;
struct node *next;
};
class adt
{
struct node *start,*start1,*start3;
public:
adt()
{
start=NULL;
start1=NULL;
start3=NULL;
}
void insert(int,int);
void display();
void add_poly();
void insert1(int,int);
};
void adt::add_poly()
{
struct node *temp1,*temp2,*p;
temp1=start;
temp2=start1;
while(temp1!=NULL&&temp2!=NULL)
{
p=(struct node*)malloc(sizeof(struct node));
if(temp1->power>temp2->power)
{
p->coeff=temp1->coeff;
p->power=temp1->power;
temp1=temp1->next;
}
else if(temp1->power<temp2->power)
{
p->coeff=temp2->coeff;
p->power=temp2->power;
temp2=temp2->next;
}
else
{
p->coeff=temp1->coeff+temp2->coeff;
p->power=temp1->power;
temp1=temp1->next;
temp2=temp2->next;
}
start3=p;
p->next=start3;
}
while(temp1!=NULL)
{
p=(struct node*)malloc(sizeof(struct node));
p->coeff=temp1->coeff;
p->power=temp1->power;
p->next=start3;
start3=p;
temp1=temp1->next;
}
while(temp2!=NULL)
{
p=(struct node*)malloc(sizeof(struct node));
p->coeff=temp2->coeff;
p->power=temp2->power;
p->next=start3;
start3=p;
temp2=temp2->next;
}
}
void adt::display()
{
cout<<"\nFIRST LIST:\n";
struct node *temp;
temp=start;
cout<<endl;
while(temp!=NULL)
{
cout<<temp->coeff<<"|"<<temp->power<<"->";
temp=temp->next;
}
cout<<"\nSECOND LIST:\n";
temp=start1;
while(temp!=NULL)
{
cout<<temp->coeff<<"|"<<temp->power<<"->";
temp=temp->next;
}
cout<<"\nRESULTANT LIST:\n";
temp=start3;
while(temp!=NULL)
{
cout<<temp->coeff<<"|"<<temp->power<<"->";
temp=temp->next;
}
}
void adt::insert(int cfcnt,int pwr)
{
cout<<"inside insert\n";
struct node *temp,*p;
p=(struct node*)malloc(sizeof(struct node));
p->coeff=cfcnt;
p->power=pwr;
p->next=NULL;
if(start==NULL)
{
start=p;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
}
}
void adt::insert1(int cfcnt1,int pwr1)
{
cout<<"inside insert\n";
struct node *temp,*p;
p=(struct node*)malloc(sizeof(struct node));
p->coeff=cfcnt1;
p->power=pwr1;
p->next=NULL;
if(start1==NULL)
{
start1=p;
}
else
{
temp=start1;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
}
}
int main()
{
adt obj;
int choice=0,item,pos;
while(choice!=3)
{
cout<<"Enter your choice\nEnter 1 for display the list\nEnter 2 for inserting an item in the first list\nEnter 3 for exit\nEnter 4 for inserting an item in the second list\nEnter 5 for adding polynomials\n";
cin>>choice;
switch(choice)
{
case 1:
obj.display();
break;
case 2:
int coeff,power;
cout<<"enter the Coefficient\n";
cin>>coeff;
cout<<"enter the Power\n";
cin>>power;
obj.insert(coeff,power);
break;
case 4:
int coeff1,power1;
cout<<"enter the Coefficient\n";
cin>>coeff1;
cout<<"enter the Power\n";
cin>>power1;
obj.insert1(coeff1,power1);
break;
case 5:
obj.add_poly();
break;
}
}
return 0;
}
Comments