noj数据结构稀疏矩阵的加法十字链表_数据结构之十字链表——稀疏矩阵的链式存储及加法运算...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

以下简单实现了数据结构中的十字链表(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");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值