队列的概念及实现
-
队列是一个有序列表,可以用数组或链表来实现。遵循先进先出原则,先入队的数据,优先取出。队列的应用场景如银行排队等。
-
使用数组模拟队列
import java.util.Scanner;
public class MyArrayQueue {
//容量
private int capacity;
//队头
private int front;
//队尾
private int rear;
//存放队列的数据
private int[] arr;
MyArrayQueue(int capacity){
this.capacity = capacity;
this.arr = new int[capacity];
this.front = -1; //指向队头的前一个位置
this.rear = -1; //指向队尾
}
boolean isFull() {
return rear == capacity - 1;
}
boolean isEmpty() {
return rear == front;
}
/**
* 从队尾入队
* @param data 数据
*/
void addQueue(int data) {
if (isFull()) {
System.out.println("队列满,不能加入数据");
return;
}
arr[++rear] = data;
}
/**
* 从队头出队
*/
int getData() {
if (isEmpty()) {
throw new RuntimeException("队列空,不能取数据");
}
return arr[++front];
}
void showQueue() {
if(isEmpty()){
return;
}
for(int i=0;i<arr.length;i++){
System.out.printf("arr[%d]=%d\n", i, arr[i]);
}
}
/**
* 队头元素
*/
int peek() {
if(isEmpty()){
throw new RuntimeException("队列空,没有数据");
}
return arr[front+1];
}
public static void main(String[] args) {
MyArrayQueue queue = new MyArrayQueue(3);
Scanner input = new Scanner(System.in);
boolean loop = true;
char key = ' ';
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 = input.next().charAt(0);
switch(key){
case 's':
queue.showQueue();
break;
case 'a':
System.out.println("请输入一个数");
int value = input.nextInt();
queue.addQueue(value);
break;
case 'g':
try {
int res = queue.getData();
System.out.printf("取出的数据是%d\n", res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = queue.peek();
System.out.printf("队列头的数据是%d\n", res);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 'e':
input.close();
loop = false;
default:
break;
}
}
System.out.println("程序退出");
}
}
- 环形队列,避免假溢出,需要牺牲空间
import java.util.Scanner;
public class CircleQueue {
private int maxSize;
//指向队列的第一个元素
private int front;
//指向队列的最后一个元素的后一个元素
private int rear;
private int[] arr;
CircleQueue(int size) {
maxSize = size;
arr = new int[maxSize];
}
boolean isEmpty(){
return rear == front;
}
boolean isFull(){
return (rear+1)%maxSize == front;
}
void addQueue(int data){
if(isFull()){
return;
}
arr[rear] = data;
rear = (rear+1)%maxSize;
}
int getData(){
if(isEmpty()){
throw new RuntimeException("队列空,不能取数据");
}
int value = arr[front];
front = (front+1)%maxSize;
return value;
}
void show(){
if(isEmpty()){
return;
}
for(int i=front;i<front+size();i++){
System.out.printf("arr[%d]=%d\n", i%maxSize, arr[i%maxSize]);
}
}
int size(){
//判断队列中有效元素的个数
return (maxSize + rear - front)%maxSize;
}
int peek(){
if(isEmpty()){
throw new RuntimeException("队列空,没有数据");
}
return arr[front];
}
public static void main(String[] args) {
System.out.println("测试数组模拟环形队列");
CircleQueue queue = new CircleQueue(4);
Scanner input = new Scanner(System.in);
boolean loop = true;
char key;
while(loop) {
System.out.println("a(add):添加数据到队列");
System.out.println("g(get):从队列取出数据");
System.out.println("h(head):查询队列头的数据");
System.out.println("s(show):显示队列");
System.out.println("e(exit):退出程序");
key = input.next().charAt(0);
switch(key){
case 's':
queue.show();
break;
case 'a':
System.out.println("请输入一个数");
int value = input.nextInt();
queue.addQueue(value);
break;
case 'g':
try {
int res = queue.getData();
System.out.printf("取出的数据是%d\n", res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = queue.peek();
System.out.printf("队列头的数据是%d\n", res);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 'e':
input.close();
loop = false;
default:
break;
}
}
System.out.println("程序退出");
}
}