稀疏数组的链式存储实现(使用单向链表)

稀疏数组的链式存储实现(使用单向链表)

(1)链表类

public class LinkedList {
    //data域
    public int row;//用链表来存储原始数组中有效数据所在的行
    public int column;//用链表来存储原始数组中有效数据所在的列
    public int data;//用链表来存储原始数组中的有效数据值
    //模拟的指针域
    public LinkedList next;//模拟指针,next域存贮的类型应当还为LinkedList类型

    public LinkedList() {
    }

    public LinkedList(int row, int column, int data) {
        this.row = row;
        this.column = column;
        this.data = data;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "LinkedList{" +
                "row=" + row +
                ", column=" + column +
                ", data=" + data +
                '}';
    }
}

(2)业务实现

public class LinkedListSparseArray {

    //定义一个头结点,head结点不存储原始数组中的数据
    private LinkedList head = new LinkedList(0,0,0);

    //创建原始数组
    public int[][] createArray(){
        //1.模拟出来棋盘数据,使用二维数组
        int[][] array = new int[11][11];
        array[1][1] = 1;
        array[2][2] = 3;
        array[3][3] = 9;
        array[9][9] = 81;

        //打印棋盘查看效果
        //for(ElementType element: arrayName){};
        for (int[] row : array) {
            for (int val : row) {
                System.out.printf("%d\t", val);
            }
            System.out.println();//分行
        }

        //1.计算有效数据个数,
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (array[i][j] != 0) {
                    sum++;
                }
            }
        }
        System.out.println("有效数据个数:" + sum);
        return array;
    }

    //把原始数组转换到链表中
    public void arrayTransferLinkedList(int[][] paramArray,LinkedList linkedList){

        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (paramArray[i][j] != 0) {
                    linkedList.setRow(i);
                    linkedList.setColumn(j);
                    linkedList.setData(paramArray[i][j]);

                    LinkedList temp = head;
                    while (true){
                        if(temp.next == null){
                            break;
                        }
                        //temp.next不为空,就把temp.next赋值给下一个结点temp
                        temp = temp.next;
                    }
                    temp.next = linkedList;
                    linkedList = new LinkedList();
                }
            }
        }
    }

    //查看链表中的每一个元素
    public void showNode() {
        if (head.next == null) {
            System.out.println("这是空链表,查看所有元素完毕");
//            return;
        }

        LinkedList temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            } else {
                System.out.println(temp);
            }
            temp = temp.next;
        }
    }
}

(3)测试

public class TestLinkedListSparseArray {
    public static void main(String[] args) {
        LinkedListSparseArray linkedListSparseArray = new LinkedListSparseArray();
        LinkedList LinkedListParam = new LinkedList();
        int[][] array = linkedListSparseArray.createArray();
        linkedListSparseArray.arrayTransferLinkedList(array,LinkedListParam);
        linkedListSparseArray.showNode();
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值