标题:帮忙看看关于指针的题目的问题出在哪里??
只看楼主
angelakill
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2009-8-25
 问题点数:0 回复次数:0 
帮忙看看关于指针的题目的问题出在哪里??
输入输出的结果不一样。。。。。。。
#include <stdio.h>
#include <malloc.h>
typedef int Elem;
typedef struct LNode
    {Elem exp;
     Elem date;
     struct LNode *next;
    } LinkList;
Elem judge(LinkList *ha,LinkList *hb)  /*判断指数,并返回值*/
    {LinkList *pa=ha->next;
     LinkList *pb=hb->next;
     if(pa->exp<pb->exp) return 1;
     if(pa->exp=pb->exp) return 0;
     if(pa->exp>pb->exp) return -1;
    }
void DispList(LinkList *L)            /*输出链表*/
    {LinkList *p=L->next;
     while(p->next!=NULL)
        {printf("%dx^%d",L->date,L->exp);
         p=p->next;
        }
     printf("\n");
    }
void CreateListR(LinkList *&L,Elem n)       /*尾插入法  创建链表*/
    {LinkList *p,*s;
     Elem i=0;
     L=(LinkList *)malloc(sizeof(LinkList));
     L->next=NULL;
     p=L;
     while(i<n)
        {    s=(LinkList *)malloc(sizeof(LinkList));
            printf("Please input exp:\n");
            scanf("%d",&(s->exp));
            printf("Please input date:\n");
            scanf("%d",&(s->date));
            p->next=s;
            p=s;
            i++;
        }
     p->next=NULL;
     printf("over\n");
    }
void addList(LinkList *ha,LinkList *hb,LinkList *&hc)  /*系数相加*/
    {LinkList *pa=ha->next;
     LinkList *pb=hb->next;
     LinkList *s,*p;
     hc=(LinkList *)malloc(sizeof(LinkList));
     hc->next=NULL;
     p=hc;
     while(pa->next!=NULL&&pb->next!=NULL)
        { s=(LinkList *)malloc(sizeof(LinkList));
          switch (judge(pa,pb))
            { case 1:s->date=pa->date;
                 s->exp=pa->exp;
                 p->next=s;
                 s->next=NULL;
                 p=s;
                 pa=pa->next;
                 break;
             case 0:s->date=pa->date+pb->date;
                 s->exp=pa->exp;
                 p->next=s;
                 s->next=NULL;
                 p=s;
                 pa=pa->next;
                 pb=pb->next;
                 break;
             case -1:s->date=pb->date;
                 s->exp=pb->exp;
                 p->next=s;
                 s->next=NULL;
                 p=s;
                 pb=pb->next;
                 break;
            }
        }
     if(pb->next!=NULL) pa->next=pb;
     while(pa->next!=NULL)
        {
         s->date=pa->date;
         s->exp=pa->exp;
         p->next=s;
         s->next=NULL;
         p=s;
         pa=pa->next;
        }
    }
void main()
    {    LinkList *ha,*hb,*hc;
        CreateListR(ha,2);
        CreateListR(hb,3);
        DispList(ha);
        DispList(hb);
        addList(ha,hb,hc);
        DispList(hc);
    }
搜索更多相关主题的帖子: 指针 
2009-09-03 18:32



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-284694-1-1.html




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

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