Advertisement
rp21

Addition of polynomial using Linked List

Sep 5th, 2015
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. //Program to add two polynomials using singly linked list.
  2.  #include<stdio.h>
  3.  #include<conio.h>
  4.  #include<stdlib.h>
  5.  struct node
  6.  {
  7.     int coeff,exp;
  8.     struct node *link;
  9.  };
  10.  void display(struct node *t)
  11.  {
  12.     printf("\n The resultant polynomial is : ");
  13.     while(t->link!=NULL)
  14.     {
  15.         printf("%dx^%d+",t->coeff,t->exp);
  16.         t=t->link;
  17.     }
  18.     printf("%dx^%d",t->coeff,t->exp);
  19.  }
  20.  void add(struct node *p,struct node *q)
  21.  {
  22.     struct node *temp,*s;
  23.     s=NULL;
  24.     while(p!=NULL && q!=NULL)
  25.     {
  26.         struct node *r=(struct node*)malloc(sizeof(struct node));
  27.         if(p->exp==q->exp)
  28.         {
  29.             r->coeff=p->coeff+q->coeff;
  30.             r->exp=p->exp;
  31.             r->link=NULL;
  32.             p=p->link;
  33.             q=q->link;
  34.         }
  35.         else if(p->exp>q->exp)
  36.         {
  37.             r->exp=p->exp;
  38.             r->coeff=p->coeff;
  39.             r->link=NULL;
  40.             p=p->link;
  41.         }
  42.         else if(p->exp<q->exp)
  43.         {
  44.             r->exp=q->exp;
  45.             r->coeff=q->coeff;
  46.             r->link=NULL;
  47.             q=q->link;
  48.         }
  49.         if(s==NULL)
  50.             s=r;
  51.         else
  52.         {
  53.             temp=s;
  54.             while(temp->link!=NULL)
  55.                 temp=temp->link;
  56.             temp->link=r;
  57.         }
  58.     }
  59.     if(p==NULL && q==NULL)
  60.     {
  61.         goto x;
  62.     }
  63.     else if(p!=NULL)
  64.     {
  65.         temp=s;
  66.         while(temp->link!=NULL)
  67.             temp=temp->link;
  68.         temp->link=p;
  69.     }
  70.     else if(q!=NULL)
  71.     {
  72.         temp=s;
  73.         while(temp->link!=NULL)
  74.             temp=temp->link;
  75.         temp->link=q;
  76.     }
  77.     x:
  78.         display(s);
  79.  }
  80.  void main()
  81.  {
  82.     int c,e,ch;
  83.     struct node *p,*last,*q,*temp1;
  84.     clrscr();
  85.     //Creation of first linked list.
  86.     p=NULL;  last=NULL;
  87.     printf("\n For first linked list.");
  88.     do
  89.     {
  90.         printf("\n Enter coeff and exp : ");
  91.         scanf("%d %d",&c,&e);
  92.         temp1=(struct node*)malloc(sizeof(struct node));
  93.         temp1->coeff=c;
  94.         temp1->exp=e;
  95.         temp1->link=NULL;
  96.         if(last==NULL)
  97.         {
  98.             p=temp1;
  99.             last=p;
  100.         }
  101.         else
  102.         {
  103.             last->link=temp1;
  104.             last=temp1;
  105.         }
  106.         printf("\n Do u want to add some more nodes (1 or 0): ");
  107.         scanf("%d",&ch);
  108.     }while(ch==1);
  109.     //Creation of second linked list.
  110.     q=NULL;  last=NULL;
  111.     printf("\n For second linked list.");
  112.     do
  113.     {
  114.         printf("\n Enter coeff and exp : ");
  115.         scanf("%d %d",&c,&e);
  116.         temp1=(struct node*)malloc(sizeof(struct node));
  117.         temp1->coeff=c;
  118.         temp1->exp=e;
  119.         temp1->link=NULL;
  120.         if(last==NULL)
  121.         {
  122.             q=temp1;
  123.             last=q;
  124.         }
  125.         else
  126.         {
  127.             last->link=temp1;
  128.             last=temp1;
  129.         }
  130.         printf("\n Do u want to add some more nodes (1 or 0): ");
  131.         scanf("%d",&ch);
  132.     }while(ch==1);
  133.     add(p,q);
  134.     getch();
  135.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement