A. c语言写一个程序,用链表实现多项式相加相减
//要求:多项式按降幂排列
#include<stdafx.h>
#include<iostream.h>
structnode
{
intcoef;//系数
intexp;//指数
node*next;
};
node*h1=NULL;//第一个多项式的头指针
node*h2=NULL;//第二个多项式的头指针
node*insert(intc,inte)//创建一个系数为c,指数为e的结点,并返回其地址
{
node*newnode=newnode;//创建新结点
newnode->coef=c;//系数等于c
newnode->exp=e;//指数等于e
newnode->next=NULL;
returnnewnode;
}
node*create(node*h)
{
intcoef;
intexp;
charc;
cout<<"请输入系数:";
cin>>coef;
cout<<"请输入指数:";
cin>>exp;
h=insert(coef,exp);
cout<<"是否输入完毕?(Y/N)";
cin>>c;
if(c=='y'||c=='Y')
{
returnh;//如果输入完毕,返回头指针地址
}
else
{
h->next=create(h->next);//否则递归建立下一个结点
returnh;//返回头指针地址
}
}
node*(node*source)//将一个结点的内容复制到另一个结点,并返回该结点地址
{
if(source!=NULL)//如果源结点不为空
{
node*des=newnode;//创建新结点
des->coef=source->coef;//新结点的系数等于源结点的系数
des->exp=source->exp;//新结点的指数等于源结点的指数
des->next=NULL;
returndes;//返回新结点地址
}
returnNULL;
}
voidprint(node*head)//输出头指针为head的多项式链表
{
node*h=head;
if(h==NULL)
{
cout<<"0";//如果链表为空,输出0
}
else
{
while(h->next!=NULL)//否则,当其下一个结点不为空时
{
if(h->exp==0)//如果指数为0,即常数
{
if(h->coef>0)//如果系数大于0
{
cout<<h->coef;//输出系数
}
else
{
cout<<""<<h->coef;//否则,退一格(消除前面的+号),输出系数
}
}
elseif(h->exp==1)//否则,如果指数为1
{
if(h->coef>0)//如果系数大于0
{
if(h->coef==1)//如果系数等于1
{
cout<<"x"<<"+";//输出x+
}
else
{
cout<<h->coef<<"*x"<<"+";//否则,输出相应的系数
}
}
else
{
if(h->coef==-1)//否则,如果系数等于-1
{
cout<<""<<"-x"<<"+";//退一格,输出-x
}
else
{
cout<<""<<h->coef<<"*x"<<"+";//否则,退一格,输出相应的系数
}
}
}
else//否则,指数大于1
{
if(h->coef>0)//如果系数大于0
{
if(h->coef==1)//如果系数等于1
{
cout<<"x"<<h->exp<<"+";
}
else
{
cout<<h->coef<<"*x"<<h->exp<<"+";
}
}
else
{
if(h->coef==-1)//否则,如果系数等于-1
{
cout<<""<<"-x"<<h->exp<<"+";
}
else
{
cout<<""<<h->coef<<"*x"<<h->exp<<"+";
}
}
}
h=h->next;
}
//输出最后一项
if(h->exp==0)//如果指数为0,即常数
{
if(h->coef>0)//如果系数大于0
{
cout<<h->coef;//输出系数
}
else
{
cout<<""<<h->coef;//否则,退一格,输出系数
}
}
elseif(h->exp==1)//否则,如果指数为1
{
if(h->coef>0)//如果系数大于0
{
if(h->coef==1)//如果系数等于1
{
cout<<"x";//输出x
}
else
{
cout<<h->coef<<"*x";
}
}
else
{
if(h->coef==-1)//否则,如果系数等于-1
{
cout<<""<<"-x";//退一格,输出-x
}
else
{
cout<<""<<h->coef<<"*x";
}
}
}
else//否则,指数大于1
{
if(h->coef>0)//如果系数大于0
{
if(h->coef==1)//如果系数等于1
{
cout<<"x"<<h->exp;
}
else
{
cout<<h->coef<<"*x"<<h->exp;
}
}
else
{
if(h->coef==-1)//否则,如果系数等于-1
{
cout<<""<<"-x"<<h->exp;
}
else
{
cout<<""<<h->coef<<"*x"<<h->exp;
}
}
}
}
}
voidprints(node*p1,node*p2,node*r)//输出相加结果,形如p1+p2=r
{
print(p1);
cout<<endl<<"+"<<endl;
print(p2);
cout<<endl<<"="<<endl;
print(r);
cout<<endl;
}
charcompare(node*n1,node*n2)//比较两个结点的指数大小
{
if(n1->exp==n2->exp)
{
return'=';
}
elseif(n1->exp>n2->exp)
{
return'>';
}
else
{
return'<';
}
}
node*add(node*p1,node*p2)//计算两个多项式相加,返回结果链表首地址
{
node*fr;
//如果有一个为空,就把另外一个链表其后的部分复制到结果链表的尾部
if(p1==NULL)
{
node*x=(p2);
node*temp=x;
while(p2!=NULL)
{
x->next=(p2->next);
x=x->next;
p2=p2->next;
}
returntemp;
}
elseif(p2==NULL)
{
node*x=(p1);
node*temp=x;
while(p1!=NULL)
{
x->next=(p1->next);
x=x->next;
p1=p1->next;
}
returntemp;
}
//如果都不为空
else
{
switch(compare(p1,p2))//比较两个结点的指数大小
{
case'='://相等
if(p1->coef+p2->coef!=0)//如果系数和不为0
{
fr=insert(p1->coef+p2->coef,p1->exp);//新结点的系数为两个结点的系数和,指数为这两个结点的指数
fr->next=add(p1->next,p2->next);
returnfr;
}
else
{
fr=add(p1->next,p2->next);//否则,新结点地址为这两个结点之后的部分链表的和链表的首地址
returnfr;
}
case'>'://大于
fr=(p1);//新结点的内容与p1相同
fr->next=add(p1->next,p2);//以p1->next为新的p1,递归调用add,创建链表余下的部分
returnfr;
case'<'://小于
fr=(p2);//新结点的内容与p2相同
fr->next=add(p1,p2->next);//以p2->next为新的p2,递归调用add,创建链表余下的部分
returnfr;
default:
returnNULL;
}
}
}
voidmain(void)
{
cout<<"先建立第一个多项式:"<<endl;
h1=create(h1);
cout<<"再建立第二个多项式:"<<endl;
h2=create(h2);
cout<<"和:"<<endl;
prints(h1,h2,add(h1,h2));
}
B. 两个多项式相加运算(用c语言)
#include<stdio.h>
#include<malloc.h>
#define Null 0
typedef struct Node
{
int coeff;
int expo;
Node *next;
}listNode,*list;
list createList()
{
list head;
head = (list)malloc(sizeof(listNode));
head = NULL;
printf("want to create a new node?y/n\n");
char ch;
fflush(stdin);
scanf("%c",&ch);
while(ch=='Y' || ch== 'y')
{
list p;
p = (list)malloc(sizeof(listNode));
printf("input coeff\n");
int coeff;
scanf("%d",&coeff);
p->coeff = coeff;
printf("input expo\n");
int expo;
scanf("%d",&expo);
p->expo = expo;
p->next = NULL;
//链表为空的时候,即添加首个元素
if(head == NULL)
{
head=p;//添加代码
}
else
{
list prev,curr;
curr = head;
prev = NULL;
//找到添加位置
while(curr!=NULL && curr->expo>p->expo)
{
prev=curr;
curr=curr->next;//添加代码
}
if(curr!=NULL && curr->expo == p->expo)
{
curr->coeff = curr->coeff + p->coeff;
printf("want to create a new node?y/n\n");
fflush(stdin);
scanf("%c",&ch);
if(ch=='Y' || ch== 'y')
continue;
else
return head;
}
//插入结点,结点非首
if(prev != NULL)
{
p->next=curr;
prev->next=p;
//添加代码
}
//插入结点,结点为首
else
{
p->next=curr;
head=p;
//添加代码
}
}
printf("want to create a new node?y/n\n");
fflush(stdin);
scanf("%c",&ch);
}
return head;
}
list add(list head1,list head2)
{
list head,newNode,ptr1,ptr2,ptr3;
ptr1 = head1;
ptr2 = head2;
head = NULL;
while(ptr1 != NULL && ptr2 != NULL)
{
newNode = (list)malloc(sizeof(listNode));
if(ptr1->expo > ptr2->expo)
{
newNode->coeff = ptr1->coeff;
newNode->expo = ptr1->expo;
newNode->next = NULL;
ptr1 = ptr1->next;
}
else if(ptr1->expo < ptr2->expo)
{
newNode->coeff = ptr2->coeff;
newNode->expo = ptr2->expo;
newNode->next = NULL;
ptr2 = ptr2->next;//添加代码
}
else
{
newNode->coeff = ptr1->coeff + ptr2->coeff;
newNode->expo = ptr1->expo;
newNode->next = NULL;
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
if(head==NULL)
{
head = newNode;
}
else
{
ptr3 = head;
//添加代码
while(ptr3->next != NULL)
ptr3 = ptr3->next;
ptr3->next = newNode;
}
}
while(ptr1 != NULL)
{
newNode = (list)malloc(sizeof(listNode));
newNode->coeff = ptr1->coeff;
newNode->expo = ptr1->expo;
newNode->next = NULL;
ptr3 = head;
if(ptr3 == NULL)
head = ptr3 = newNode;
else
{
while(ptr3->next != NULL)
ptr3 = ptr3->next;
ptr3->next = newNode;//添加代码
}
ptr1 = ptr1->next;
}
while(ptr2 != NULL)
{
newNode = (list)malloc(sizeof(listNode));
newNode->coeff = ptr2->coeff;
newNode->expo = ptr2->expo;
ptr3 = head;
if(ptr3 == NULL)
head = ptr3 = newNode;
else
{
while(ptr3->next != NULL)
ptr3 = ptr3->next;
ptr3->next = newNode;
}
ptr2 = ptr2->next;
}
return head;
}
void display(list head)
{
list ptr = head;
while(ptr != NULL)
{
if(ptr != head )
printf("+");
printf("%d",ptr->coeff);
printf("x^");
printf("%d",ptr->expo);
ptr = ptr->next;
}
printf("\n");
}
int main(int argc, char* argv[])
{
list head,head1,head2;
printf("input the first list\n");
head1 = createList();
display(head1);
printf("input the second list\n");
head2 = createList();
display(head2);
head = add(head1,head2);
display(head);
return 0;
}
C. 用C语言链表实现多元多项式及其乘法,加法。
用C语言链表实现多项式, 例如f(x_{i,k}^{l})=3+x_{1,2}^2x_{3,2}+x_{1,3}x_{4,3}^3 (变量x_{i,k}^{l}有3个指标i,k,l, i,k,l可以取遍1到n的整数)。要求多项式由键盘输入,用链表存储单项式(节点是x_{i,k}^{l}),用链表存储多项式(节点是单项式)。编写3个函数分别是实现多项式加法,乘法的函数,以及输出多项式的函数。
D. 数据结构(C语言)用单链表存储一元多项式,并实现两个多项式的相加运算,怎么做
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef int ElemType;
/*单项链表的声明*/
typedef struct PolynNode{
int coef; // 系数
int expn; // 指数
struct PolynNode *next; }PolynNode,*PolynList;
/*正位序(插在表尾)输入n个元素的值,建立带表头结构的单链线性表*/
/*指数系数一对一对输入*/ void CreatePolyn(PolynList &L,int n)
{
int i;
下载
原文档已转码为如下格式,以便移动设备查看
数据结构(c语言)用单链表存储一元多项式,并实现两个多项式的相加运算【最新】
阅读:1037次 页数:36页 2016-03-21 举报
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef int ElemType;
/*单项链表的声明*/
typedef struct PolynNode{
int coef; // 系数
int expn; // 指数
struct PolynNode *next; }PolynNode,*PolynList;
/*正位序(插在表尾)输入n个元素的值,建立带表头结构的单链线性表*/
/*指数系数一对一对输入*/ void CreatePolyn(PolynList &L,int n)
{
int i;
PolynList p,q;
L=(PolynList)malloc(sizeof(PolynNode)); // 生成头结点
L->next=NULL;
q=L;
printf("成对输入%d个数据 ",n);
for(i=1;i<=n;i++)
{
p=(PolynList)malloc(sizeof(PolynNode));
scanf("%d%d",&p->coef,&p->expn); //指数和系数成对输入
q->next=p;
q=q->next;
}
p->next=NULL;
}
// 初始条件:单链表L已存在
// 操作结果: 依次对L的每个数据元素调用函数vi()。一旦vi()失败,则操作失败
void PolynTraverse(PolynList L,void(*vi)(ElemType, ElemType)) {
PolynList p=L->next;
while(p)
{
vi(p->coef, p->expn);
if(p->next)
{
printf(" + "); //“+”号的输出,最后一项后面没有“+”
}
p=p->next;
}
printf(" ");
}
/*ListTraverse()调用的函数(类型要一致)*/ void visit(ElemType c, ElemType e) {
if(c != 0)
{
printf("%dX^%d",c,e); //格式化输出多项式每一项
}
}
/* 多项式相加,原理:归并 */ /* 参数:两个已经存在的多项式 */ /* 返回值:归并后新的多项式的头结点 */
PolynList MergeList(PolynList La, PolynList Lb) {
PolynList pa, pb, pc, Lc;
pa = La->next;
pb = Lb->next;
Lc = pc = La; // 用La的头结点作为Lc的头结点
while(pa&&pb)
{
if(pa->expn < pb->expn)
{
pc->next = pa; //如果指数不相等,pc指针连上指数小的结
点,
pc = pa;
pa = pa->next; //指向该结点的指针后移
}
else if (pa ->expn > pb->expn )
{
pc->next = pb; //pc指针连上指数小的结点,
pc = pb;
pb = pb->next; //指向该结点的指针后移
}
else //(pa ->expn = pb->expn )
{
pa->coef = pa->coef + pb->coef; //指数相等时,系数相加
pc->next = pa;
pc = pa;
pa = pa->next; //两指针都往后移
pb = pb->next;
}
}
pc->next = pa ? pa:pb; // 插入剩余段
return Lc;
}
void main()
{
PolynList ha,hb,hc;
printf("非递减输入多项式ha, ");
CreatePolyn(ha,5); // 正位序输入n个元素的值
printf("非递减输入多项式hb, ");
CreatePolyn(hb,5); // 正位序输入n个元素的值
E. C语言用链表实现一元多项式的相加
C语言代码:
#include"stdio.h"
#include"malloc.h"
/*链表结点结构*/
typedefstructLNode{
doublecoef;/*系数*/
intexp;/*指数*/
structLNode*next;
}LNode;
/*初始化链表*/
LNode*Init()
{
LNode*head=(LNode*)malloc(sizeof(LNode));
head->next=NULL;
returnhead;
}
/*将值为data的结点插入到head链表的最后*/
voidAddNode(LNode*head,doublecoef,intexp)
{
LNode*pre=head->next;
LNode*temp;
temp=(LNode*)malloc(sizeof(LNode));
temp->coef=coef;
temp->exp=exp;
temp->next=NULL;
if(pre==NULL)
{
head->next=temp;
return;
}
for(;pre->next!=NULL;pre=pre->next);
pre->next=temp;
}
/*输出head链表的所有结点的值*/
voidList(LNode*head)
{
LNode*curr;
printf("Allnodes:");
for(curr=head->next;curr!=NULL;curr=curr->next)
{
printf("(%lf,%d) ",curr->coef,curr->exp);
}
printf(" ");
}
/*返回head链表的所有结点的数量*/
intSize(LNode*head)
{
intlen=0;
LNode*curr;
for(curr=head->next;curr!=NULL;curr=curr->next,len++);
returnlen;
}
LNode*Add(LNode*headA,LNode*headB)
{
LNode*currA,*currB,*headC;
doublesum;
currA=headA->next;
currB=headB->next;
headC=Init();
while(currA!=NULL&&currB!=NULL)
{
if(currA->exp>currB->exp)
{
AddNode(headC,currA->coef,currA->exp);
currA=currA->next;
}
elseif(currA->exp<currB->exp)
{
AddNode(headC,currB->coef,currB->exp);
currB=currB->next;
}
else
{
sum=currA->coef+currB->coef;
if(sum!=0)
{
AddNode(headC,sum,currA->exp);
}
currA=currA->next;
currB=currB->next;
}
}
while(currA!=NULL)
{
AddNode(headC,currA->coef,currA->exp);
currA=currA->next;
}
while(currB!=NULL)
{
AddNode(headC,currB->coef,currB->exp);
currB=currB->next;
}
returnheadC;
}
voidmain()
{
LNode*headA,*headB,*headC;
headA=Init();
headB=Init();
AddNode(headA,1.0,5);
AddNode(headA,-1.0,3);
AddNode(headA,1,0);
AddNode(headB,0.5,5);
AddNode(headB,1.0,4);
AddNode(headB,1.0,3);
List(headA);
List(headB);
headC=Add(headA,headB);
List(headC);
}
运行测试:
F. 多项式相加 链表 C语言
#include#includestructnode{
intcoef; //系数
intindex; //指数
structnode*next;
};
//初始化linklist
structnode*init_ll(){
structnode*p;
p=(structnode*)malloc(sizeof(structnode)); //为节点指针分配内存空间
p->next=NULL; //*为节点的next值赋值为NULL方便表的遍历,作为遍历为结尾的判断条件
returnp; //返回指针,便于操作
}
//头插法插入linklist
voidinsert_ll(structnode*head,intcoef,intindex){
structnode*p;
p=(structnode*)malloc(sizeof(structnode)); //为节点指针分配内存空间
p->coef=coef; //实例化系数
p->index=index; //实例化指数
p->next=head->next; //*该节点的next指针指向head指向的节点
head->next=p; //*插入在头指针的后面
}
//打印输出linklist
voidreverse_ll(structnode*head){
structnode*p;
p=head->next;
while(p!=NULL){ //为空时结束循环
printf("%d次项系数为%d",p->index,p->coef); //打印输出
p=p->next; //指针迭代,进行循环输出
}
printf(" ");
}
//销毁linklist释放空间
voiddestory_ll(structnode*head){
structnode*p,*q;
p=head->next;
while(p!=NULL){
q=p;
p=p->next;
free(q);
}
free(head);
}
//两个多项式进行相加操作
voidadd_poly(structnode*head1,intlength1,structnode*head2,intlength2){
structnode*p1;
structnode*p2;
if(length1>length2){
p1=head1->next;
p2=head2->next;
while(p1!=NULL){
if(p1->index==p2->index){
p1->coef=(p1->coef)+(p2->coef);
p2=p2->next;
}
p1=p1->next;
}
}
else{
p1=head1->next;
p2=head2->next;
while(p2!=NULL){
if(p1->index==p2->index){
p2->coef=(p2->coef)+(p1->coef);
p1=p1->next;
}
p2=p2->next;
}
}
}
//两个多项式进行相减操作
voidsub_poly(structnode*head1,intlength1,structnode*head2,intlength2){
structnode*p1;
structnode*p2;
if(length1>length2){
p1=head1->next;
p2=head2->next;
while(p1!=NULL){
if(p1->index==p2->index){
p1->coef=(p1->coef)-(p2->coef);
p2=p2->next;
}
p1=p1->next;
}
}
else{
p1=head1->next;
p2=head2->next;
while(p2!=NULL){
p2->coef=-(p2->coef);
if(p1->index==p2->index){
p2->coef=(p2->coef)+(p1->coef);
p1=p1->next;
}
p2=p2->next;
}
}
}
main(void){
structnode*head1; //多项式1头节点
structnode*head2; //多项式2头节点
intindex_current=0; //要输入的指数
intcoef_current=0; //要输入的系数
intindex_max1; //多项式1次数
intindex_max2; //多项式2次数
int i=0; //循环控制变量
head1=init_ll();
printf("-请输入第一个多项式的次数- ");
scanf_s("%d",&index_max1);
for(i=0;i<index_max1;i++){
printf("-请输入%d次项系数:",index_current);
scanf_s("%d",&coef_current);
insert_ll(head1,coef_current,index_current); //头插法
index_current++;
}
head2=init_ll();
index_current=0;
printf("-请输入第二个多项式的次数- ");
scanf_s("%d",&index_max2);
for(i=0;i<index_max2;i++){
printf("-请输入%d次项系数:",index_current);
scanf_s("%d",&coef_current);
insert_ll(head2,coef_current,index_current); //头插法
index_current++;
}
add_poly(head1,index_max1,head2,index_max2); //进行相加操作
//sub_poly(head1,index_max1,head2,index_max2); //进行相减操作
printf("多项式1的结果为:");
reverse_ll(head1);
printf("多项式2的结果为:");
reverse_ll(head2);
destory_ll(head1);
destory_ll(head2);
getchar();
getchar();
}