C LAB‎ > ‎(Sem2) Data Structures‎ > ‎

posted Mar 19, 2011, 2:13 AM by Neil Mathew   [ updated Mar 19, 2011, 2:21 AM ]

SOURCE CODE:

 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 ``` ```#include   struct node {        int coeff; //coefficient        int exp;  //exponent        struct node * next;   }*ptr;   void INS(int exp,int coeff,struct node* start) {        if( coeff!=0 ) // If coeff is zero, node need not be created.       {                   struct node* newnode;         newnode=(struct node*) malloc( sizeof(struct node) );                 newnode->coeff=coeff;         newnode->exp=exp;         newnode->next=NULL;                 ptr=start;         while(ptr->next!=NULL) //points ptr to the last node         ptr=ptr->next;                 ptr->next = newnode;               } }   void INPUT(struct node* start) {      int exp,coeff;             printf("\n Enter the highest exponent: ");       scanf("%d",&exp);       printf("\n");             while(exp>=0)       {              printf(" Enter the Coefficient of X^%d : ",exp);        scanf("%d",&coeff);                INS(exp,coeff,start);        exp--;       };   }   void ADD(struct node*start1,struct node*start2,struct node*start3) {      struct node *ptr1;      struct node *ptr2;            ptr1=start1->next;      ptr2=start2->next;               // I based it on the same principle followed in Merge Sort              while(ptr1!=NULL && ptr2!=NULL)      {             if( ptr1->exp > ptr2->exp )       {           INS(ptr1->exp, ptr1->coeff, start3);           ptr1=ptr1->next;       }       else if( ptr2->exp > ptr1->exp )       {           INS(ptr2->exp, ptr2->coeff, start3);           ptr2=ptr2->next;               }       else // IF ptr1->exp = ptr2->exp       {           INS( ptr1->exp, (ptr1->coeff + ptr2->coeff), start3);             ptr1=ptr1->next;           ptr2=ptr2->next;       }      }            while( ptr1!=NULL )      {           INS(ptr1->exp, ptr1->coeff, start3);           ptr1=ptr1->next;      }            while( ptr2!=NULL )      {           INS(ptr2->exp, ptr2->coeff, start3);           ptr2=ptr2->next;      } }   void DISPLAY(struct node *start) {      if(start->next==NULL)      printf(" No Nodes. ");      else      {          ptr=start;          ptr=ptr->next; //Header Node's values not considered.                           do         {                               if(ptr->exp==0) //formatting for constant           printf(" %d",ptr->coeff);           else           printf(" %dX^%d",ptr->coeff, ptr->exp);                     ptr=ptr->next;                           if(ptr!=NULL)           printf(" +");                 }         while(ptr!=NULL);              }     }   main() {             struct node * start1;       struct node * start2;       struct node * start3;             //Declaring the Header Node of First Polynomial.       struct node* newnode1;       newnode1= (struct node *) malloc( sizeof(struct node) );             newnode1->coeff=0;       newnode1->exp=-1;       newnode1->next=NULL;             if(newnode1==NULL)       {       printf(" No Memory available. ");       exit(0);       }             start1=newnode1;             //Declaring the Header Node of Second Polynomial.       struct node* newnode2;       newnode2= (struct node *) malloc( sizeof(struct node) );             newnode2->coeff=0;       newnode2->exp=-1;       newnode2->next=NULL;             if(newnode2==NULL)       {       printf(" No Memory available. ");       exit(0);       }             start2=newnode2;             //Declaring the Header Node of Resultant Polynomial.       struct node* newnode3;       newnode3= (struct node *) malloc( sizeof(struct node) );             newnode3->coeff=0;       newnode3->exp=-1;       newnode3->next=NULL;             if(newnode3==NULL)       {       printf(" No Memory available. ");       exit(0);       }             start3=newnode3;             //Others       printf("\n The polynomial in the format :");       printf(" 5X^3 + 2X^2 + 7X^1 + 13 ");             printf("\n\n First Polynomial: ");       INPUT(start1);             printf("\n Second Polynomial: ");       INPUT(start2);                 printf("\n Polynomial 1: ");       DISPLAY(start1);             printf("\n Polynomial 2: ");       DISPLAY(start2);             ADD(start1, start2, start3);             printf("\n\n After Addition: ");       printf("\n Resultant Polynomial: ");       DISPLAY(start3);             //getch();       }        ```

OUTPUT:

```
The polynomial in the format : 5X^3 + 2X^2 + 7X^1 + 13

First Polynomial:
Enter the highest exponent: 3

Enter the Coefficient of X^3 : 12
Enter the Coefficient of X^2 : 15
Enter the Coefficient of X^1 : 0
Enter the Coefficient of X^0 : 20

Second Polynomial:
Enter the highest exponent: 5

Enter the Coefficient of X^5 : 20
Enter the Coefficient of X^4 : 20
Enter the Coefficient of X^3 : 8
Enter the Coefficient of X^2 : 5
Enter the Coefficient of X^1 : 20
Enter the Coefficient of X^0 : 0

Polynomial 1:  12X^3 + 15X^2 + 20
Polynomial 2:  20X^5 + 20X^4 + 8X^3 + 5X^2 + 20X^1