鱼C论坛

 找回密码
 立即注册
查看: 1141|回复: 3

求助!单链表实现多项式相加

[复制链接]
发表于 2021-9-30 14:14:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
test文件
  1. #include"E:\Study Source\Ccode\DataStructure_Ex2\second\SecondLinkList.h"
  2. int main() {
  3.         linklist p;
  4.     initLink(p);
  5.     addNode(p,3);
  6.     display(p);
  7.     printf("%d\n",p->coe);
  8.    
  9.     linklist q;
  10.     initLink(q);
  11.     addNode(q,4);
  12.         display(q);
  13.         printf("%d\n",q->coe);
  14.         
  15.         
  16.         linklist L=addEveryOne(p,q);
  17.         display(L);
  18.     return 0;
  19. }
复制代码

linklist头文件
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <E:\Study Source\Ccode\DataStructure_Ex1\third\c1.h>
  4. typedef struct Link{
  5.         int coe;
  6.     int elem;
  7.     struct Link *next;
  8. }link,*linklist;
  9. Status initLink(linklist &L){
  10.         L=(linklist)malloc(sizeof(link));
  11.         L->next=NULL;
  12.         L->coe=0;
  13.         return OK;
  14. }
  15. Status GetElem(linklist &L,int i,int &coe,int &elem){
  16.         int j=1;
  17.         linklist p=L->next;
  18.         while(p&&(j<i)){
  19.                 p=p->next;
  20.                 ++j;
  21.         }
  22.         if(!p||(j>i))return ERROR;
  23.         coe=p->coe;
  24.         elem=p->elem;
  25.         return OK;
  26. }
  27. link *LocateElem(linklist &L,int coe,int elem){
  28.         linklist p=(linklist)malloc(sizeof(link));
  29.         p=L->next;
  30.         while(p&&(p->coe!=coe)&&p->elem!=elem){
  31.                 p=p->next;
  32.         }
  33.         return p;
  34. }
  35. Status LinkInsert(linklist &L,int i,int coe, int elem){
  36.         linklist p=L;
  37.         int j=0;
  38.         while(p&&(j<i-1)){
  39.                 p=p->next;
  40.                 ++j;
  41.         }
  42.         if(!p||(j>i-1))
  43.         return ERROR;
  44.         linklist s=(linklist)malloc(sizeof(link));
  45.         s->coe=coe;
  46.         s->elem=elem;
  47.         s->next=p->next;
  48.         p->next=s;
  49.         L->coe++;
  50.         return OK;
  51. }
  52. Status ListDel(linklist &L,int i){
  53.         linklist p=L;
  54.         int j=0;
  55.         while((p->next)&&(j<i-1)){
  56.                 p=p->next;
  57.                 ++j;
  58.         }
  59.         if(!(p->next)||(j>i-1))return ERROR;
  60.         linklist q=p->next;
  61.         p->next=q->next;
  62.         free(q);
  63.         L->coe--;
  64.         return OK;
  65. }
  66. void addNode(linklist &L,int n){
  67.         linklist p,tail;
  68.         tail=L;
  69.         for(int i=0;i<n;i++){
  70.                 p=(linklist)malloc(sizeof(link));
  71.                 scanf("%d %d",&p->coe,&p->elem);
  72.                 tail->next=p;
  73.                 tail=p;
  74.         }
  75.         L->coe+=n;
  76.         tail->next=NULL;
  77. }
  78. void addNode(linklist &L,int coe,int elem){
  79.         linklist p,tail;
  80.         tail=L;
  81.         while(tail->next){
  82.         tail=tail->next;
  83.     }
  84.                 p=(linklist)malloc(sizeof(link));
  85.                 p->coe=coe;p->elem=elem;
  86.                 tail->next=p;
  87.                 tail=p;
  88.         L->coe++;
  89.         tail->next=NULL;
  90. }
  91. void display(linklist &L){
  92.     linklist temp=L;
  93.     while (temp->next) {
  94.         temp=temp->next;
  95.         printf("%d的%d次方 ",temp->coe,temp->elem);
  96.     }
  97.     printf("\n");
  98. }
  99. linklist addEveryOne(linklist &a,linklist &b){
  100.     linklist p,q,r,temp;
  101.     p=a->next;
  102.     q=b->next;
  103.     r=(linklist)malloc(sizeof(link));
  104.     temp=r;
  105.     while(p&&q){
  106.         if(p->elem>q->elem){
  107.                 addNode(r,q->coe,q->elem);
  108.                 temp=temp->next;
  109.             q=q->next;
  110.         }
  111.         else if(p->elem<q->elem){
  112.             addNode(r,p->coe,p->elem);
  113.             temp=temp->next;
  114.             p=p->next;
  115.         }
  116.         else if(p->elem==q->elem){
  117.             int sum = p->coe+q->coe;
  118.             if(sum == 0){
  119.                 p = p->next;
  120.                 q = q->next;
  121.             }
  122.             else{
  123.                     addNode(r,sum,p->elem);
  124.                     temp=temp->next;
  125.                     p = p->next;
  126.                 q = q->next;
  127.                 }
  128.             }
  129.         }
  130.         if(p!=NULL)
  131.             temp->next = p;
  132.         if(q!=NULL)
  133.             temp->next = q;
  134.         if(p==NULL&&q==NULL)
  135.             temp->next=NULL;
  136.     return r;
  137. }
