CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

帮我看看这个链表,总是显示内存错误

楼主tygfr(Try to do!)2006-03-01 09:46:49 在 C/C++ / C++ 语言 提问

想做一个一元多项式的加减乘,以链表方式存储,但是编译成功,运行时总是显示内存错误  
  帮忙看看,谢谢。  
  #include   <iostream>  
  using   namespace   std;  
  struct   Multi_node  
  {  
  int   data1;                         //for   the   quotiety  
  int   data2;     //for   the   exponent  
  Multi_node   *next;  
  };                                
  struct   Multi_head  
  {  
  int   num;                             //the   count   of   the   multinomial    
  Multi_node   *next;  
  };//**********************************************************************************  
  Multi_head   *   INSERT(Multi_head   *head,int   m,int   n)       //insert   ,   use   in   bulids   and   counts  
  {  
  Multi_node   *p,*q;                 //P   locat   the   right   location  
  p=head->next;  
  q=new   Multi_node;                 //Q   point   to   the   new   node  
  q->data1=m;  
  q->data2=n;  
  q->next=NULL;                         //new   node's   initialization  
  if(p)                                         //if   this   chain   belt   is   not   null  
  {  
  while(p)  
  p=p->next;                
  p->next=q;                       //pointer   p   locat  
  }  
  else                                           //if   null  
  head->next=q;                 //if   this   is   the   first   node    
  return   head;  
  }//************************************************************************************  
  Multi_head   *   BUILD_NEW_TEMP(Multi_head   *   p)             //to   set   up   one   new   multinomial   ,   a   temple   function  
  {  
  cout<<endl<<"Please   input   some   couple   of   NUMBERS   ,   use   the   SPACE   to   separate   and   \"   0   \"   to   advance"<<endl;  
  int   a,b;                                     //temple   variable   for   inputing  
  do  
  {  
  a=b=0;  
  cin>>a;  
  if(a<=0)  
  return   p;  
  cin>>b;  
  p=INSERT(p,a,b);  
  ++p->num;  
  }while(1);  
  return   p;  
  }//************************************************************************************  
  void   BUILD_NEW_REAL()                     //to   set   up   a   couple   of   multiomial   ,   which   use   BUILD_NEW_TEMP   twice  
  {  
  Multi_head   *head1,*head2;  
  head1=new   Multi_head;  
  head2=new   Multi_head;  
  head1->next=head2->next=NULL;  
  head1->num=head2->num=0;                             //head   pointer   initialization  
  cout<<endl<<"Multiomial   A"<<endl;  
  head1=BUILD_NEW_TEMP(head1);  
  cout<<endl<<"Multiomial   B"<<endl;  
  head2=BUILD_NEW_TEMP(head2);  
  }//************************************************************************************  
  void   CONTANT()       //the   main   skin   of   this   program  
  {  
  int   n;  
  cout<<"Multinomial"<<endl<<endl<<"CONTANT"<<endl<<"1.BUILD   NEW   COUNTING"<<endl<<"2.BACK   TO   WINDOWS"<<endl;  
  cout<<"Please   select.....     ";  
  cin>>n;  
  switch(n)  
  {  
  case   1:   BUILD_NEW_REAL();  
  break;  
  case   2:   exit(0);  
  }  
  }//*************************************************************************************                                              
  void   main   ()  
  {  
  CONTANT();  
  }                                       //subtration 问题点数:100、回复次数:5Top

1 楼piaochen_2002(执子之手,与子偕老!)回复于 2006-03-01 10:10:30 得分 50

Multi_head   *   INSERT(Multi_head   *head,int   m,int   n)       //insert   ,   use   in   bulids   and   counts  
  {  
  Multi_node   *p,*q;                 //P   locat   the   right   location  
  p=head->next;  
  q=new   Multi_node;                 //Q   point   to   the   new   node  
  q->data1=m;  
  q->data2=n;  
  q->next=NULL;                         //new   node's   initialization  
  if(p)                                         //if   this   chain   belt   is   not   null  
  {  
  while(p->next)  
  p=p->next;                
  p->next=q;                       //pointer   p   locat  
  }  
  else                                           //if   null  
  head->next=q;                 //if   this   is   the   first   node    
  return   head;  
  }//************************************************************************************ 改成这样看看!Top

2 楼zw4u(zw4u)回复于 2006-03-01 10:16:29 得分 0

连表添加操作要用   引用   吧Top

3 楼ycy589(ycy589)回复于 2006-03-01 10:19:08 得分 0

顶Top

4 楼ugg(逸学堂(exuetang.net))回复于 2006-03-01 10:23:43 得分 50

1:Multi_head中  
  num使用不正确,在insert操作中并没有实现+1操作。  
   
  2:  
  a=b=0;  
  cin>>a;  
  if(a<=0)  
  return   p;  
  cin>>b;  
  p=INSERT(p,a,b);  
  ++p->num;//   这一句的操作是什么含义,是指针p++?lz是不是想写p->num++吧。或者是++(p->num)  
   
  3:  
  head1=new   Multi_head;  
  head2=new   Multi_head;  
  new   的对象都没有delete,  
  所以最后会内存泄漏。  
   
  看代码lz的功底应该还可以,所以上面三点修改后,应该没有什么问题。Top

5 楼dnliophsam()回复于 2006-03-01 11:03:17 得分 0

同意piaochen_2002的改动  
  主要问题应该是链表尾节点判别错误  
  p->next==NULL才表示到了链表末尾而不是p==NULLTop

相关问题

  • win2000内存只读错误?
  • 内存错误问题
  • 内存分配错误
  • help 内存错误.dll
  • 求救!vb程序编译出来以后 在别的机子运行显示 错误07内存不足!
  • 127.0.0.1 显示错误
  • c++ 内存分配错误 求解
  • 这个内存错误怪怪的!!!!!
  • 临界内存分配错误求解
  • 读内存错误:怎么解决呢?

关键词

  • null
  • multi
  • head
  • 链表
  • 错误
  • next
  • 看看
  • node
  • num
  • insert

得分解答快速导航

  • 帖主:tygfr
  • piaochen_2002
  • ugg

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo