帮我看看这个链表,总是显示内存错误
想做一个一元多项式的加减乘,以链表方式存储,但是编译成功,运行时总是显示内存错误
帮忙看看,谢谢。
#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




