回 帖 发 新 帖 刷新版面

主题:[转帖]建立一个具有N个结点的单链表,

PNode Createlist(PNode llist,float n)  /* 创建链表*/
{     PNode  p,q;
     int    i;
  llist=(PNode)malloc(sizeof (struct node));
  llist->coef=n;
  llist->exp=-1;
  llist->link=NULL;
  p=llist;
for (i=0;i<(int)n;i++)
   {  q=(PNode)malloc(sizeof (struct node));
      scanf("%f,%d",&q->coef,&q->exp);
      p->link=q;
      q->link=NULL;
    }
  return llist;
 }
 我想问一下没有加这句p=p->link;那个P指针就是头指针一直没有移动,执行
p->link=q;是不是有问题,但运行的时候却没问题??

回复列表 (共3个回复)

沙发


应该是不对的,如果按你说的能运行正确,你应该把你程序贴上完,我也好调试看看

板凳

typedef  struct poly{ float    coef;
                       int   exp;
                       struct   poly   * next;}polytype;
    (polytype *)  createlist (polytype *llist,float n) /*创建链表*/
 { struct poly *p,*t;
      int i;
      p=llist;
     if(p!=null) {p->coef=n;
                  p->exp=-1;}
    p=(polytype *) malloc(sizeof(struct poly));/*申请第一个节点空间*/
        for(i=0;i<n;i++)      /*逐个申请后面节点的空间*/
       {  t=(polytype *) malloc(sizeof(struct poly));
       scanf("%f%d\n",&p->coef,&p->exp);
       p->link=t;
       t->link=null;
       p=p->link;
       }
     return(llist);
 }

 int  printf_list(polytype *llsit,float n) /*排序算法*/
 {   int i,t,q,s;    /*q,s用于交换变量值时充当临时变量*/
     float r;
   struct poly *p;
   llist=(polytype *) createlist(polytype *llist,float n);
   p=llist;
   t=p->exp;r=p->coef;
   p=p->link; 
   while(p)
   { if( t<p->exp )  { q=t;t=p->exp;p->exp=q;  /*把指数按从大到小排列*/
   s=r;r=p->coef;p->coef=s;    /*系数跟原指数匹配*/
   p=p->link;
   }
   p=llist;       /*把指针l赋为头指针*/
   printf("%f",&p->link);
   p=p->link;
   for(i=0;i<n;i++)
   { printf("%f%d",&p->coef,&p->exp); p=p->link;}
   return(1);
   }
   


  ( polytype *)  add_list(polytype *pa,polytype *pb)
{ struct poly *p,*q,*pre,*pc,*u;int x=0,sel;
     p=pa->next;
     q=pb->next;
     pre=pa;
     pc=pa;
while(p&&q)
{
     if ((p->exp)<(q->exp))
     sel=1;
     else if ((p->exp)==(q->exp)) sel=2;
     else  sel=3;
     switch(sel)
     {case 1: {pre=p;p=p->next;break;}
      case 2: {x=p->coef+q->coef;
             if (x!=0)    {p->coef=x;pre=p;}
               {pre->next=p->next;
                p=pre->next;
                u=q;
                q=q->next;
                free(u);
}
break;
}
case 3: {u=q->next;pre->next=q;q->next=p;pre=q;q=u;break;}
 }
}
    if (q!=null)   pre->next=q;    free(pb);
    p=pc->next;
printf("now these %drecords are:\n",n);
            while (p!=null)
            {printf("%d,%d\n",p->coef,p->exp);
            p=p->next;}
return(pc);
}


  main()
  {  struct poly *pa,*pb
     int n1,n2;
    printf("请输入两多项式的长度(以浮点型输入):\n");
     scanf("%f,%f",&n2,&n1);
   printf("请逐项输入系数和指数:\n");
   pa=( polytype *) createlist (pa,n1);
   printf("继续输入另一个多项式的系数和指数:\n");
   pb=( polytype *) createlist (pb,n2);
   printf("输出第一个降序序列");
   printf_list(pa,n1);
   printf("输出第二个降序序列");
   printf_list(pb, n2);
   add_list(pa,pb);
  }

你干脆帮我看看这个,要求是建立并输出多项式,输出多项式,n是多项式的个数,ci,ei是第i项的系数和指数,序列按降序排列,输出两个多项式相加的结果!我实在弄不出来了!对于那些返回值的类型还有结点类型真的混淆了!!

3 楼

#include<stdio.h>
#include<malloc.h> 
typedef  struct poly{ float    coef;
                       int   exp;
                       struct   poly   * next;}polytype;
    polytype *  createlist (polytype *llist,float n) /*创建链表*/
 { struct poly *p,*t;
      int i;
      p=llist;
     if(p!=NULL) {p->coef=n;
                  p->exp=-1;}
    p=(polytype *) malloc(sizeof(struct poly));/*申请第一个节点空间*/
        for(i=0;i<n;i++)      /*逐个申请后面节点的空间*/
       {  t=(polytype *) malloc(sizeof(struct poly));
       scanf("%f%d",&p->coef,&p->exp);
       p->next=t;
       t->next=NULL;
       p=p->next;
       }
     return(llist);
 }

 int  printf_list(polytype *llist,float n) /*排序算法*/
 {   int i,t,q,s;    /*q,s用于交换变量值时充当临时变量*/
     float r;
   struct poly *p;
   llist= createlist(llist, n);
   p=llist;
   t=p->exp;r=p->coef;
   p=p->next;
   while(p)
   { if( t<p->exp )  { q=t;t=p->exp;p->exp=q;  /*把指数按从大到小排列*/
   s=r;r=p->coef;p->coef=s;    /*系数跟原指数匹配*/
   p=p->next;
   } }
   p=llist;       /*把指针l赋为头指针*/
   printf("%f",*p->next);
   p=p->next;
   for(i=0;i<n;i++)
   { printf("%f%d",p->coef,p->exp); p=p->next;}
   return(1);
   }
   


   polytype  *add_list(polytype *pa, polytype *pb)
  {   struct poly *p,*q,*pre,*pc,*u;
     int x=0,sel,n;
     p=pa->next;
     q=pb->next;
     pre=pa;
     pc=pa;

while(p&&q)
{
     if ((p->exp)<(q->exp))
     sel=1;
     else if ((p->exp)==(q->exp)) sel=2;
     else  sel=3;
     switch(sel)
     {case 1: {pre=p;p=p->next;break;}
      case 2: {x=p->coef+q->coef;
             if (x!=0)    {p->coef=x;pre=p;}
               {pre->next=p->next;
                p=pre->next;
                u=q;
                q=q->next;
                free(u);
}
break;
}
case 3: {u=q->next;pre->next=q;q->next=p;pre=q;q=u;break;}
 }
}
    if (q!=NULL)   pre->next=q;    free(pb);
    p=pc->next;
printf("now these %drecords are:\n",n);
            while (p!=NULL)
            {printf("%d,%d\n",p->coef,p->exp);
            p=p->next;}
return(pc);
}


  main()
  {  struct poly *pa,*pb ;
     float n1,n2;
    printf("请输入两多项式的长度(以浮点型输入):\n");
     scanf("%f%f",&n1,&n2);
     pa=(polytype *) malloc(sizeof(struct poly));
     pb=(polytype *) malloc(sizeof(struct poly));
   printf("请逐项输入系数和指数:\n");
   pa= createlist (pa,n1);
   printf("继续输入另一个多项式的系数和指数:\n");
   pb= createlist (pb,n2);
   printf("输出第一个降序序列");
   printf_list(pa,n1);
   printf("输出第二个降序序列");
   printf_list(pb, n2);
   add_list(pa,pb);
   getch( );
  }

我来回复

您尚未登录,请登录后再回复。点此登录或注册