public class CrossLinkedMatrix {
private int col;
private int row;
private Node[] rowHead;
private Node[] colHead;
private class Node {
int x;
int y;
int elem;
Node right;
Node down;
public Node(int x, int y, int elem, Node right, Node down) {
this.x = x;
this.y = y;
this.elem = elem;
this.right = right;
this.down = down;
}
}
CrossLinkedMatrix compression(int[][] target) {
if (target == null) {
throw new UnsupportedOperationException("不支持空");
}
col = target[0].length;
row = target.length;
rowHead = new Node[row];
colHead = new Node[col];
for (int y = 0; y < row; y++) {
Node left = rowHead[y];
for (int x = 0; x < col; x++) {
if (target[y][x] != 0) {
Node node = new Node(x, y, target[y][x], null, null);
Node up = colHead[x];
if (up == null) {
colHead[x] = node;
} else {
while (up.down!=null) {
up = up.down;
}
up.down = node;
}
if (left == null) {
rowHead[y] = node;
} else {
left.right = node;
}
left = node;
}
}
}
return this;
}
CrossLinkedMatrix printColMatrix() {
if (rowHead == null) {
throw new UnsupportedOperationException("没有初始化");
}
for (int y = 0; y < row; y++) {
Node node = rowHead[y];
for (int x = 0; x < col; x++) {
if (node != null && node.x == x) {
System.out.print(node.elem + "\t");
node = node.right;
} else{
System.out.print("0\t");
}
}
System.out.println();
}
return this;
}
void printRowMatrix(){
if (colHead == null) {
throw new UnsupportedOperationException("没有初始化");
}
for (int x = 0; x < col; x++) {
Node node = colHead[x];
for (int y = 0; y < row; y++) {
if (node != null && node.y == y) {
System.out.print(node.elem + "\t");
node = node.down;
} else{
System.out.print("0\t");
}
}
System.out.println();
}
}
}
十字链表保存矩阵(Java实现)
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 链结点:在链表中,每个数据项都被包含在"链结点"(Link)中.一个链结点是某个类的对象,这个类可以叫Link.而...
- 本章内容主要来自算法(第四版) Robert Sedgewick著 1.3小节 栈(后进先出)的基本功能 队列(先...
- 本文行文思路结构 一. 线性表 部分定义和解释见下面链接:http://blog.csdn.net/menglan...