标题:十字链表的遍历不出来啊,编译没出错,求教啊各位
取消只看楼主
lianjiecuowu
Rank: 3Rank: 3
来 自:安徽
等 级:论坛游侠
帖 子:152
专家分:107
注 册:2011-5-20
结帖率:85.71%
 问题点数:0 回复次数:0 
十字链表的遍历不出来啊,编译没出错,求教啊各位
#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:38 编辑 ]
搜索更多相关主题的帖子: class 十字 private include public 
2011-09-13 22:36



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-349855-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.168985 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved