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

数据结构-------数组模拟队列、环形队列

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

数据结构-------数组模拟队列、环形队列

队列 1、队列介绍

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

遵循先入先出的原则,即:先存入队列的数据,要 先取出。后存入的数据要后取出。

示意图

1.1、数组模拟队列

如上图所示:

maxSize表示该队列的最大容量

两个变量front和rear分别记录队列前后端的下标,front会随着数据的输出而改变,rear会随着数据的输入而改变

package com.queue;

import java.util.Scanner;

import javax.xml.transform.Source;


public class ArrayQueueDemo {

	public static void main(String[] args) {
		ArrayQueue queue = new ArrayQueue(3);
		char key = ' ';//接受用户输入
		Scanner sc = new Scanner(System.in);
		boolean loop = true;
		while(loop){
			System.out.println("s(show):显示队列");
			System.out.println("e(exit):退出程序");
			System.out.println("a(add):添加数据到队列");
			System.out.println("g(get):从队列取出数据");
			System.out.println("h(head):查看队列头的数据");
			key = sc.next().charAt(0);//接收一个字符
			switch (key) {
			case 's':
				queue.showQueue();
				break;
			case 'a':
				System.out.println("输出一个数");
				int value = sc.nextInt();
				queue.addQueue(value);
				break;
			case 'g'://取出数据
				try {
					int res = queue.getQueue();
					System.out.printf("取出的数据是%dn",res);
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e.getMessage());
				}
				break;
			case 'h'://查看队列头的数据
				try {
					int head = queue.headQueue();
					System.out.printf("队列头的数据是%dn",head);
				} catch (Exception e) {
					// TODO: handle exception 
					System.out.println(e.getMessage());
				}
				break;
			case 'e':
				sc.close();
				loop = false;
				break;
			}
		}
		System.out.println("程序退出");
	}
}

//使用数组模拟队列-编写一个ArrayQueue类
class ArrayQueue{
	private int maxSize;  //定义数组的最大容量
	private int front,rear;  //定义前后端两个下标
	private int queueArr[];  //数组用来存放数列
	
	//创建队列的构造器
	public ArrayQueue(int arrMaxSize) {
		maxSize = arrMaxSize;
		queueArr = new int[maxSize];
		front=-1;//指向队列头的前一个位置
		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++;
		queueArr[rear] = n;
	}
	
	//取出队列的数据
	public int getQueue() {
		//判断是否为空
		if (isEmpty()) {
			throw new RuntimeException("队列空,不能取出数据");
		}
		front++;
		return queueArr[front];		
	}
	
	//显示队列的所有数据
	public void showQueue() {
		if (isEmpty()) {
			System.out.println("队列为空,没有数据~");
			return;
		}
		for (int i = 0; i < queueArr.length; i++) {
			System.out.printf("queueArr[%d]=%dn",i,queueArr[i]);
		}
	}
	//显示队列头数据,注意不是取数据
	public int headQueue() {
		if (isEmpty()) {
			throw new RuntimeException("对列是空的,没有数据~");
		}
		return queueArr[front+1];
	}
	
}

数组模拟队列会出现问题,当队列加入数据然后再取出数据,导致front与rear的值相等后,结果会导致不能再次加入数据,数组使用一次就不能复用了。用数组模拟环形队列解决上述问题

1.2、数组模拟环形队列

队列满的条件是(rear+1)%maxSize == front (如下图所示)

当rear>front时,此时队列的长度为rear-front(如下图所示)

当rear

合在一起就是rear-front+maxSize(如下图所示)

所以通用的计算队列长度公式为(rear-front+maxsize)%maxSize

 package com.queue;
 ​
 import java.util.Scanner;
 ​
 public class CircleArrayQueueDemo {
     public static void main(String[] args) {
         //测试
         System.out.println("测试数组模拟环形队列");
         CircleArray queue = new CircleArray(4);  //长度为4有效最大空间为3
         char key = ' ';//接受用户输入
         Scanner sc = new Scanner(System.in);
         boolean loop = true;
         while(loop){
             System.out.println("s(show):显示队列");
             System.out.println("e(exit):退出程序");
             System.out.println("a(add):添加数据到队列");
             System.out.println("g(get):从队列取出数据");
             System.out.println("h(head):查看队列头的数据");
             key = sc.next().charAt(0);//接收一个字符
             switch (key) {
             case 's':
                 queue.showQueue();
                 break;
             case 'a':
                 System.out.println("输出一个数");
                 int value = sc.nextInt();
                 queue.addQueue(value);
                 break;
             case 'g'://取出数据
                 try {
                     int res = queue.getQueue();
                     System.out.printf("取出的数据是%dn",res);
                 } catch (Exception e) {
                     // TODO: handle exception
                     System.out.println(e.getMessage());
                 }
                 break;
             case 'h'://查看队列头的数据
                 try {
                     int head = queue.headQueue();
                     System.out.printf("队列头的数据是%dn",head);
                 } catch (Exception e) {
                     // TODO: handle exception 
                     System.out.println(e.getMessage());
                 }
                 break;
             case 'e':
                 sc.close();
                 loop = false;
                 break;
             }
         }
         System.out.println("程序退出");
     }
 }
 ​
 ​
 class CircleArray{
     private int maxSize;  //表示数组的最大容量
     private int front;//队列头,指向第一个元素的位置
     private int rear;//队列尾,指向队列最后一个元素的后一个位置
     private int[] arr; //该数组用来存放数据,模拟队列
     
     public CircleArray (int arrMaxSize) {
         maxSize = arrMaxSize;
         arr = new int[maxSize];
         front = 0;
         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("队列为空,不能取出数据~");
         }
         //这里需要分析出front是指向对垒的第一个元素
         //1、先把front对应的值保留在一个临时白能量
         //2、将front后移,考虑取模
         //3、将临时保存的变量返回
         int value = arr[front];
         front = (front+1)%maxSize;
         return value;
     }
     //显示队列的所有数据
     public void showQueue() {
         if (isEmpty()) {
             System.out.println("队列为空,没有数据~");
             return;
         }
         
         //思路:从front开始遍历,遍历有效有效长度位数的数据
         for (int i = front; i  

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

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

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