Advertisement
Shailrshah

Adding and Subtracting Polynomials Using Linked Lists

Nov 7th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct node{
  4.     int exponent;
  5.     int coefficient;
  6.     struct node* next;
  7. };
  8. struct node* create(int e, int c){
  9.     struct node *newNode = (struct node*) malloc(sizeof(struct node));
  10.     newNode->exponent = e; newNode->coefficient = c; newNode->next = NULL;
  11.     return newNode;
  12. }
  13. struct node* insert(struct node *head, struct node *newNode){
  14.     if(!head || newNode->exponent > head->exponent){
  15.         newNode->next = head;
  16.         head = newNode;
  17.     }
  18.     else{
  19.         struct node *help = head, *prev = NULL;
  20.         while(help && newNode->exponent < help->exponent){
  21.             prev = help;
  22.             help = help->next;
  23.         }
  24.         if(help && newNode->exponent == help->exponent) help->coefficient+=newNode->coefficient;
  25.         else{
  26.             newNode->next = help;
  27.             prev->next = newNode;
  28.         }
  29.     }
  30.     return head;
  31. }
  32. void display(struct node* head){
  33.     struct node *help = head;
  34.     while(help){
  35.         printf("%d", help->coefficient);
  36.         printf("X^(%d)+", help->exponent);
  37.         help = help->next;
  38.     }
  39.     printf("0");
  40. }
  41. struct node* enter(struct node *head, int n){
  42.     int i, e, c;
  43.     for(i = 0; i < n; i++){
  44.         printf("\n\nEnter the exponent: ");
  45.         scanf("%d", &e);
  46.         printf("Enter the coefficient: ");
  47.         scanf("%d", &c);
  48.         head = insert(head, create(e,c));
  49.     }
  50.     return head;
  51. }
  52. struct node *copy(struct node* headC, struct node* headA){
  53.     struct node *last = NULL;
  54.     while(headA){
  55.         struct node* newNode = create(headA->exponent, headA->coefficient);
  56.         if(!headC) headC = last = newNode;
  57.         else{
  58.             last->next = newNode;
  59.             last = newNode;
  60.         }
  61.         headA = headA->next;
  62.     }  
  63.     return headC;
  64. }
  65. void add(struct node *headA, struct node *headB){
  66.     struct node *headC = NULL;
  67.     headC = copy(headC, headA);
  68.     struct node *helpB = headB;
  69.     while(helpB){
  70.         headC = insert(headC, create(helpB->exponent, helpB->coefficient));
  71.         helpB = helpB->next;
  72.     }
  73.     printf("\n\nThe addition of polinomials A and B is ");
  74.     display(headC);
  75. }
  76. void sub(struct node *headA, struct node *headB){
  77.     struct node *headC = NULL;
  78.     headC = copy(headC, headA);
  79.     struct node *helpB = headB;
  80.     while(helpB){
  81.         headC = insert(headC, create(helpB->exponent, -helpB->coefficient));
  82.         helpB = helpB->next;
  83.     }
  84.     printf("\nThe subtraction of polinomials A and B is ");
  85.     display(headC);
  86. }
  87. int main(){
  88.     int nA, nB;
  89.     struct node *headA = NULL;
  90.     struct node *headB = NULL;
  91.     printf("Enter the number of terms for A: ");
  92.     scanf("%d", &nA);
  93.     headA = enter(headA, nA);
  94.     printf("\nEnter the number of terms for B: ");
  95.     scanf("%d", &nB);
  96.     headB = enter(headB, nB);
  97.     printf("\n\nPolynomial A is :- ");
  98.     display(headA);
  99.     printf("\nPolynomial B is :- ");
  100.     display(headB);
  101.     add(headA, headB);
  102.     sub(headA, headB);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement