Polynomial in Hindi – Data Structure

इस पोस्ट में हम data structure में Polynomial in Hindi के बारें में पढेंगे, तो चलिए start करते हैं:-

Polynomial in Hindi – Data Structure

एक polynomial object जो है वह pairs (<exponent,coefficient>)  का एक homogeneous ordered list होता है. जहाँ प्रत्येक coefficient यूनिक होता है.

दूसरे शब्दों में कहें तो, “polynomial एक गणित का expression होता है जिसमें coefficient और exponent होता है.”

उदाहरण के लिए – 12x+ 34x ; यहाँ 12 और 34 coefficients हैं और 2 और 1 exponent value हैं.

Representation of Polynomial

Polynomial को हम निम्नलिखित प्रकार से represent कर सकते हैं:-

array के द्वारा representation –

कभी-कभी ऐसी situations आती है जब हमें बहुत सारें polynomial expressions को evaluate करना पड़ता है और basic गणितीय कार्यों जैसे कि- जोड़ना, घटाना, गुणा करना आदि करना पड़ता है. इसके लिए हम array का प्रयोग करते हैं.

linked list के द्वारा representation –

polynomial linked list में, coefficients और exponents को list के data node के रूप में define किया जाता है.

c++ example

#include<bits/stdc++.h>
using namespace std;
struct Node{
   int coeff;
   int pow;
   struct Node *next;
};
void create_node(int x, int y, struct Node **temp){
   struct Node *r, *z;
   z = *temp;
   if(z == NULL){
      r =(struct Node*)malloc(sizeof(struct Node));
      r->coeff = x;
      r->pow = y;
      *temp = r;
      r->next = (struct Node*)malloc(sizeof(struct Node));
      r = r->next;
      r->next = NULL;
   } else {
      r->coeff = x;
      r->pow = y;
      r->next = (struct Node*)malloc(sizeof(struct Node));
      r = r->next;
      r->next = NULL;
   }
}
void polyadd(struct Node *p1, struct Node *p2, struct Node *result){
   while(p1->next && p2->next){
      if(p1->pow > p2->pow){
         result->pow = p1->pow;
         result->coeff = p1->coeff;
         p1 = p1->next;
      }
      else if(p1->pow < p2->pow){
         result->pow = p2->pow;
         result->coeff = p2->coeff;
         p2 = p2->next;
      } else {
         result->pow = p1->pow;
         result->coeff = p1->coeff+p2->coeff;
         p1 = p1->next;
         p2 = p2->next;
      }
      result->next = (struct Node *)malloc(sizeof(struct Node));
      result = result->next;
      result->next = NULL;
   }
   while(p1->next || p2->next){
      if(p1->next){
         result->pow = p1->pow;
         result->coeff = p1->coeff;
         p1 = p1->next;
      }
      if(p2->next){
         result->pow = p2->pow;
         result->coeff = p2->coeff;
         p2 = p2->next;
      }
      result->next = (struct Node *)malloc(sizeof(struct Node));
      result = result->next;
      result->next = NULL;
   }
}
void printpoly(struct Node *node){
   while(node->next != NULL){
      printf("%dx^%d", node->coeff, node->pow);
      node = node->next;
      if(node->next != NULL)
         printf(" + ");
   }
}
int main(){
   struct Node *p1 = NULL, *p2 = NULL, *result = NULL;
   create_node(41,7,&p1);
   create_node(12,5,&p1);
   create_node(65,0,&p1);
   create_node(21,5,&p2);
   create_node(15,2,&p2);
   printf("polynomial 1: ");
   printpoly(p1);
   printf("\npolynomial 2: ");
   printpoly(p2);
   result = (struct Node *)malloc(sizeof(struct Node));
   polyadd(p1, p2, result);
   printf("\npolynomial after adding p1 and p2 : ");
   printpoly(result);
   return 0;
}

Leave a Comment