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實現(xiàn))
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門笼平,熙熙樓的掌柜王于貴愁眉苦臉地迎上來园骆,“玉大人,你說我怎么就攤上這事寓调⌒客伲” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵夺英,是天一觀的道長晌涕。 經(jīng)常有香客問我,道長痛悯,這世上最難降的妖魔是什么余黎? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮载萌,結(jié)果婚禮上惧财,老公的妹妹穿的比我還像新娘。我一直安慰自己扭仁,他們只是感情好垮衷,可當我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著乖坠,像睡著了一般搀突。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上熊泵,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼悬秉!你這毒婦竟也來了澄步?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布冤议,位于F島的核電站斟薇,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏恕酸。R本人自食惡果不足惜堪滨,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蕊温。 院中可真熱鬧椿猎,春花似錦、人聲如沸寿弱。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽症革。三九已至筐咧,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間噪矛,已是汗流浹背量蕊。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 鏈表一種在物理存儲單元上非連續(xù)脉漏、非順序的一種存儲結(jié)構(gòu)苞冯,元素通過鏈表中的指針鏈接次序?qū)崿F(xiàn)。鏈表是由節(jié)點組成侧巨,每一個節(jié)...
- 鏈結(jié)點:在鏈表中,每個數(shù)據(jù)項都被包含在"鏈結(jié)點"(Link)中.一個鏈結(jié)點是某個類的對象,這個類可以叫Link.而...
- 本章內(nèi)容主要來自算法(第四版) Robert Sedgewick著 1.3小節(jié) 棧(后進先出)的基本功能 隊列(先...
- 本文行文思路結(jié)構(gòu) 一. 線性表 部分定義和解釋見下面鏈接:http://blog.csdn.net/menglan...