复制代码
运行addEveryOne函数的时候似乎就一直不动无法退出循环了XD,求助
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-9-30 20:00:11 | 显示全部楼层
本帖最后由 jhq999 于 2021-10-1 07:01 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //#include <E:\Study Source\Ccode\DataStructure_Ex1\third\c1.h>
  4. enum Status{ERROR,OK};
  5. typedef struct Link{
  6.         int coe;
  7.     int elem;
  8.     struct Link *next;
  9. }link,*linklist;
  10. Status initLink(linklist &L){
  11.         L=(linklist)malloc(sizeof(link));
  12.         L->next=NULL;
  13.         L->coe=0;
  14.         return OK;
  15. }
  16. Status GetElem(linklist &L,int i,int &coe,int &elem){
  17.         int j=1;
  18.         linklist p=L->next;
  19.         while(p&&(j<i)){
  20.                 p=p->next;
  21.                 ++j;
  22.         }
  23.         if(!p||(j>i))return ERROR;
  24.         coe=p->coe;
  25.         elem=p->elem;
  26.         return OK;
  27. }
  28. link *LocateElem(linklist &L,int coe,int elem){
  29.         linklist p=(linklist)malloc(sizeof(link));
  30.         p=L->next;
  31.         while(p&&(p->coe!=coe)&&p->elem!=elem){
  32.                 p=p->next;
  33.         }
  34.         return p;
  35. }
  36. Status LinkInsert(linklist &L,int i,int coe, int elem){
  37.         linklist p=L;
  38.         int j=0;
  39.         while(p&&(j<i-1)){
  40.                 p=p->next;
  41.                 ++j;
  42.         }
  43.         if(!p||(j>i-1))
  44.         return ERROR;
  45.         linklist s=(linklist)malloc(sizeof(link));
  46.         s->coe=coe;
  47.         s->elem=elem;
  48.         s->next=p->next;
  49.         p->next=s;
  50.         L->coe++;
  51.         return OK;
  52. }
  53. Status ListDel(linklist &L,int i){
  54.         linklist p=L;
  55.         int j=0;
  56.         while((p->next)&&(j<i-1)){
  57.                 p=p->next;
  58.                 ++j;
  59.         }
  60.         if(!(p->next)||(j>i-1))return ERROR;
  61.         linklist q=p->next;
  62.         p->next=q->next;
  63.         free(q);
  64.         L->coe--;
  65.         return OK;
  66. }
  67. void addNode(linklist &L,int n){
  68.         linklist p,tail;
  69.         tail=L;
  70.         for(int i=0;i<n;i++){
  71.                 p=(linklist)malloc(sizeof(link));
  72.                 scanf("%d %d",&p->coe,&p->elem);
  73.                 tail->next=p;
  74.                 tail=p;
  75.         }
  76.         L->coe+=n;
  77.         tail->next=NULL;
  78. }
  79. void addNode(linklist &L,int coe,int elem){
  80.         linklist p,tail;
  81.         tail=L;
  82.         while(tail->next){
  83.         tail=tail->next;
  84.     }
  85.                 p=(linklist)malloc(sizeof(link));
  86.                 p->coe=coe;p->elem=elem;
  87.                 tail->next=p;
  88.                 tail=p;
  89.         L->coe++;
  90.         tail->next=NULL;
  91. }
  92. void display(linklist &L){
  93.     linklist temp=L;
  94.     while (temp->next) {
  95.         temp=temp->next;
  96.         printf("%d的%d次方 ",temp->coe,temp->elem);
  97.     }
  98.     printf("\n");
  99. }
  100. linklist addEveryOne(linklist &a,linklist &b){
  101.     linklist p,q,r,temp;
  102.     p=a->next;
  103.     q=b->next;
  104.     r=(linklist)malloc(sizeof(link));
  105.     r->coe=r->elem=0;r->next=NULL;///////////////////////////////////////初始化为0,主要是r->next=NULL,要不天知道这个指针指向哪里?
  106.     temp=r;
  107.     while(p&&q){
  108.         if(p->elem>q->elem){
  109.                 addNode(r,q->coe,q->elem);
  110.                 temp=temp->next;
  111.             q=q->next;
  112.         }
  113.         else if(p->elem<q->elem){
  114.             addNode(r,p->coe,p->elem);
  115.             temp=temp->next;
  116.             p=p->next;
  117.         }
  118.         else if(p->elem==q->elem){
  119.             int sum = p->coe+q->coe;
  120.             if(sum == 0){
  121.                 p = p->next;
  122.                 q = q->next;
  123.             }
  124.             else{
  125.                     addNode(r,sum,p->elem);
  126.                     temp=temp->next;
  127.                     p = p->next;
  128.                 q = q->next;
  129.                 }
  130.             }
  131.         }
  132.         if(p!=NULL)
  133.             temp->next = p;
  134.         if(q!=NULL)
  135.             temp->next = q;
  136.         if(p==NULL&&q==NULL)
  137.             temp->next=NULL;
  138.     return r;
  139. }
  140. //#include"E:\Study Source\Ccode\DataStructure_Ex2\second\SecondLinkList.h"
  141. int main() {
  142.         linklist p;
  143.     initLink(p);
  144.     addNode(p,3);
  145.     display(p);
  146.     printf("%d\n",p->coe);
  147.    
  148.     linklist q;
  149.     initLink(q);
  150.     addNode(q,4);
  151.         display(q);
  152.         printf("%d\n",q->coe);
  153.         
  154.         
  155.         linklist L=addEveryOne(p,q);
  156.         display(L);
  157.     return 0;
  158. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-1 07:04:25 | 显示全部楼层
反正我是不管声明都初始化,宁可开始费功夫也比出BUG强
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-1 18:20:02 | 显示全部楼层
代码级别高啊!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 06:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表