为什么遍历不出元素啊,编译没出错啊,求教啊各位老师,提点就行啊,谢谢
#include<iostream>#include<cstdlib>
using namespace std;
class crosslist;
class snode
{
private:
friend class crosslist;
public:
int i;
int j;
int e;
snode *right;
snode *down;
};
typedef snode * snodepointer;
class crosslist
{
private:
snodepointer *rhead;
snodepointer *chead;
int row;
int col;
int num;
public:
crosslist();
~crosslist(){clear();}
void clear();
bool isempty();
int getcolnum();
int getrownum();
int getnum();
void creat_crosslist();
void treavlist();
};
crosslist::crosslist()
{
row=0;
col=0;
num=0;
rhead=NULL;
chead=NULL;
}
void crosslist::clear()
{
if(rhead)
delete []rhead;
if(chead)
delete []chead;
row=0;
col=0;
num=0;
rhead=NULL;
chead=NULL;
}
bool crosslist::isempty()
{
return num==0;
}
int crosslist::getcolnum()
{
return col;
}
int crosslist::getrownum()
{
return row;
}
int crosslist::getnum()
{
return num;
}
void crosslist::creat_crosslist()
{
int r=0,c=0,n=0,s;
row=r;col=c;num=n;
int k=0,ii,jj,ee;
snodepointer *p,q;
cout<<"请输入所创建的稀疏矩阵的--行数: ";
cin>>r;
cout<<"请输入所创建的稀疏矩阵的--列数: ";
cin>>c;
cout<<"请输入所创建的稀疏矩阵的--非零元素的个数: ";
cin>>n;
rhead=new snodepointer[r];
if(rhead==NULL)
{
cerr<<"memory allocation failare!"<<endl;
return ;
}
for(k=0;k<r;k++)
rhead[k]=NULL;
chead=new snodepointer[c];
if(chead==NULL)
{
cerr<<"memory allocation failare!"<<endl;
return ;
}
for(k=0;k<c;k++)
chead[k]=NULL;
if(n)
{
p=new snodepointer[c];
if(p==NULL)
{
cerr<<"memory allocation failare!"<<endl;
return ;
}
for(s=0;s<c;s++)
p[s]=chead[s]=NULL;
for(k=0;k<n;k++)
{
cout<<"请输入非零元素所处的行 :";
cin>>ii;
cout<<"请输入非零元素所处的列 :";
cin>>jj;
cout<<"请输入非零元素的值 :";
cin>>ee;
snode *newp=new snode;
if(newp==NULL)
{
cerr<<"memory allocation failare!"<<endl;
return ;
}
newp->i=ii;
newp->j=jj;
newp->e=ee;
newp->right=NULL;
newp->down=NULL;
if(rhead[newp->i-1]==NULL)
rhead[newp->i-1]=newp;
else
q->right=newp;
q=newp;
if(chead[newp->j-1]==NULL)
chead[newp->j-1]=newp;
else
{
p[newp->j-1]->down=newp;
p[newp->j-1]=newp;
}
}
delete []p;
}
}
void crosslist::treavlist()
{
for(int i=0;i<row;i++)
{
snodepointer p=rhead[i];
while(p)
{
cout<<p->e<<"\t";
p=p->right;
}
}
}
int main()
{
crosslist C;
C.creat_crosslist();
C.treavlist(); //为什么便利不出来啊非零元素
system("pause");
return 0;
}
[ 本帖最后由 lianjiecuowu 于 2011-9-13 22:39 编辑 ]