Data structures: queue

  A queue is an ordered list, it can be implemented in an array or list.

  Follow the FIFO principle. That is, the data stored in the first queue, first removed. After the deposit to be removed later.

 

  Queue itself is ordered list, by using the structure of the array to the data stored in the queue, the queue is declared as an array, which is the maximum capacity of the maxSize queue.

  Since the output queue, respectively from the rear end of the input is handled. Thus requires two variables are respectively front and rear ends of the front and rear queue index record. front will change as the output data, while the rear, along with the input data changes.

  

 

 Array queue

package com.queue;

import java.util.Scanner;

public class ArrayQueueDemo {
	public static void main(String[] args){
		
		ArrayQueue arrayQueue = new ArrayQueue(3);
		char key = ' '; // 接受用户输入
		Scanner scanner = 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 = scanner.next().charAt(0);
			switch(key){
				case 's':
					arrayQueue.showQueue();
					break;
				case 'a':
					System.out.println("请输入一个数:");
					int value = scanner.nextInt();
					arrayQueue.addQueue(value);
					break;
				case 'g': // 取出数据
					try{
						int res = arrayQueue.getQueue();
						System.out.printf("取出的数据是:%d\n",res);
					}catch(Exception e){
						System.out.println(e.getMessage());
					}
					break;
				case 'h': // 查看队列的头部
					try{
						int head = arrayQueue.headQueue();
						System.out.printf("队列的头部数据是:%d\n",head);
					}catch(Exception e){
						System.out.println(e.getMessage());
					}
					break;
				case 'e':
					scanner.close();
					loop = false;
					break;
				default:
					break;
			}
		}
		
		System.out.println("程序退出....");
		
	}
}

// 使用数组模拟队列, 编写一个ArrayQueue类
class ArrayQueue{
	private int maxSize; // 表示数组的最大容量
	private int front;   // 队列头
	private int rear;    // 队列尾
	private int[] arr;   // 该数组用于存取数据,模拟队列
	
	// 创建队列的构造器
	public ArrayQueue(int maxSize){
		this.maxSize = maxSize;
		arr = 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++; // 让rear后移
		arr[rear] = n;
	}
	
	// 出队列
	public int getQueue(){
		// 判断队列是否空
		if(isEmpty()){
			// 抛出异常
			throw new RuntimeException("队列为空,不能取数");
		}
		
		front++; // front后移
		return arr[front];
	}
	
	// 显示队列的所有数据
	public void showQueue(){
		if(isEmpty()){
			System.out.println("队列为空");
			return;
		}
		for(int i = 0; i<arr.length; i++){
			System.out.printf("%d\n", arr[i]);
		}
	}
	
	public int headQueue(){
		if(isEmpty()){
			throw new RuntimeException("队列为空,,,");
		}
		
		return arr[front+1];
	}
}

 

Optimization: Analog Array annular queue

   Front meaning of the variables to make an adjustment: front number points to the first element of the queue, that arr [front] number is the first element of the queue. front = 0

   Meaning rear variables to make an adjustment: rear point to a position after the last element of the queue. This empty space as a convention. rear = 0

   When the queue is full, with the proviso that (rear + 1)% maxSize == front

   When the queue is empty, provided that the rear == front

   The number of the queue: (rear + maxSize - front)% maxSize

package com.queue;

import java.util.Scanner;

public class CircleQueueArrayDemo {
	public static void main(String[] args){
		System.out.println("测试数组模拟环形队列的案例~~");
		
		CircleArray circleQueue = new CircleArray(4); // 设置maxSize=4, 那么队列的有效数据最大是3
		char key = ' '; // 接受用户输入
		Scanner scanner = 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 = scanner.next().charAt(0);
			switch(key){
				case 's':
					circleQueue.showQueue();
					break;
				case 'a':
					System.out.println("请输入一个数:");
					int value = scanner.nextInt();
					circleQueue.addQueue(value);
					break;
				case 'g': // 取出数据
					try{
						int res = circleQueue.getQueue();
						System.out.printf("取出的数据是:%d\n",res);
					}catch(Exception e){
						System.out.println(e.getMessage());
					}
					break;
				case 'h': // 查看队列的头部
					try{
						int head = circleQueue.headQueue();
						System.out.printf("队列的头部数据是:%d\n",head);
					}catch(Exception e){
						System.out.println(e.getMessage());
					}
					break;
				case 'e':
					scanner.close();
					loop = false;
					break;
				default:
					break;
			}
		}
		
		System.out.println("程序退出....");
	}
}

//使用数组模拟队列, 编写一个ArrayQueue类
class CircleArray{
	private int maxSize; // 表示数组的最大容量
	// front变量的含义做一个调整:front数指向队列的第一个元素,也就是说arr[front]数是队列的第一个元素。 front = 0
	private int front;
	// rear变量的含义做一个调整:rear指向队列的最后一个元素的后一个位置。这样可以空出一个空间做为约定。rear = 0
	private int rear;
	private int[] arr;   // 该数组用于存取数据,模拟队列
	
	public CircleArray(int maxSize){
		this.maxSize = maxSize;
		front = 0;
		rear = 0;
		arr = new int[maxSize];
	}
	
	// 队列是否已满
	public boolean isFull(){
		return (rear + 1)%maxSize == front;
	}
	
	// 队列是否为空
	public boolean isEmpty(){
		return front == rear;
	}
	
	// 添加数据到队列-- 入队列
	public void addQueue(int n){
		// 判断队列是否满
		if(isFull()){
			System.out.println("队列已满,不能再加入数据");
			return;
		}
		
		arr[rear] = n; // 直接将数据加入队列
		rear = (rear + 1)%maxSize; // 让rear后移,指向队列最后一个元素的下一个空间
	}
	
	// 出队列
	public int getQueue(){
		// 判断队列是否空
		if(isEmpty()){
			// 抛出异常
			throw new RuntimeException("队列为空,不能取数");
		}
		
		// 1.先把front对应的值保留到一个临时变量
		int res = arr[front];
		// 2.front后移
		front = (front + 1)%maxSize;
		// 3.将临时保存的变量值返回
		return res;
	}
	
	// 显示队列的所有数据
	public void showQueue(){
		if(isEmpty()){
			System.out.println("队列为空");
			return;
		}
		// (rear+maxSize-front)%maxSize 为队列元素的总个数
		for(int i = 0; i<(rear+maxSize-front)%maxSize; i++){
			System.out.printf("a[%d]=%d\n", (front+i)%maxSize,arr[(front+i)%maxSize]);
		}
	}
	
	// 显示队列的第一个元素
	public int headQueue(){
		if(isEmpty()){
			throw new RuntimeException("队列为空,,,");
		}
		
		return arr[front];
	}
}

 

Guess you like

Origin blog.csdn.net/m0_37564426/article/details/90921976