| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 398 人关注过本帖
标题:将数读入链表的基础问题
只看楼主 加入收藏
goldfrapp04
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-5-10
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
将数读入链表的基础问题
题目是读入两个多项式,然后做加法,用链表实现。C语言上学期学的,这学期学数据结构,链表几乎忘光,自己看着课本debug不出来……
整个程序在这里,但是大侠们帮我解决输入数据后无法正常CreatePoly的问题就好。
比如P1: x+x^2+2x^3, P2:2+3x^2+x^5

#include <stdio.h>

struct Poly{
       double coef;
       int expt;
       struct Poly *next;
       };
int size = sizeof(struct Poly);

struct Poly *CreatePoly();
struct Poly *AddPoly(struct Poly *p1, struct Poly *p2);
struct Poly *PrintPoly(struct Poly *head);
      
main()
{
      struct Poly *head1, *head2, *headsum;
      
      head1 = head2 = headsum = NULL;
      printf("Polynomial 1\n");
      head1 = CreatePoly();
      printf("Polynomial 2\n");
      head2 = CreatePoly();
      if(head1 != NULL && head2 != NULL)
               headsum = AddPoly(head1, head2);
      PrintPoly(head1);
      printf("+");
      PrintPoly(head2);
      printf("=");
      PrintPoly(headsum);
}
                  
struct Poly *CreatePoly()
{
       struct Poly *head, *tail, *p;
       double coef;
       int expt;
      
       printf("Input coefficients & exponents ascendingly. End with the coefficient 0\n");
       scanf("%lf%d", &coef, &expt);
       head = tail = NULL;
       while(1){
                  p = (struct Poly *) malloc(size);   /*调试时是到这里就跳出TC……为什么呢?*/
                  if(p == NULL){
                          printf("Memory allocation failure.\n");
                          return NULL;
                  }
                  p->coef = coef;
                  p->expt = expt;
                  if(head == NULL){
                          head = p;
                          tail = head;
                  }
                  else tail->next = p;
                  tail = p;
                  scanf("%lf", &coef);
                  if(coef != 0)
                          scanf("%d", &expt);
                  else break;
       }
       return head;
}

struct Poly *AddPoly(struct Poly *p1, struct Poly *p2)
{
       struct Poly *q1, *q2, *head, *tail, *p;
      
       head = tail = NULL;
       q1 = p1;
       q2 = p2;
       do{
            if(head == NULL){
                    p = (struct Poly *) malloc(size);
                    if(q1->expt < q2->expt){
                                p->coef = q1->coef;
                                p->expt = q1->expt;
                                q1 = q1->next;
                                }
                    else if(q1->expt == q2->expt){
                         p->coef = q1->coef + q2->coef;
                         p->expt = q1->expt;
                         q1 = q1->next;
                         q2 = q2->next;
                         }
                    else{
                         p->coef = q2->coef;
                         p->expt = q2->expt;
                         q2 = q2->next;
                         }
                    head = p;
                    tail = head;
            }
            else{
                 if(q1->expt < q2->expt || q2 == NULL){
                             p->coef = q1->coef;
                             p->expt = q1->expt;
                             q1 = q1->next;
                             }
                 else if(q1->expt == q2->expt){
                         p->coef = q1->coef + q2->coef;
                         p->expt = q1->expt;
                         q1 = q1->next;
                         q2 = q2->next;
                         }
                 else{
                         p->coef = q2->coef;
                         p->expt = q2->expt;
                         q2 = q2->next;
                         }
                 tail->next = p;
                 tail = p;
            }
       }while(!(q1->next == NULL && q2->next == NULL));
       return head;
}
                 
struct Poly *PrintPoly(struct Poly *head)
{
       struct Poly *p;   
       for(p = head; p->next != NULL; p = p->next){
             if(p->expt != 0)
                  printf("%lfx^%d+", p->coef, p->expt);   
             else printf("%lf+", p->coef);
       }
       if(p->expt != 0)
                  printf("%lfx^%d", p->coef, p->expt);
       else printf("%lf", p->coef);
}               
搜索更多相关主题的帖子: 链表 基础 
2010-09-14 00:07
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:20 
没有你说的那个问题啊

struct Poly *PrintPoly(struct Poly *head);函数没有返回值
加个#include<malloc.h>试试!
2010-09-14 09:08
goldfrapp04
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-5-10
收藏
得分:0 
回复 2楼 hahayezhe
有的,多几项就有了。
比如P1:X+X^2
P2:2+2x^2+x^5
可以再帮我看看么?
我照你的那个改了~PrintPoly的确有问题……
malloc.h是不是TC没有的?
2010-09-14 15:43
encounter
Rank: 5Rank: 5
来 自:扬州
等 级:职业侠客
威 望:2
帖 子:150
专家分:359
注 册:2010-7-24
收藏
得分:0 

 x+x^2+2x^3=x(1+x(1+2x))

ping   nbtstat   netstat   tracert    nat   at    ftp   telnet..................
2010-09-14 23:20
快速回复:将数读入链表的基础问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031699 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved