美文网首页
十字链表保存矩阵(Java实现)

十字链表保存矩阵(Java实现)

作者: thebigsilly | 来源:发表于2018-04-21 17:19 被阅读0次
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实现)

  • 链表

    单链表 C实现 Java实现 双链表 C实现 Java实现

  • 链表

    一、单向链表 单向链表的普通实现 Java实现: Kotlin实现: 单向链表的递归实现 Java实现: 二、双向...

  • 如何实现HashMap的顺序存储

    从Java API知道,LinkedHashMap继承于HashMap,并且通过双向链表保存各节点的位置信息,实现...

  • [矩阵] 十字链表

  • 2017.5.25

    lua学习总结:数据结构: 使用Lua实现链表(单向链表和双向链表),队列 使用Lua保存图,用table保存,每...

  • 5 图的复习目录

    5.1 图 5.2 图的存储结构 邻接矩阵 邻接表 十字链表 邻接多重链表 5.3 图的遍历 深度优先 广...

  • 基于动态数组的实现 Java实现 基于链表的栈的实现 Java实现

  • 队列

    基于数组的循环队列 Java实现 基于链表的队列实现 Java实现

  • 15反转链表

    题目描述 输入一个链表,反转链表后,输出新链表的表头。 Java实现

网友评论

      本文标题:十字链表保存矩阵(Java实现)

      本文链接:https://www.haomeiwen.com/subject/jhzxlftx.html