Sunday, December 6, 2020

10

 CMSACOR05P: Data Structures Lab


10. WAP to scan a polynomial using linked list and add two polynomial.


PROGRAME :  WAP to scan a polynomial using linked list and add two polynomial…

 

 

PROGRAME CODE

#include<stdio.h>

#include<iostream>

using namespace std;

class node{

                public:

                                int degree;

                                int data;

                                node* link1;

                                node(){

                                                degree=0;

                                                data=0;

                                                link1=NULL;

                                }

};

class poly{

                public:

                                node* head;

                                poly(){

                                                head=NULL;

                                }

                                int check(int k){

                                                node* ptr=head;

                                                while(ptr!=NULL){

                                                                if(ptr->degree==k)

                                                                                return 1;

                                                                ptr=ptr->link1;

                                                }

                                                return 0;

                                }

                                void append(node* n){

                                                if(head==NULL)

                                                                head=n;

                                                else{

                                                                if(check(n->degree))

                                                                cout<<"THE DGREE IS ALREADY EXIST TRY WITH ANOTHER ....."<<endl;

                                                                else{

                                                                                node* ptr=head;

                                                                                while(ptr!=NULL){

                                                                                                if(ptr->link1==NULL){

                                                                                                                ptr->link1=n;

                                                                                                                break;

                                                                                                }

                                                                                                ptr=ptr->link1;

                                                                                }

                                                                }

                                                }

                                }

                                poly operator +(poly ob1){

                                                node* ptr=head;

                                                node* ptr1=head;

                                                node* ptr3=NULL;

                                                node* ptr4=head;

                                                node* ptr5=head;

                                                node* ptr6=head;

                                                cout<<"ADDITION SUCCESSFULL "<<endl;

                                                while(ptr1->link1!=NULL)

                                                                ptr1=ptr1->link1;

                                                ptr1->link1=ob1.head;

                                                for(ptr=head;ptr!=NULL;ptr=ptr->link1){

                                                                ptr3=ptr->link1;

                                                                while(ptr3!=NULL){

                                                                                if(ptr3->degree==ptr->degree){

                                                                                                ptr->data=ptr->data+ptr3->data;

                                                                                }

                                                                                ptr3=ptr3->link1;

                                                                }

                                                }

                                               

                                                while(ptr4!=NULL){

                                                                ptr6=ptr4;

                                                                ptr5=ptr4->link1;

                                                                while(ptr5!=NULL){

                                                                                if(ptr4->degree==ptr5->degree)

                                                                                                ptr6->link1=ptr5->link1;

                                                                                ptr6=ptr5;

                                                                                ptr5=ptr5->link1;

                                                                }

                                                                ptr4=ptr4->link1;

                                                } 

                                }  

                                void display(){

                                                if(head==NULL)

                                                                cout<<".........EMPTY............"<<endl;

                                                else{

                                                                node* ptr=head;

                                                                while(ptr!=NULL){

                                                                                cout<<"("<<ptr->data<<" x^"<<ptr->degree<<")";

                                                                                if(ptr->link1!=NULL)

                                                                                                cout<<" + ";

                                                                                ptr=ptr->link1;

                                                                }

                                                                cout<<"= 0 ";

                                                }

                                }

};

int main(){

                int opt=0;

                poly ob;

                poly ob1;

                do{

                                cout<<"\nPRESS 0 TO EXIT...."<<endl;

                                cout<<"PRESS 1 TO APPEND NODE FOR 1st POLYNOMIAL..........."<<endl;

                                cout<<"PRESS 2 TO APPEND NODE FOR 2nd POLYNOMIAL..........."<<endl;

                                cout<<"PRESS 3 TO DISPLAY 1st POLYNOMIAL......"<<endl;

                                cout<<"PRESS 4 TO DISPLAY 2nd POLYNOMIAL......"<<endl;

                                cout<<"PRESS 5 TO ADD TWO POLYNOMIALS........"<<endl;

                                cout<<"PRESS 6 TO DISPLAY THE RESULT........"<<endl;  

                                cin>>opt;

                                node *n=new node();

                                switch(opt){

                                                case 0:

                                                                break;

                                                case 1:

                                                                cout<<"ENTER THE DEGREE....."<<endl;

                                                                cin>>n->degree;

                                                                cout<<"ENTER THE DATA......"<<endl;

                                                                cin>>n->data;

                                                                ob.append(n);

                                                                break;

                                                case 2:

                                                                cout<<"ENTER THE DEGREE....."<<endl;

                                                                cin>>n->degree;

                                                                cout<<"ENTER THE DATA......"<<endl;

                                                                cin>>n->data;

                                                                ob1.append(n);

                                                                break;

                                                case 3:

                                                                ob.display();

                                                                break;

                                                case 4:

                                                                ob1.display();

                                                                break;

                                                case 5:

                                                                ob+ob1;

                                                                break;

                                                case 6:

                                                                ob.display();

                                                                break;                                                  

                                                default:

                                                                cout<<".........ERROR........"<<endl;

                                                                break;

                                }

                }while(opt!=0);

                return 0;

}


No comments:

Post a Comment