首页 > 代码库 > 十字链表
十字链表
-
十字链表的语言描述
-
基本运算的算法——建立稀疏矩阵的十字链表的算法、输出稀疏矩阵十字链表的算法
?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147#include<iostream>
using
namespace
std;
template
<
class
T>
class
shizi
{
private
:
struct
Node
{
int
i;
int
j;
T data;
Node * right;
Node * down;
Node():right(NULL),down(NULL) {}
Node(
int
x,
int
y , T a)
{
i = x;
j = y;
data = http://www.mamicode.com/a;
right = NULL;
down = NULL;
}
};
Node *rhead;
Node *chead;
Node *rcur;
Node *ccur;
int
row,col,number;
public
:
shizi(
int
x,
int
y ,
int
num)
{
row = x;
Node * head1 =
new
Node();
rhead = head1;
rcur = head1;
for
(
int
i = 0 ; i < row ; i++)
//建立一列空的行节点
{
Node * p =
new
Node();
rcur->down = p;
rcur = rcur -> down;
}
col = y;
Node * head2 =
new
Node();
chead = head2;
ccur = head2;
for
(
int
i = 0 ; i < col ; i++)
//建立一行空的列节点
{
Node * p =
new
Node();
ccur -> right = p;
ccur = ccur -> right;
}
number = num;
}
int
returnx()
//返回x值
{
return
rcur->i;
}
int
returny()
//返回y值
{
return
rcur->j;
}
T returndata()
//返回data值
{
return
rcur->data;
}
void
charu()
//插入
{
for
(
int
i = 0 ; i < number ; i++)
{
cout<<
"please input x y data"
<<endl;
int
x1,y1;
T data1;
cin>>x1>>y1>>data1;
rcur = rhead;
ccur = chead;
//行---建立节点
Node * p =
new
Node(x1,y1,data1);
for
(
int
i = 0 ; i < x1 ; i++ )
//找到要插入的行
{
rcur = rcur -> down;
}
for
(
int
i = 0 ; i < y1 ; i++)
//找到要插入的列
{
if
(rcur -> right == NULL)
//如果列的元素为空
{
rcur -> right = p;
break
;
}
else
{
rcur = rcur -> right;
}
}
//列指针的连接
for
(
int
i = 0 ; i < y1 ; i++)
{
ccur = ccur -> right;
}
for
(
int
i = 0 ; i < x1 ; i++)
{
if
(ccur -> down == NULL )
{
ccur -> down = p;
break
;
}
else
{
ccur = ccur -> down;
}
}
}
}
void
print()
//打印
{ Node * tem = rhead;
rcur = rhead;
tem = tem->down;
rcur = rcur->down;
for
(
int
i = 0 ; i < row ; i++)
//按行打印,遍历一行所有元素,然后移到下一行
{
while
(rcur!=NULL)
{
if
(returnx()!=0&&returny()!=0)
cout<<returnx()<<
" "
<<returny()<<
" "
<<returndata()<<endl;
rcur = rcur -> right;
}
rcur = tem -> down;
tem = tem -> down;
}
}
};
int
main()
{
int
x,y,n;
cout<<
"please input row col number"
<<endl;
cin>>x>>y>>n;
shizi <
int
> dusk(x,y,n);
dusk.charu();
dusk.print();
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。