该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
以下简单实现了数据结构中的十字链表(Java实现):代码为添加(add)方法及求和(sum)方法,其它方法后续有时间再添加,最下面有测试方法
CrossList交叉链表实现代码如下:
package com.shxt.tengfei.metadata;
public class CrossList {
private Element base = null; //0元素
private int rowSize = 0;
private int columnSize = 0;
private int size = 0;
public CrossList() {
base = new Element(0, 0, null, null, null);
}
public int size() {
return size;
}
public void add(int row, int column, T d) {
Element rowTemp = base;
if (row > rowSize) {
while (rowTemp.down != null) {
rowTemp = rowTemp.down;
}
int newEleSize = row - rowSize;
for (int i = 0; i < newEleSize; i++) {
rowSize++;
Element newRowBase = new Element(rowSize, 0, null, null, null);
rowTemp.down = newRowBase;
rowTemp = rowTemp.down;
}
} else {
for (int i = 0; i < row; i++) {
rowTemp = rowTemp.down;
}
}
Element columnTemp = base;
if (column > columnSize) {
while (columnTemp.right != null) {
columnTemp = columnTemp.right;
}
int newEleSize = column - columnSize;
for (int i = 0; i < newEleSize; i++) {
columnSize++;
Element newColumnBase = new Element(0, columnSize, null, null, null);
columnTemp.right = newColumnBase;
columnTemp = columnTemp.right;
}
} else {
for (int i = 0; i < column; i++) {
columnTemp = columnTemp.right;
}
}
Element newEle = new Element(row, column, d, null, null);
if (rowTemp.right == null) {
rowTemp.right = newEle;
} else {
Element rightEle = rowTemp.right;
while (true) {
if (newEle.column < rightEle.column) {
newEle.right = rightEle;
rowTemp.right = newEle;
break;
}
else if (rightEle.right == null) {
rightEle.right = newEle;
break;
}
else {
rowTemp = rightEle;
rightEle = rightEle.right;
}
}
}
if (columnTemp.down == null) {
columnTemp.down = newEle;
} else {
Element downEle = columnTemp.down;
while (true) {
if (newEle.row < downEle.row) {
newEle.down = downEle;
columnTemp.down = newEle;
break;
}
else if (downEle.down == null) {
downEle.down = newEle;
break;
}
else {
columnTemp = downEle;
downEle = downEle.down;
}
}
}
size++;
}
public int[][] toArray() {
Element tempRowBase = base;
int[][] elements = new int[rowSize][columnSize];
while (tempRowBase.down != null) {
tempRowBase = tempRowBase.down;
Element tempColumnBase = tempRowBase;
while (tempColumnBase.right != null) {
tempColumnBase = tempColumnBase.right;
String strValue = tempColumnBase.data == null ? "0" : "" + tempColumnBase.data;
int value = Integer.parseInt(strValue);
elements[tempColumnBase.row - 1][tempColumnBase.column - 1] = value;
}
}
return elements;
}
public void print(int[][] array) {
for (int[] tempRow : array) {
for (int temp : tempRow) {
System.out.print(temp + "\t");
}
System.out.println();
}
}
public CrossList sum(CrossList second) {
int[][] fa = this.toArray();
int[][] sa = second.toArray();
CrossList overList = new CrossList();
boolean bCross = false;
if (fa.length == sa.length) {
for (int i = 0; i < fa.length; i++) {
if (fa[i].length != sa[i].length) {
break;
}
for (int j = 0; j < fa[i].length; j++) {
fa[i][j] = fa[i][j] + sa[i][j];
overList.add(i+1, j+1, fa[i][j]);
}
}
bCross = true;
}
return bCross ? overList : null;
}
class Element {
int row;
int column;
D data;
Element down;
Element right;
public Element(int r, int c, D d, Element down, Element right) {
row = r;
column = c;
data = d;
this.down = down;
this.right = right;
}
public String toString() {
return "[" + row + "," + column + " --> " + data + "]";
}
}
}
》》》》》》》》》》》》》》》》》》》》》》》》》》测试代码如下:
package com.shxt.tengfei.metadata;
public class TestCross {
public static void main(String[] args) {
CrossList cross1 = new CrossList();
//check new base
cross1.add(1, 1, -88);
cross1.add(3, 3, 99);
//insert a element (不影响其他元素)
cross1.add(2, 2, 1000);
//影响其他位置元素
cross1.add(2, 3, -5);
cross1.add(2, 1, 777);
cross1.print(cross1.toArray());
System.out.println("====================== cross1 end ========================\n");
CrossList cross2 = new CrossList();
cross2.add(1, 1, -66);
cross2.add(2, 2, 500);
cross2.add(3, 3, 35);
cross2.print(cross2.toArray());
System.out.println("====================== cross2 end ========================\n");
//cross1和cross2求和
CrossList cross3 = cross1.sum(cross2);
if (cross3 != null) {
cross3.print(cross3.toArray());
}
System.out.println("====================== sum(+) end ========================\n");
}
}