栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数据结构与算法(Java)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构与算法(Java)

数据结构与算法(Java) 一、稀疏数组
public class SparseArr {
    public static void main(String[] args) {
        // 1.定义一个数组
        int[][] arr = new int[11][11];
        // 2.存放数值(1.为白子;2为黑子;0为空;)
        arr[1][1] = 1;
        arr[2][2] = 1;
        arr[3][3] = 2;
        arr[4][4] = 2;
        arr[6][6] = 2;
        getSparseArr(arr);
    }

    
    public static void getSparseArr(int[][] arr) {
        // 行
        int row = arr.length;
        // 列
        int col = arr[0].length;
        // 记录有效数值
        int sum = 0;

        // 1,循环得到二维数组的一共有几个不为零的值(或者最多的值以外的值)【初学者注意数组越界问题】
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (arr[i][j] != 0) {
                    // 有效数字加一
                    sum++;
                }
            }
        }
        System.out.println("有效数据一共有:" + sum);
        // 定义一个稀疏数组
        int[][] sparseArr = new int[sum + 1][3];
        // 二维数组的行
        sparseArr[0][0] = row;
        // 二维数组的列
        sparseArr[0][1] = col;
        // 二维数组的有效值数量
        sparseArr[0][2] = sum;
        // 从稀疏数组的第二行开始存放数据,定义一个开始的行号
        int count = 1;
        // 循环数组将有效值存放到稀疏数组中
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (arr[i][j] != 0) {
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = arr[i][j];
                    count++;
                }
            }
        }
        System.out.println("稀疏数组如下:");
        for (int i = 0; i < sum + 1; i++) {
            for (int j = 0; j < 3; j++) {
                // 不换行
                System.out.print(sparseArr[i][j] + "t");
            }
            // 换行
            System.out.println();
        }


        // 二:将稀疏数组逆向回二维数组
        // 1.原数组的行列,构建原数组
        int a = sparseArr[0][0];
        int b = sparseArr[0][1];

        int[][] ints = new int[a][b];
        // 循环稀疏数组
        for (int i = 1; i < sparseArr.length; i++) {
            ints[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }
        System.out.println("原有数组为:");
        for (int[] anInt : ints) {
            for (int i : anInt) {
                System.out.print(i + "t");
            }
            System.out.println();
        }

    }
}
二、队列 1、普通队列
public class QueueNormal {
    
    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue(5);
        myQueue.push(3);
        myQueue.push(4);
        myQueue.push(5);
        myQueue.push(6);
        myQueue.push(7);
//        myQueue.push(7);
//        myQueue.push(7);
        myQueue.printOut();
        System.out.println();
        for (int i = 0; i < 1000; i++) {
            myQueue.pull();
        }
    }

    // 总:先入后出
    // 1.构建一个队列容器
    // 2.入队列
    // 2.1判断队列是否满?不能入:入队列
    // 3.出队列
    // 3.1判断队列是否为空?无:出
    static class MyQueue {
        // 1.队列最大长度
        private int maxSize;
        // 2.头指针
        private int front;
        // 3.尾指针
        private int rear;
        // 4.容器
        private int[] queue;

        
        public MyQueue(int maxSize) {
            this.maxSize = maxSize;
            this.queue = new int[maxSize];
            this.front = -1;
            this.rear = -1;
        }

        
        public boolean isFull() {
            return rear == maxSize - 1;
        }

        
        public boolean isEmpty() {
            return front == rear;
        }

        
        public boolean push(int num) {
            if (isFull()) {
                throw new RuntimeException("队列已满");
            }
            queue[++rear] = num;
            return true;
        }

        
        public int pull() {
            if (isEmpty()) {
                throw new RuntimeException("队列是空的");
            }
            int num = queue[rear];
            rear--;
            System.out.println(num);
            return num;
        }

        public void printOut() {
            for (int i = 0; i < queue.length; i++) {
                System.out.println(queue[i]);
            }
        }
    }
}

-----更新2022.03.06

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/754302.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号