如何实现C语言单链表多项式相加的操作

来源:爱站网时间:2020-06-20编辑:网友分享
单链表通常都使用于多个表单中,而我们使用单链表一般都是以多项式相加进行操作的,那么如何实现C语言单链表多项式相加的操作大家清楚吗?快来爱站技术频道看看吧。

单链表通常都使用于多个表单中,而我们使用单链表一般都是以多项式相加进行操作的,那么如何实现C语言单链表多项式相加的操作大家清楚吗?快来爱站技术频道看看吧。

//多项式的相加和相乘 
#include
#include
#pragma warning(disable:4996)//兼容scanf
typedef struct node {
  int coef;
  int expon;
  struct node* link;
}Polynode,*Polynomial;
Polynomial InsertPolyLinklist(Polynomial in,Polynomial Pread) {
  Pread->link = in;
  Pread = in;
  in->link = NULL;
  return Pread;
}
Polynomial ReadPoly(void) {

  Polynomial Pread = (Polynomial)malloc(sizeof(Polynode));
  Pread->link = NULL;
  Polynomial H = Pread;
  int N;
  scanf("%d ", &N);
  while (N--) {
    Polynomial p = (Polynomial)malloc(sizeof(Polynode));
    scanf("%d %d", &p->coef, &p->expon);
    Pread= InsertPolyLinklist(p,Pread);
  }
  Polynomial F;
  F = H->link;
  free(H);
  return F;
}
void PrintPoly(Polynomial F) {
  while(F != NULL) {
    printf("%d %d ", F->coef, F->expon);
    F = F->link;
  }
  printf("\n");
}
Polynomial Add(Polynomial p1, Polynomial p2) {
  Polynomial t1=p1,t2=p2;
  Polynomial p=(Polynomial)malloc(sizeof(Polynode));
  p->link = NULL;
  Polynomial q = p;
  Polynomial read;
  while (t1&&t2) {
    if (t1->expon == t2->expon) {
      if (t1->coef + t2->coef) {
        t1->coef = t1->coef + t2->coef;
        t1->expon = t1->expon;
        read = t1;
        q->link = read;
        q = read;
        t1 = t1->link;
        t2 = t2->link;   
      }
    }
    else {
      if (t1->expon > t2->expon){
        read = t1;
        q->link = read;
        q = read;
        t1 = t1->link;
      }
      else {
        if (t1->expon expon) {
          read = t2;
          q->link = read;
          q = read;
          t2 = t2->link;
        }
      }
    }
  }    
  if (t1) {
    q->link = t1;
  }
  if (t2) {
    q->link = t2;
  }
  Polynomial F = p->link;
  free(p);
    return F;
}
int main(void) {
  Polynomial p1, p2, pp, ps;
  p1 = ReadPoly();
  PrintPoly(p1);
  p2 = ReadPoly();
  PrintPoly(p2);
  pp = Add(p1, p2);
  PrintPoly(pp);
// ps = Mult(p1, p2);
// PrintPoly(ps);
  return 0;
}

如何实现C语言单链表多项式相加的操作,现在大家都知道了吧,未来的互联网是怎样的,这我们没办法预测,当下只能做好我们能做到的。

上一篇:C语言开发中二叉树的生成方法

下一篇:带你掌握C++数据结构之创建链表

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载