数据结构Java实现——④数组——>稀疏矩阵十字链表存储法

时间:2025-02-19 07:07:51
  • package org.;
  • import org.;
  • import org.;
  • import org.;
  • /**
  • * @author_Stone6762
  • */
  • public class CrossList {
  • /**
  • * @cols原始矩阵的列数
  • */
  • private int cols;
  • /**
  • * @rows原始矩阵的行数
  • */
  • private int rows;
  • /**
  • * @nums原始矩阵中非零元素的个数
  • */
  • private int nums;
  • /**
  • * @rhead列指针----单纯的充当头指针,执行该行第一个非零元素,所以其长度等于cols
  • */
  • private OLNode[] rhead;
  • /**
  • * @chead行指针----单纯的充当头指针,执行该列第一个非零元素,其长度等于rows
  • */
  • private OLNode[] chead;
  • public CrossList(int cols, int rows) {
  • inintHeader(cols, rows);
  • }
  • /**
  • * @构造器 
  • * @Description: TODO(将一个数组变成一个稀疏矩阵存储的形式)
  • * @param datas
  • */
  • public CrossList(double[][] datas) {
  • inintHeader(datas[0].length, );
  • for (int row = 0; row < ; row++) {
  • for (int col = 0; col < datas[0].length; col++) {
  • if (datas[row][col] != 0) {
  • insert(row, col, datas[row][col]);
  • }
  • }
  • }
  • }
  • /**
  • * @Description: TODO( 在该稀疏矩阵中插入一个元素 )
  • * @param row
  • * @param col
  • * @param data
  • */
  • public void insert(int row, int col, double data) {
  • this.nums++;
  • // 创建一个十字链表结点,并将数据存储进去
  • TripleNode da = new TripleNode(row, col, data);
  • OLNode newNode = new OLNode(da);
  • // 通过行列头指针,确定指向该新结点的指针
  • OLNode t = rhead[row];// 找到该行的头指针
  • while (() != null) {// 找到该行的末尾
  • t = ();
  • }
  • (newNode);// 让该行的末尾指向该新结点
  • //
  • t = chead[col];
  • while (() != null) {
  • t = ();
  • }
  • (newNode);
  • }
  • /**
  • * @Description: TODO( 通过行数和列数,初始化行列头指针 )
  • * @param cols列
  • * @param rows行
  • */
  • public void inintHeader(int cols, int rows) {
  • this.cols = cols;
  • this.rows = rows;
  • rhead = new OLNode[cols];
  • chead = new OLNode[rows];
  • // 初始化行的头指针
  • for (int i = 0; i < ; i++) {
  • rhead[i] = new OLNode();
  • }
  • // 设置列的头指针
  • for (int i = 0; i < ; i++) {
  • chead[i] = new OLNode();
  • }
  • }
  • /**
  • * @Description: TODO( 将十字存储的链表还原成原始矩阵 )
  • * @return
  • */
  • public double[][] reBackToArr() {
  • double arr[][] = new double[rows][cols];
  • for (int i = 0; i < ; i++) {
  • OLNode t = rhead[i];
  • while (t != null) {
  • if (() != null) {// 头指针数据为空
  • arr[().getRow()][().getColumn()] = t
  • .getData().getValue();
  • }
  • t = ();
  • }
  • }
  • return arr;
  • }
  • /**
  • * @Description: TODO( 遍历整个十字链表 )
  • */
  • public void printfArrOfRC() {
  • ("原始矩阵 共" + rows + "行, " + cols + "列, " + this.nums
  • + "个非零元素");
  • ("---------------------------------------");
  • ("从行上来看");
  • ("行号");
  • for (int i = 0; i < ; i++) {
  • (i + " ");
  • OLNode t = rhead[i];
  • while (t != null) {
  • if (() != null) {// 头指针数据为空
  • (().getValue() + "->");
  • }
  • t = ();
  • }
  • ();
  • }
  • ("---------------------------------------");
  • ("从列上来看");
  • ("列号");
  • for (int i = 0; i < ; i++) {
  • (i + " ");
  • OLNode t = chead[i];
  • while (t != null) {
  • if (() != null) {
  • (().getValue() + "->");
  • }
  • t = ();
  • }
  • ();
  • }
  • }
  • /**
  • * @Description: TODO( 以数组的形式打印)
  • */
  • public void printfArr() {
  • ("稀疏矩阵的三元组储存结构为: ");
  • ("行数" + rows + ", 列数为:" + cols + " ,非零元素个数为: "
  • + nums);
  • double arr[][] = reBackToArr();
  • (arr);
  • }
  • public CrossList() {
  • super();
  • }
  • public CrossList(int cols, int rows, int nums, OLNode[] rhead,
  • OLNode[] chead) {
  • super();
  • this.cols = cols;
  • this.rows = rows;
  • this.nums = nums;
  • this.rhead = rhead;
  • this.chead = chead;
  • }
  • public int getCols() {
  • return cols;
  • }
  • public void setCols(int cols) {
  • this.cols = cols;
  • }
  • public int getRows() {
  • return rows;
  • }
  • public void setRows(int rows) {
  • this.rows = rows;
  • }
  • public int getNums() {
  • return nums;
  • }
  • public void setNums(int nums) {
  • this.nums = nums;
  • }
  • public OLNode[] getRhead() {
  • return rhead;
  • }
  • public void setRhead(OLNode[] rhead) {
  • this.rhead = rhead;
  • }
  • public OLNode[] getChead() {
  • return chead;
  • }
  • public void setChead(OLNode[] chead) {
  • this.chead = chead;
  • }
  • public static void main(String[] args) {
  • double[][] arr = { { 0, 0, 1, 0 }, { 1, 0, 0, 4 }, { 0, 0, 3, 0 },
  • { 1, 2, 0, 4 } };
  • CrossList cList = new CrossList(arr);
  • ();
  • }
  • }