JAVA数据结构与算法之数组模拟队列

  • 前言:
    数据结构是计算机存储、组织数据的方式。数据结构是指相互之间存在一种或多种特定关系的数据元素的集合。通常情况下,精心选择的数据结构可以带来更高的运行或者存储效率

目录

  • 队列
    • 队列简介:
    • 队列的存储结构:
    • 队列基本操作:
    • 代码实现以及思路:
    • 完整代码:

队列

队列简介:

  • 队列属于一种线性存储结构,是一个特殊的线性表
  • 队列是一种先入先出 FIFO的线性表

队列的存储结构:

  • 顺序存储:
    利用一组连续的地质单元存放队列元素(通常使用数组
    JAVA数据结构与算法之数组模拟队列_第1张图片

(注:因数组模拟队列,为了方便起见出入队只将两个指针进行移动)

  • 链式存储:

后续更新

队列基本操作:

JAVA数据结构与算法之数组模拟队列_第2张图片

  • 入队(增): 将数据放入队尾(rear),并将队尾指针后移一位
  • 出队(删):队首(front) 数据取出,并将队首指针后移一位
  • 查询队首元素(查): 返回队首元素的值,并不移动指针
  • 判断队列是否为空: 判断队列中是否含有数据
  • 判断队列是否已满: 判断队列是否满或者 假溢出1

为防止假溢出的出现也为了重复利用数组资源所以我们通常采用环形队列,详情请见另一篇文章——数组模拟环形队列

代码实现以及思路:

  • 准备工作:
  • 对程序中用到的变量做出如下约定:
  1. front:指向队列头前一个元素
  2. rear:指向队尾
  3. 两个指针初始值均为-1
	private int maxSize; // 最大容量
	private int front; // 指向队列头前一个元素
	private int rear; // 指向队列尾
	private int[] data; // 核心数组

	public ArrayQueue(int maxSize) {
		this.maxSize = maxSize;
		this.data = new int[maxSize];
		this.front = -1;
		this.rear = -1;
	}
  • 判空:
  1. 本身未插入数据,即 rear=front=-1
  2. 插入数据后又将数据全部取出,已经取走队尾的数据,即 rear==front
	/**
	 * 
	 * 队列是否为空
	 * 
* * @return */
public boolean isEmpty() { return rear == front; }
  • 判满:

判断队尾指针是否已经到达顺序表最后一位

	/**
	 * 
	 * 队列是否为空
	 * 
* * @return */
public boolean isEmpty() { return rear == front; }
  • 入队(增):
  1. 先判断队列是否已满
  2. 将数据写在当前队尾的后一个位置
    (1)先将队尾指针后移
    (2)将数据写入队尾指针所在位置
	/**
	 * 
	 * 向队列中增加数据
	 * 
	 * 
	 * 
	 * @param n 插入数据
	 * @see ArrayQueue#isFull()
	 */
	public void add(int n) {
		// 判断队列是否已满
		if (isFull())
			throw new ArrayIndexOutOfBoundsException("队列已满");
		/**
		 * 等同于rear++; data[rear]=n;
		 */
		data[++rear] = n;
	}
  • 出队(删):
  1. 先判断队列是否为空
  2. 取出队首位置元素,并将队首指针后移
    (1)因我们约定front指向队首前一个元素,先将front后移
    (2)再取出front位置的元素
	/**
	 * 
	 * 取数据
	 * 
* * @return * @see ArrayQueue#isEmpty() */
public int get() { if (isEmpty()) throw new RuntimeException("队列空"); /** * 等同于front++; return data[front]; */ return data[++front]; }
  • 查询队首元素:

思路与出队基本相同,但是不将队首指针后移
因此只需取出front+1位置上的元素

	/**
	 * 
	 * 读取队首数据
	 * 
* * @return * @see ArrayQueue#isEmpty() */
public int read() { if (isEmpty()) throw new RuntimeException("队列空"); /** * 等同于front=front+1; return data[front]; */ return data[front + 1]; }
  • 打印队列:
  1. 首先判断队列是否为空
  2. 遍历核心数组
    但是因为在做出队时只是将front后移,并没有真正的删除
    因此在遍历时,应从front+1的位置开始,到rear的位置结束
	/**
	 * 打印数据
	 * 
	 * @see ArrayQueue#isEmpty()
	 */
	public void display() {
		if (isEmpty())
			throw new RuntimeException("队列空");
		for (int i = front + 1; i <= rear; i++) {
			System.out.print(data[i] + " ");
		}
		System.out.println();
	}

完整代码:

package DataStructures.linear.queue;

/**
 * 
 * 
 * 数组实现队列
 * 
* * @param int maxSize->最大容量 * @param int front->指向队列头前一个元素 * @param int rear->指向队列尾 * @param int[] data->核心数组 * * @author Lansion * @version 1.0 * @since 1.0 */
public class ArrayQueue { private int maxSize; // 最大容量 private int front; // 头 private int rear; // 尾 private int[] data; // 核心数组 public ArrayQueue(int maxSize) { this.maxSize = maxSize; this.data = new int[maxSize]; this.front = -1; this.rear = -1; } /** *
	 * 队列是否已满
	 * 
* * @return */
public boolean isFull() { return rear == maxSize - 1; } /** *
	 * 队列是否为空
	 * 
* * @return */
public boolean isEmpty() { return rear == front; } /** *
	 * 向队列中增加数据
	 * 
	 * 
	 * 
	 * @param n 插入数据
	 * @see ArrayQueue#isFull()
	 */
	public void add(int n) {
		// 判断队列是否已满
		if (isFull())
			throw new ArrayIndexOutOfBoundsException("队列已满");
		/**
		 * 等同于rear++; data[rear]=n;
		 */
		data[++rear] = n;
	}

	/**
	 * 
	 * 取数据
	 * 
* * @return * @see ArrayQueue#isEmpty() */
public int get() { if (isEmpty()) throw new RuntimeException("队列空"); /** * 等同于front++; return data[front]; */ return data[++front]; } /** *
	 * 读取队首数据
	 * 
* * @return * @see ArrayQueue#isEmpty() */
public int read() { if (isEmpty()) throw new RuntimeException("队列空"); /** * 等同于front=front+1; return data[front]; */ return data[front + 1]; } /** * 打印数据 * * @see ArrayQueue#isEmpty() */ public void display() { if (isEmpty()) throw new RuntimeException("队列空"); for (int i = front + 1; i <= rear; i++) { System.out.print(data[i] + " "); } System.out.println(); } }

  1. 是指队尾指针已经指向最后一个存储位置,但前面仍有空闲位置,这时程序已经认为队列已满,其实还有空间,防止假溢出最简单的方式就是环形队列。 ↩︎

你可能感兴趣的:(java数据结构与算法)