下面是程序,是十字链表的,应该可以运行,其中有我加入了输出 行列序 ,我遇到的问题是同一个结构体里的元素在指针传递的时候没有都传递,不知道什么原因,输出的col和item是对的,而row不对。谢谢各位了~
#include
using namespace std;
int main()
{
matrix M1;
}
enum boolean{FALSE,TRUE};
struct element{int row,col;float item;};
class matrix;
class node
{
friend class matrix;
node()
{
head=FALSE;
down=right=NULL;
}
node(boolean b,element *t);
private:
node *down,*right;
boolean head;
union
{
element triple;
node *next;
};
};
node::node(boolean b,element *t)
{
head=b;
if(b)
{
right=down=this;
}
else triple=*t;
}
//typedef node *nodeptr;
class matrix
{
private:
node *headnode;
public:
matrix();
~matrix();
};
matrix::matrix()
{
headnode=new node(TRUE,0);
headnode->triple.row=0;
headnode->triple.col=0;
headnode->triple.item=0;
int irow=1,icol=1,i=1;
int rows,cols,terms;
int iitem;
cout>rows>>cols;
cout>terms;
cout next;
coutiitem;
if(iitem)
{
p=new node();
before->next=p;
p->triple.row=irow;
p->triple.col=icol;
p->triple.item=iitem;
couttriple.itemtriple.rowtriple.coltriple.row==p->triple.row)
{
before->right=p;
couttriple.col==p->triple.col)
{
before->down=p;
coutnext;
p=p->next;
i++;
}
}
p=headnode->next;
for(i=0;itriple.itemdown;
}
}
matrix::~matrix()
{
node *d=headnode->next;
node *p=d;
delete []headnode;
for(;!d;)
{
p=d;
d=d->next;
delete []p;
}
}
|