LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 851|回复: 9

高手救命!!!!!!

[复制链接]
发表于 2004-3-9 12:36:30 | 显示全部楼层 |阅读模式
这是多项式加法,编译成功,可是运行不行,不知是什么问题




#include <iostream.h>
#define null 0
struct list{int n;int m;list *next;};
struct list *head;struct list *head1;struct list *head2;

void showlist(list *head)
{while(head)
{cout<<head->n<<"X"<<head->m<<endl;
head=head->next;}}

list *create()
{struct list *ps;struct list *pend;ps=new list;
cin>>ps->n>>ps->m;
head=null;pend=ps;
while(ps->n!=0)
{if(head==null)head=ps;else pend->next=ps;
pend=ps;ps=new list;cin>>ps->n>>ps->m;}
pend->next=null;delete ps;return(head);}


void add(list *head1,list *head2)
{struct list *p1,*p2,*p3;
p1=head1;p2=head1;p3=head2;
while(p2!=null)
{if(p2->m<p3->m)
{p2=p2->next;}
else if (p2->m>p3->m)
{p3->next=p1;p1=p3;}
else p2->m=p2->m+p3->m;
p3=p3->next;}

p2->next=p3;
}


void main()
{head1=create();showlist(head1);
head2=create();showlist(head2);
add(head1,head2);showlist(head1);}
发表于 2004-3-9 17:41:43 | 显示全部楼层
这样没法看啊
这类程序一看头都大了,注意缩进啊
发表于 2004-3-9 19:13:37 | 显示全部楼层
请楼主整理一下源代码,不然没法看。
 楼主| 发表于 2004-3-10 20:55:51 | 显示全部楼层
不好意思,初学者,请高手看看吧
#include <iostream.h>

#define null 0

struct list{
int n;
int m;
list *next;};

struct list *head;
struct list *head1;
struct list *head2;

void showlist(list *head)
{
  while(head)
  {
     cout<<head->n<<"X"<<head->m<<endl;
     head=head->next;
  }
}

list *create()
{
   struct list *ps;
   struct list *pend;
   ps=new list;

   cin>>ps->n>>ps->m;
   head=null;
   pend=ps;
   while(ps->n!=0)
   {
    if(head==null)
       head=ps;
    else
       pend->next=ps;

    pend=ps;
    ps=new list;
    cin>>ps->n>>ps->m;
   }
   pend->next=null;
   delete ps;
   return(head);
}


void add(list *head1,list *head2)
{
   struct list *p1,*p2,*p3;
   p1=head1;
   p2=head1;
   p3=head2;

   while(p2!=null)
    {
      if(p2->m<p3->m)
      {
         p2=p2->next;
      }
      else if (p2->m>p3->m)
      {
         p3->next=p1;
         p1=p3;
      }
      else
      p2->m=p2->m+p3->m;

      p3=p3->next;
   }

   p2->next=p3;
}


void main()
{
   head1=create();
   showlist(head1);
   head2=create();
   showlist(head2);
   add(head1,head2);
   showlist(head1);
}
发表于 2004-3-10 22:06:16 | 显示全部楼层
能不能说说你的add函数是怎么考虑的?
总觉得不对,乱的很,
比如对p2->m < p3->m的情况,实际执行的是:
p2=p2->next;
p2->next=p3;
这是怎么个意思呢?

p2->m=p2->m+p3->m;又是怎么回事呢?应该是n吧。

