C语言用链表实现一元多项式的相加
#include<stdio.h>#include<malloc.h>#include<stdlib.h>typedefstructpolynode{floatcoef;...
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct polynode
{
float coef;
int exp;
struct polynode *next;
}polynode,*PLinklist;
PLinklist Create(int n)
{
PLinklist L,p;
int i;
L=(PLinklist)malloc(sizeof(polynode));
L->next=NULL;
for(i=n;i>0;--i)
{
p=(PLinklist)malloc(sizeof(polynode));
scanf("%f %d",&p->coef,&p->exp);
p->next=L->next;
L->next=p;
}
return(L);
}
PLinklist Attach(float co,int ex,PLinklist o)
{
PLinklist c;
c=(PLinklist)malloc(sizeof(polynode));
c->coef=co;
c->exp=ex;
o->next=c;
return c;
}
PLinklist Print(PLinklist L)
{
int i=0;
PLinklist p=L;
while(p->next!=NULL)
{
i++;
printf("%fx^%d ",p->coef,p->exp);
p=p->next;
}
printf("\n");
}
PLinklist Add(PLinklist A,PLinklist B)
{
PLinklist C;
PLinklist o;
PLinklist p=A;
PLinklist q=B;
int sum;
C=(PLinklist)malloc(sizeof(polynode));
o=C;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
sum=p->coef+q->coef;
if(sum!=0)
o=Attach(sum,p->exp,o);
p=p->next;
q=q->next;
}
else if(p->exp<q->exp)
{
o=Attach(q->coef,q->exp,o);
q=q->next;
}
else{
o=Attach(p->coef,p->exp,o);
p=p->next;
}
}
while(p!=NULL)
{
o=Attach(p->coef,p->exp,o);
p=p->next;
}
while(q!=NULL)
{
o=Attach(q->coef,q->exp,o);
q=q->next;
}
o->next=NULL;
p=C;
C=C->next;
return C;
}
void main()
{
int n,m;
PLinklist *A,*B,*C;
printf("Input the length of A :\n");
scanf("%d",&n);
A=Create(n);
Print(A);
printf("Input the length of B :\n");
scanf("%d",&n);
b=Create(n);
Print(b);
C=Add(A,B);
Print(C);
}
这是我写的程序,弄了好久出不来,请高手看看吧 展开
#include <malloc.h>
#include <stdlib.h>
typedef struct polynode
{
float coef;
int exp;
struct polynode *next;
}polynode,*PLinklist;
PLinklist Create(int n)
{
PLinklist L,p;
int i;
L=(PLinklist)malloc(sizeof(polynode));
L->next=NULL;
for(i=n;i>0;--i)
{
p=(PLinklist)malloc(sizeof(polynode));
scanf("%f %d",&p->coef,&p->exp);
p->next=L->next;
L->next=p;
}
return(L);
}
PLinklist Attach(float co,int ex,PLinklist o)
{
PLinklist c;
c=(PLinklist)malloc(sizeof(polynode));
c->coef=co;
c->exp=ex;
o->next=c;
return c;
}
PLinklist Print(PLinklist L)
{
int i=0;
PLinklist p=L;
while(p->next!=NULL)
{
i++;
printf("%fx^%d ",p->coef,p->exp);
p=p->next;
}
printf("\n");
}
PLinklist Add(PLinklist A,PLinklist B)
{
PLinklist C;
PLinklist o;
PLinklist p=A;
PLinklist q=B;
int sum;
C=(PLinklist)malloc(sizeof(polynode));
o=C;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
sum=p->coef+q->coef;
if(sum!=0)
o=Attach(sum,p->exp,o);
p=p->next;
q=q->next;
}
else if(p->exp<q->exp)
{
o=Attach(q->coef,q->exp,o);
q=q->next;
}
else{
o=Attach(p->coef,p->exp,o);
p=p->next;
}
}
while(p!=NULL)
{
o=Attach(p->coef,p->exp,o);
p=p->next;
}
while(q!=NULL)
{
o=Attach(q->coef,q->exp,o);
q=q->next;
}
o->next=NULL;
p=C;
C=C->next;
return C;
}
void main()
{
int n,m;
PLinklist *A,*B,*C;
printf("Input the length of A :\n");
scanf("%d",&n);
A=Create(n);
Print(A);
printf("Input the length of B :\n");
scanf("%d",&n);
b=Create(n);
Print(b);
C=Add(A,B);
Print(C);
}
这是我写的程序,弄了好久出不来,请高手看看吧 展开
3个回答
展开全部
C语言代码:
#include "stdio.h"
#include "malloc.h"
/* 链表结点结构 */
typedef struct LNode{
double coef; /* 系数 */
int exp; /* 指数 */
struct LNode *next;
}LNode;
/* 初始化链表 */
LNode *Init()
{
LNode *head = (LNode*)malloc(sizeof(LNode));
head->next = NULL;
return head;
}
/* 将值为data的结点插入到head链表的最后 */
void AddNode(LNode *head, double coef, int exp)
{
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链表的所有结点的值 */
void List(LNode *head)
{
LNode *curr;
printf("All nodes : ");
for(curr=head->next; curr!=NULL; curr=curr->next)
{
printf("(%lf, %d)\t", curr->coef, curr->exp);
}
printf("\n");
}
/* 返回head链表的所有结点的数量 */
int Size(LNode *head)
{
int len = 0;
LNode *curr;
for(curr=head->next; curr!=NULL; curr=curr->next,len++);
return len;
}
LNode *Add(LNode *headA, LNode *headB)
{
LNode *currA, *currB, *headC;
double sum;
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;
}
else if(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;
}
return headC;
}
void main()
{
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);
}
运行测试:
展开全部
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct polynode
{
float coef;
int exp;
struct polynode *next;
}polynode,*PLinklist;
PLinklist Create(int n)
{
PLinklist L,p;
int i;
L = (PLinklist)malloc(sizeof(polynode));
L->next = NULL;
for(i=n; i>0 ;--i)
{
p = (PLinklist)malloc(sizeof(polynode));
scanf("%f%d",&p->coef,&p->exp);
p->next = L->next;
L->next = p;
}
return(L);
}
PLinklist Attach(float co,int ex,PLinklist o)
{
PLinklist c;
c = (PLinklist)malloc(sizeof(polynode));
c->coef = co;
c->exp = ex;
o->next = c;
return c;
}
void Print(PLinklist L)
{
int i = 0;
PLinklist p = L;
while(p->next != NULL)
{
i++;
printf("%fx^%d ",p->coef,p->exp);
p=p->next;
}
printf("\n");
}
PLinklist Add(PLinklist A,PLinklist B)
{
PLinklist C;
PLinklist o;
PLinklist p = A;
PLinklist q = B;
float sum;
C = (PLinklist)malloc(sizeof(polynode));
o=C;
while((p != NULL) && (q != NULL))
{
if(p->exp == q->exp)
{
sum = p->coef+q->coef;
if(sum != 0)
o = Attach(sum,p->exp,o);
p = p->next;
q = q->next;
}
else if(p->exp < q->exp)
{
o = Attach(q->coef,q->exp,o);
q = q->next;
}
else{
o = Attach(p->coef,p->exp,o);
p = p->next;
}
}
while(p != NULL)
{
o = Attach(p->coef,p->exp,o);
p = p->next;
}
while(q != NULL)
{
o = Attach(q->coef,q->exp,o);
q = q->next;
}
o->next = NULL;
p = C;
C = C->next;
return C;
}
void main()
{
int n;
PLinklist A, B, C;
printf("Input the length of A :\n");
scanf("%d",&n);
A = Create(n);
Print(A);
printf("Input the length of B :\n");
scanf("%d",&n);
B = Create(n);
Print(B);
C = Add(A,B);
Print(C);
}
OK啦 ...帮你改了下细节部分,没改你实现思路....可以编译通过的
#include <malloc.h>
#include <stdlib.h>
typedef struct polynode
{
float coef;
int exp;
struct polynode *next;
}polynode,*PLinklist;
PLinklist Create(int n)
{
PLinklist L,p;
int i;
L = (PLinklist)malloc(sizeof(polynode));
L->next = NULL;
for(i=n; i>0 ;--i)
{
p = (PLinklist)malloc(sizeof(polynode));
scanf("%f%d",&p->coef,&p->exp);
p->next = L->next;
L->next = p;
}
return(L);
}
PLinklist Attach(float co,int ex,PLinklist o)
{
PLinklist c;
c = (PLinklist)malloc(sizeof(polynode));
c->coef = co;
c->exp = ex;
o->next = c;
return c;
}
void Print(PLinklist L)
{
int i = 0;
PLinklist p = L;
while(p->next != NULL)
{
i++;
printf("%fx^%d ",p->coef,p->exp);
p=p->next;
}
printf("\n");
}
PLinklist Add(PLinklist A,PLinklist B)
{
PLinklist C;
PLinklist o;
PLinklist p = A;
PLinklist q = B;
float sum;
C = (PLinklist)malloc(sizeof(polynode));
o=C;
while((p != NULL) && (q != NULL))
{
if(p->exp == q->exp)
{
sum = p->coef+q->coef;
if(sum != 0)
o = Attach(sum,p->exp,o);
p = p->next;
q = q->next;
}
else if(p->exp < q->exp)
{
o = Attach(q->coef,q->exp,o);
q = q->next;
}
else{
o = Attach(p->coef,p->exp,o);
p = p->next;
}
}
while(p != NULL)
{
o = Attach(p->coef,p->exp,o);
p = p->next;
}
while(q != NULL)
{
o = Attach(q->coef,q->exp,o);
q = q->next;
}
o->next = NULL;
p = C;
C = C->next;
return C;
}
void main()
{
int n;
PLinklist A, B, C;
printf("Input the length of A :\n");
scanf("%d",&n);
A = Create(n);
Print(A);
printf("Input the length of B :\n");
scanf("%d",&n);
B = Create(n);
Print(B);
C = Add(A,B);
Print(C);
}
OK啦 ...帮你改了下细节部分,没改你实现思路....可以编译通过的
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我写了一个 你看下
思路相同
#include "stdio.h"
#include "stdlib.h"
#define Null 0
typedef struct LNode
{
int coef;
int expn;
struct LNode *next;
}LNode,*Linklist;
void Creat(Linklist *L)
{
int i,j;
Linklist p,q;
(*L)=(Linklist)malloc(sizeof(LNode));
(*L)->coef=0;(*L)->next=Null;
p=*L;
scanf("%d,%d",&i,&j);
while(i!=0)
{
q=(Linklist)malloc(sizeof(LNode));
q->coef=i;q->expn=j;q->next=Null;
p->next=q;
p=q;
scanf("%d,%d",&i,&j);
}
}
void Add(Linklist La, Linklist Lb, Linklist *Lc)
{
Linklist pa=La->next,pb=Lb->next,pc;
*Lc=(Linklist)malloc(sizeof(LNode));
(*Lc)->coef=0;(*Lc)->next=Null;
pc=*Lc;
while(pa&&pb)
{
if(pa->expn<pb->expn)
{
pc->next=pa;
pc=pa;
pa=pa->next;
}
else
{
if(pa->expn>pb->expn)
{
pc->next=pb;
pc=pb;
pb=pb->next;
}
else
{
pa->coef=pa->coef+pb->coef;
pc=pa;
pa=pa->next;
pb=pb->next;
}
}
}
if(pa) pc->next=pa;
else pc->next=pb;
}
void Print(Linklist L)
{
Linklist p=L->next;
while(p)
{
printf("%d,%d\n",(int)p->coef,p->expn);
p=p->next;
}
}
int main()
{
Linklist La,Lb,Lc;
printf("Input the data of La:\n");
Creat(&La);
printf("Input the data of Lb:\n");
Creat(&Lb);
printf("\nLa:\n");
Print(La);
printf("\nLb:\n");
Print(Lb);
printf("\nAdd La and Lb to Lc:\n");
Add(La,Lb,&Lc);
Print(Lc);
printf("\n");
getch();
return 0;
}
思路相同
#include "stdio.h"
#include "stdlib.h"
#define Null 0
typedef struct LNode
{
int coef;
int expn;
struct LNode *next;
}LNode,*Linklist;
void Creat(Linklist *L)
{
int i,j;
Linklist p,q;
(*L)=(Linklist)malloc(sizeof(LNode));
(*L)->coef=0;(*L)->next=Null;
p=*L;
scanf("%d,%d",&i,&j);
while(i!=0)
{
q=(Linklist)malloc(sizeof(LNode));
q->coef=i;q->expn=j;q->next=Null;
p->next=q;
p=q;
scanf("%d,%d",&i,&j);
}
}
void Add(Linklist La, Linklist Lb, Linklist *Lc)
{
Linklist pa=La->next,pb=Lb->next,pc;
*Lc=(Linklist)malloc(sizeof(LNode));
(*Lc)->coef=0;(*Lc)->next=Null;
pc=*Lc;
while(pa&&pb)
{
if(pa->expn<pb->expn)
{
pc->next=pa;
pc=pa;
pa=pa->next;
}
else
{
if(pa->expn>pb->expn)
{
pc->next=pb;
pc=pb;
pb=pb->next;
}
else
{
pa->coef=pa->coef+pb->coef;
pc=pa;
pa=pa->next;
pb=pb->next;
}
}
}
if(pa) pc->next=pa;
else pc->next=pb;
}
void Print(Linklist L)
{
Linklist p=L->next;
while(p)
{
printf("%d,%d\n",(int)p->coef,p->expn);
p=p->next;
}
}
int main()
{
Linklist La,Lb,Lc;
printf("Input the data of La:\n");
Creat(&La);
printf("Input the data of Lb:\n");
Creat(&Lb);
printf("\nLa:\n");
Print(La);
printf("\nLb:\n");
Print(Lb);
printf("\nAdd La and Lb to Lc:\n");
Add(La,Lb,&Lc);
Print(Lc);
printf("\n");
getch();
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询