您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

实现两个指数递减多项式的和与积

2025/8/25 17:44:40发布25次查看
有两个指数递减的一元多项式,写一程序先求这两个多项式的和,再求它们的积。
【提示】 用带表头结点的单链表作为多项式的存储表示;要建立两个单链表;多项式相加就是要把一个单链表中的结点插入到另一个单链表中去,要注意插入、删除操作中指针的正确修改。
#include #include using namespace std; /** 数据结构习题1 多项式的相加和相乘 @刘辉 **/ struct node{ int data; int index; node* next; }; node *insertlist(node* head,node* p); //插入链表 node *createlist(); //创建链表 void printlist(node *head); //打印链表 node *addlist(node *p1,node *p2); //实现加法运算 node *createlist() { int index,data; node *p,*head,*q; head = new node; p = head; coutindex; coutdata; while(index!=0) { q = new node; q->index = index; q->data = data; p->next = q; p = q; coutindex; coutdata; } p->next = null; return head; } //多项式相加 node *addlist(node *p1,node *p2) { int add; node *temp,*head,*p3; p1 = p1->next; p2 = p2->next; head = temp = new node; head->next = null; while(p1&&p2) { if(p1->index==p2->index) { add = p2->data + p1->data; if(add!=0) { p3 = new node; p3->index = p2->index; p3->data = add; p3->next = null; } p1 = p1->next; p2 = p2->next; } else if(p1->indexindex) { p3 = new node; p3->data = p2->data; p3->index = p2->index; p3->next = null; p2 = p2->next; } else { p3 = new node; p3->data = p1->data; p3->index = p1->index; p3->next = null; p1 = p1->next; } if(head->next ==null) { head->next = p3; temp = p3; } else { temp->next = p3; temp = p3; } } temp->next = p1?p1:p2; return head; } //多项式相乘 node* mullist(node *p1,node *p2) { node *head,*temp,*s,*r,*q; head = new node; head->next = null; temp = new node; temp->next = null; p1 = p1->next; p2 = p2->next; for(s=p1;s;s=s->next) { for(r=p2;r;r=r->next) { q = new node; temp->next = q; q->data = s->data * r->data; q->index = s->index + r->index; q->next = null; head = addlist(temp,head); } } return head; } //打印多项式 void printlist(node *head) { node *p = null; p = head->next; if(p==null) { cout=0) cout
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product