Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio..h>
- #include<conio.h>
- #include<malloc.h>
- struct node
- {
- int coef;
- int expo;
- struct node *link;
- };
- struct node *poly_add(struct node *,struct node *);
- //struct node *poly_sub(struct node *,struct node *);
- struct node *enter(struct node *);
- struct node *insert(struct node *,float,int);
- void display(struct node *);
- void main()
- {
- struct node *p1_start;
- struct node *p2_start;
- struct node *p3_start;
- struct node *p4_start;
- clrscr();
- p1_start=NULL;
- p2_start=NULL;
- p3_start=NULL;
- //p4_start=NULL;
- printf("Polynomial 1:\n");
- p1_start=enter(p1_start);
- printf("Polynomial 2:\n");
- p2_start=enter(p2_start);
- p3_start=poly_add(p1_start,p2_start);
- //p4_start=poly_sub(p1_start,p2_start);
- printf("Polynomial 1 : ");
- display(p1_start);
- printf("Polynomial 2 : ");
- display(p2_start);
- printf("Added Polynomial : ");
- display(p3_start);
- /*printf("Subtracted Polynomial : ");
- diplay(p4_start);*/
- getch();
- }
- struct node *enter(struct node *start)
- {
- int i,n,ex,co;
- printf("How many Time You Want To Enter\n");
- scanf("%d",&n);
- for(i=1;i<=n;i++)
- {
- printf("Co-efficient of Term %d : ",i);
- scanf("%d",&co);
- printf("Expo of Term %d : ",i);
- scanf("%d",&ex);
- start=insert(start,co,ex);
- }
- return start;
- }
- struct node *insert(struct node *start,float co,int ex)
- {
- struct node *ptr,*t;
- t=(struct node *)malloc(sizeof(struct node));
- t->coef=co;
- t->expo=ex;
- if(start==NULL||ex>start->expo)
- {
- t->link=start;
- start=t;
- }
- else
- {
- ptr=start;
- while(ptr->link!=NULL &&ptr->link->expo>ex)
- {
- ptr=ptr->link;
- }
- t->link=ptr->link;
- ptr->link=t;
- if(ptr->link==NULL)
- {
- t->link=NULL;
- }
- }
- return start;
- }
- struct node *poly_add(struct node *p1,struct node *p2)
- {
- struct node *p3_start,*p3,*t;
- p3_start=NULL;
- if(p1==NULL&&p2!=NULL)
- return p3_start;
- while(p1!=NULL&&p2!=NULL)
- {
- t=malloc(sizeof(struct node));
- if(p3_start==NULL)
- {
- p3_start=t;
- p3=p3_start;
- }
- else
- {
- p3->link=t;
- p3=p3->link;
- }
- if(p1->expo>p2->expo)
- {
- t->coef=p1->coef;
- t->expo=p1->expo;
- p1=p1->link;
- }
- else
- {
- if(p2->expo>p1->expo)
- {
- t->coef=p2->coef;
- t->expo=p2->expo;
- p2=p2->link;
- }
- else
- {
- if(p1->expo==p2->expo)
- {
- t->coef=p1->coef+p2->coef;
- t->expo=p1->expo;
- p1=p1->link;
- p2=p2->link;
- }
- }
- }
- }//end of while
- while(p1!=NULL)
- {
- t=malloc(sizeof(struct node));
- t->coef=p1->coef;
- t->expo=p1->expo;
- if(p3_start==NULL)
- {
- p3_start=t;
- p3=p3_start;
- }
- else
- {
- p3->link=t;
- p3=p3->link;
- }
- p1=p1->link;
- }//end of while
- while(p2!=NULL)
- {
- t=malloc(sizeof(struct node));
- t->coef=p2->coef;
- t->expo=p2->expo;
- if(p3_start==NULL)//poly 1 is empty
- {
- p3_start=t;
- p3=p3_start;
- }
- else
- {
- p3->link=t;
- p3=p3->link;
- }
- p2=p2->link;
- }//end of while
- p3->link=NULL;
- return p3_start;
- }
- void display(struct node *ptr)
- {
- if(ptr==NULL)
- {
- printf("Empty\n");
- return ;
- }
- while(ptr!=NULL)
- {
- printf("(%dx^%d)",ptr->coef,ptr->expo);
- ptr=ptr->link;
- }
- printf("\b\b\n");/*\b\bto erase the last+sign*/
- }
Advertisement
Add Comment
Please, Sign In to add comment