多项式的加法运算(等待修改)

来源:互联网 发布:互联网大数据 编辑:程序博客网 时间:2024/05/24 04:43
#include <stdio.h>#include <stdlib.h>typedef int ElemType;typedef struct List{ElemType index;ElemType aroe;struct List * next;}Node,*LinkList;int main(){int n;void Inital_List(LinkList *h);void Creat_List(LinkList h,int n);void Show_List(LinkList h);int Judge_Empty(LinkList h);LinkList head;Inital_List(&head);printf("请输入第一个多项式的项数:");scanf("%d",&n);Creat_List(head,n);Show_List(head);printf("请输入第二个多项式的项数:");scanf("%d",&n);Creat_List(head,n);printf("********************************************************\n");printf("最终的结果为:\n");Show_List(head);free(head);}void Inital_List(LinkList *h)//初始化带头结点的链表{*h=(LinkList)malloc(sizeof(struct List));if(NULL==*h){printf("空间申请失败,初始化链表失败……\n");}else{(*h)->next=NULL;{printf("空间申请成功,链表已初始化……\n");}}}void Creat_List(LinkList h,int n)//根据所给的函数创建链表{int i,flag;Node *p=NULL,*q1=h,*q;for(q=h->next;q!=NULL;q=q->next){q1=q;}//将q1移动到链表的尾部,因为会有两次函数的写入操作,如果只有一次函数的写入操作,就不需要这一步的移动了if(n<0)printf("数据输入不合法,无法执行函数操作\n");for(i=1;i<=n;i++){flag=1;p=(Node *)malloc(sizeof(struct List));if(NULL==p){printf("节点空间分配失败……\n");}else{printf("请输入第%d项对应的系数和指数:",i);scanf("%d%d",&p->aroe,&p->index);for(q=h->next;q!=NULL;q=q->next){if(q->index==p->index){flag=0;q->aroe+=p->aroe;break;}}//在原来的链表中发现了指数相同的项,直接对其系数进行修改if(flag){p->next=NULL;q1->next=p;q1=p;}//在原来的链表中没有发现指数相同的项,将新项直接连接到链表的尾部}}printf("该函数已成功输入进链表中\n");}int Judge_Empty(LinkList h)//判空函数{if(NULL==h->next)return 1;elsereturn 0;}void Show_List(LinkList h)//显示函数{int k=1;struct List *p=(Node *)malloc(sizeof(struct List));p=h;for(p=h->next;p!=NULL;p=p->next){printf("第%d项元素的系数为%d,指数为%d\n",k++,p->aroe,p->index);}}
下面给出完整的链表写法,动态分配内存给相应的数据
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct List
{
ElemType index;
ElemType aroe;
struct List * next;
}Node,*LinkList;
int main()
{
int n;
void Inital_List(LinkList *h);
void Creat_List(LinkList h,int n);
void Show_List(LinkList h);
int Judge_Empty(LinkList h);
LinkList head;
Inital_List(&head);
printf("请输入第一个多项式的项数:");
scanf("%d",&n);
Creat_List(head,n);
Show_List(head);
printf("请输入第二个多项式的项数:");
scanf("%d",&n);
Creat_List(head,n);
printf("********************************************************\n");
printf("最终的结果为:\n");
Show_List(head);
free(head);
}
void Inital_List(LinkList *h)//初始化带头结点的链表
{
*h=(LinkList)malloc(sizeof(struct List));
if(NULL==*h)
{printf("空间申请失败,初始化链表失败……\n");}
else
{
(*h)->next=NULL;
{printf("空间申请成功,链表已初始化……\n");}
}
}

void Creat_List(LinkList h,int n)//根据所给的函数创建链表
{
int i,flag;
Node *p=NULL,*q1=h,*q;
for(q=h->next;q!=NULL;q=q->next)
{q1=q;}//将q1移动到链表的尾部,因为会有两次函数的写入操作,如果只有一次函数的写入操作,就不需要这一步的移动了
if(n<0)
printf("数据输入不合法,无法执行函数操作\n");
for(i=1;i<=n;i++)
{
flag=1;
p=(Node *)malloc(sizeof(struct List));
if(NULL==p)
{printf("节点空间分配失败……\n");}
else
{
printf("请输入第%d项对应的系数和指数:",i);
scanf("%d%d",&p->aroe,&p->index);
for(q=h->next;q!=NULL;q=q->next)
{
if(q->index==p->index)
{flag=0;q->aroe+=p->aroe;break;}
} //在原来的链表中发现了指数相同的项,直接对其系数进行修改
if(flag)
{
p->next=NULL;
q1->next=p;
q1=p;
}//在原来的链表中没有发现指数相同的项,将新项直接连接到链表的尾部
}
}
printf("该函数已成功输入进链表中\n");
}


int Judge_Empty(LinkList h)//判空函数
{
if(NULL==h->next)
return 1;
else
return 0;
}


void Show_List(LinkList h)//显示函数
{
int k=1;
struct List *p=(Node *)malloc(sizeof(struct List));
p=h;
for(p=h->next;p!=NULL;p=p->next)
{
printf("第%d项元素的系数为%d,指数为%d\n",k++,p->aroe,p->index);
}
}
原创粉丝点击