算法与数据结构学习3-队列(1)

1.队列介绍

  • 队列是一个有序列表,可以用数组或者是链表来实现
  • 队列遵循先进先出的原则。即:先存入队列的数据要先取出,后存入的要后取出
  • 队列在加数据的时候再尾部加,取数据的时候再头部取。

2.数组模拟队列

  • 队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如下图,其中maxSize是最大容量。
  • 因为度列的输出和输入分别要从队列的头部和尾部来处理,因此需要两个变量font和rear来记录队列的头部尾部下标,font会随着数据的输出该表,而rear会随着数据的输入而改变。
  • 如图所示:
    -在这里插入图片描述
    当数据存入队列时称为“addQueue”,addQueue的处理需要有两个步骤:
    思路分析:
    1):将尾指针往后移:rear+1,判断是否front == rear【队列为空】
    2):若位置正rear小于队列的最大下标maxSize-1,则将数据存入rear所指的数组元素中,否则无法存入数据。rear=maxSize-1【队列满】

3.代码演示

import java.util.Scanner;

	public class ArrayQueue {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//测试
		//创建一个队列
		
		ArrayQueue1 aq= new ArrayQueue1(4);
		char key = ' ';//接收用户输入
		Scanner scanner = new Scanner(System.in);
		boolean loop = true;
		//输出一个菜单
		while(loop) {
			System.out.println("s(show):显示队列");
			System.out.println("e(exie):退出程序");
			System.out.println("a(add):添加数据到队列");
			System.out.println("g(get):从队列取出程序");
			System.out.println("h(head):查看队列头部数据");
			
			key = scanner.next().charAt(0);//接收一个字符
			switch(key){
			case 's':
				aq.showQueue();
				break;
			case 'a':
				System.out.println("请输入一个数字:");
				int value = scanner.nextInt();
				aq.addQueue(value);
				break;
			case 'g'://取出数据
				try {
					int res =aq.getQueue();
					System.out.printf("取出的数据为:%d\n",res);
				} catch (Exception e) {
					// TODO: handle exception
					System.out.printf(e.getMessage());
				}
				break;
			case 'h':
				try {
					int res = aq.headQueue();
					System.out.printf("队列头的数据为:%d\n",res);
				} catch (Exception e) {
					// TODO: handle exception
					System.out.printf(e.getMessage());
				}
				break;
			case 'e':
				scanner.close();
				loop=false;
				break;
				default:
					break;
			}
			
		}
		System.out.println("程序退出");

	}

	}

	//使用数组模拟队列 - 编写一个ArrayQueue类
	class ArrayQueue1 {
	
	private int maxSize;//表示数组的最大容量
	private int front;//队列头
	private int rear;//队列尾
	private int[] arr;//该数组用于存放数据,模拟队列
	
	//创建队列的构造器
	public ArrayQueue1(int arrMaxSize) {
		maxSize= arrMaxSize;
		arr = new int [maxSize];
		front = -1;//指向队列头部,分析出front是指向队列头的前一个位置
		rear = -1;//指向队列的尾部,指向队列的尾部(本身就是队列尾部)
		
	}
	
	//判断队列是否满
	public boolean isFull() {
		return rear == front-1;
	}
	
	//判断队列是否为空
	public boolean isEmpty() {
		return rear == front;
	}
	
	//添加数据到队列
	public void addQueue(int n ) {
		//判断队列是否满
		if(isFull()) {
			System.out.println("队列满,不能加入数据");
			return ;
		}
		rear ++;//让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 ;
		}
		for(int i =0;i<arr.length;i++) {
			System.out.printf("arr[%d]=%d\n",i,arr[i]);
			System.out.println();
		}
	}
	
	//显示队列的头部数据是多少,注意不是取出数据
	public int headQueue() {
		//判断是否为空
		if(isEmpty()) {
			throw new RuntimeException("队列空,没有数据!");
		}
		return arr[front+1];	
	}
	}

在这里插入图片描述
在这里插入图片描述

发布了43 篇原创文章 · 获赞 45 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/mzc_love/article/details/104281843