01稀疏数组与队列

稀疏数组和队列

1、稀疏数组 sparsearray

当一个数组大部分元素为0,或者为同一个值时,可以使用稀疏数组来保存该数组。

稀疏数组的处理方法:

  1. 记录数组有几行几列,有多少个不同的值
  2. 把具有不同值元素的行、列、值记录在一个小规模数组中,从而缩小程序规模
    在这里插入图片描述

二维数组 -> 稀疏数组:

  1. 遍历二维数组,得到有效数据个数sum
  2. 创建稀疏数组sparaseArr int[sum+1][3]
  3. 将有效数据存入稀疏数组

稀疏数组 -> 二维数组:

  1. 读取稀疏数组的第一行,创建二维数组
  2. 读取稀疏数组后几行的数据,赋值给二维数组
2、队列 queue

队列是一个有序列表,可以使用数组链表来实现。

  • 遵循先入先出原则。
2.1 数组模拟队列:
package msp.cai.queue;

import java.util.Arrays;

/*
 *  数组模拟队列
 */
public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(5);
        arrayQueue.addQueue(1);
        arrayQueue.addQueue(2);
        arrayQueue.addQueue(3);
        arrayQueue.addQueue(4);
        arrayQueue.showQueue();
        System.out.println(arrayQueue.getQueue());
        System.out.println(arrayQueue.getQueue());
        System.out.println(arrayQueue.getQueue());
        System.out.println(arrayQueue.getQueue());
    }
}

class ArrayQueue {
    private int maxSize;    // 数组最大容量
    private int front;  // 队列头
    private int rear;   // 队列尾
    private int[] arr;  // 用于存放数据的数组

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

    // 判断队列是否满
    public boolean isFull(){
        return rear == maxSize-1;
    }

    // 判断队列是否为空
    public boolean isEmpty(){
        return rear == front;
    }

    // 添加数据
    public void addQueue(int n){
        if (isFull()){
            System.out.println("队列已满...");
            return;
        }
        rear++;
        arr[rear] = n;
    }

    // 取数据
    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空...");
        }
        front++;
        return arr[front];
    }

    // 打印队列
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列为空...");
            return;
        }
        System.out.println("队列数据为:");
        System.out.println(Arrays.toString(arr));
    }
}

2.2 数组实现环形队列:
package msp.cai.queue;

import java.util.Arrays;

/*
 * 数组模拟环形队列
 */
public class ArrayQueueDemo02 {
    public static void main(String[] args) {
        ArrCycleQueue cycleQueue = new ArrCycleQueue(4);

        cycleQueue.addQueue(1);
        cycleQueue.addQueue(2);
        cycleQueue.addQueue(3);
        cycleQueue.showQueue();
        System.out.println("========");
        System.out.println(cycleQueue.getQueue());
        System.out.println(cycleQueue.getQueue());
        System.out.println(cycleQueue.getQueue());
        cycleQueue.showQueue();
        System.out.println("========");
        cycleQueue.addQueue(4);
        System.out.println(cycleQueue.getQueue());
    }
}

class ArrCycleQueue {
    private int maxSize;    // 数组最大容量
    private int front;  // 队列头
    private int rear;   // 队列尾
    private int[] arr;  // 用于存放数据的数组

    public ArrCycleQueue(int maxSize) {
        this.maxSize = maxSize;
        this.arr = new int[this.maxSize];
        this.front = 0;
        this.rear = 0;
    }

    // 判断队列是否满
    public boolean isFull(){
        return ((rear+1) % maxSize) == front;
    }

    // 判断队列是否为空
    public boolean isEmpty(){
        return rear == front;
    }

    // 添加数据
    public void addQueue(int n){
        if (isFull()){
            System.out.println("队列已满...");
            return;
        }
        arr[rear] = n;
        rear = (rear+1) % maxSize;
    }

    // 取数据
    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空...");
        }
        int value = arr[front];
        front = (front+1) % maxSize;
        return value;
    }

    // 打印队列
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列为空...");
            return;
        }
        System.out.println("队列数据为:");
        for (int i = front; i < front+((rear-front)%maxSize); i++) {
            System.out.printf("%d\t", arr[i]);
        }
        System.out.println();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值