而且你这个程序怎么看都象是个C程序,一点C++的味道都没有。
发表于 2004-3-11 19:38:35 | 显示全部楼层
libinary兄还真有耐心,我一见这种代码就头大,那有心思看下去。
发表于 2004-3-11 20:06:00 | 显示全部楼层
最初由 hjj198398 发表
不好意思,初学者,请高手看看吧

  1. #include <iostream.h>

  2. #define null 0

  3. struct list{
  4. int n;
  5. int m;
  6. list *next;};

  7. struct list *head;
  8. struct list *head1;
  9. struct list *head2;

  10. void showlist(list *head)
  11. {
  12.   while(head)
  13.   {
  14.      cout<<head->n<<"X"<<head->m<<endl;
  15.      head=head->next;
  16.   }
  17. }

  18. list *create()
  19. {
  20.    struct list *ps;
  21.    struct list *pend;
  22.    ps=new list;

  23.    cin>>ps->n>>ps->m;
  24.    head=null;
  25.    pend=ps;
  26.    while(ps->n!=0)
  27.    {
  28.     if(head==null)
  29.        head=ps;
  30.     else
  31.        pend->next=ps;

  32.     pend=ps;
  33.     ps=new list;
  34.     cin>>ps->n>>ps->m;
  35.    }
  36.    pend->next=null;
  37.    delete ps;
  38.    return(head);
  39. }


  40. void add(list *head1,list *head2)
  41. {
  42.    struct list *p1,*p2,*p3;
  43.    p1=head1;
  44.    p2=head1;
  45.    p3=head2;

  46.    while(p2!=null)
  47.     {
  48.       if(p2->m<p3->m)
  49.       {
  50.          p2=p2->next;
  51.       }
  52.       else if (p2->m>p3->m)
  53.       {
  54.          p3->next=p1;
  55.          p1=p3;
  56.       }
  57.       else
  58.       p2->m=p2->m+p3->m;

  59.       p3=p3->next;
  60.    }

  61.    p2->next=p3;
  62. }


  63. void main()
  64. {
  65.    head1=create();
  66.    showlist(head1);
  67.    head2=create();
  68.    showlist(head2);
  69.    add(head1,head2);
  70.    showlist(head1);
  71. }
复制代码
发表于 2004-3-15 11:21:02 | 显示全部楼层
你先帮我解释一下什么叫多项式加法吧?
另外,怎么这么喜欢用1,2,3这种名字呢?编程大忌。
发表于 2004-3-15 14:11:36 | 显示全部楼层
my opinion:
问题都出在add里
首先head1和head2都应该判空,否则会segmentation fault

else if (p2->m>p3->m)
{
    p3->next=p1;
    p1=p3;
}
这样就会把p3插在了head1的前面,并将p3作为新的头。
但同时有没有更新head1,新插入的p3将丢失。
同时,修改了p3->next,head2被破坏。
楼主应该是想把p3插在p2之前吧
我的做法:
p1记录p2的前一个节点(虽然这样的命名很糟糕),即将
if(p2->m<p3->m)
{
     p2=p2->next;
}
改为
if(p2->m<p3->m)
{
     p1 = p2 ;
     p2=p2->next;
}


else if (p2->m>p3->m)
{
    p3->next=p1;
    p1=p3;
}
改为
else if (p2->m>p3->m)
{
    p1 -> next = p3 ;
    p1 = p3 ;
    p3 = p3 -> next ;
    p1 -> next = p2 ;
}

我的add的代码,已测试过了:

  1. list * add( list * head1, list * head2 )
  2. {
  3.    if( head1==null || head2==null )
  4.            return null ;
  5.    struct list *p1,*p2,*p3;
  6.    p1=head1;
  7.    p2=head1;
  8.    p3=head2;

  9.    while(p2!=null)
  10.    {
  11.       if(p2->m < p3->m)
  12.       {
  13.          p1 = p2 ;
  14.          p2=p2->next;
  15.       }
  16.       else if (p2->m > p3->m)
  17.       {
  18.          if( p2 == head1 )
  19.         {
  20.                  head1 = p3 ;
  21.                  p3 = p3->next ;
  22.                  head1->next = p2 ;
  23.         }
  24.         else
  25.         {
  26.                 p1->next = p3 ;
  27.                 p3 = p3->next ;
  28.                 p1 = p1->next ;
  29.                 p1->next = p2 ;
  30.         }
  31.       }
  32.       else
  33.       {
  34.          p2->n=p2->n+p3->n;
  35.          p3=p3->next;
  36.       }

  37.           if( p3 == null )
  38.           {
  39.                   return head1 ;
  40.           }
  41.    }

  42.    p1->next=p3;
  43.    return head1 ;
  44. }
复制代码

因为可能会修改head1,所以main中调用add要
head1 = add( head1, head2 ) ;
这个add有个很大的缺点,就是head1和head2必须是按指数非递减排序。
因此create也要进行相应修改。

请多多指教
 楼主| 发表于 2004-3-16 21:47:42 | 显示全部楼层
这么多人回帖,真是感动!!!!问题贼各位高手的提示下解决了。是add函数的链表指针断了。再次感谢帮助我的人。!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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