废话不多说,直接上代码:
简单队列
import java.util.Scanner;
public class Test01 {
private Integer maxSize;
private Integer front;
private Integer rear;
private Integer[] arr;
public Test01(Integer maxSize) {
this.maxSize = maxSize;
this.front = -1;
this.rear = -1;
this.arr = new Integer[maxSize];
}
public boolean isNull(){
return front.equals(rear);
}
public boolean isFull(){
return rear.equals(maxSize-1);
}
public void addQueue(int n){
if (isFull()){
throw new RuntimeException("队列已经满");
}
rear+=1;
arr[rear] = n;
}
public int getQueue(){
if (isNull()){
throw new RuntimeException("队列为空,无法取出");
}
front+=1;
return arr[front];
}
public void show(){
if (isNull()) {
throw new RuntimeException("队列为空");
}
for (int i = front+1; i < rear+1; i++) {
System.out.printf("arr[%d]=%d\n",i,arr[i]);
}
}
public int headHead(){
if (isNull()) {
throw new RuntimeException("队列为空");
}
return arr[front+1];
}
}
class Main{
public static void main(String[] args) {
Test01 test= new Test01(3);
char key =' ';
Scanner s =new Scanner(System.in);
boolean loop =true;
while (loop){
System.out.println("1:显示队列");
System.out.println("2:退出程序");
System.out.println("3: 添加队列");
System.out.println("4: 拿出队列");
System.out.println("4: 查看头队列");
char next = s.next().charAt(0);
switch (next) {
case '1':
try {
test.show();
}catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case '2':
loop =false;
break;
case '3':
System.out.println("输出一个数");
int i = s.nextInt();
try {
test.addQueue(i);
}catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case '4':
try {
int queue = test.getQueue();
System.out.println("你取出的数据是:"+queue);
}catch(Exception e){
System.out.println(e.getMessage()); }
break;
case '5':
try {
test.headHead();
}catch (Exception e) {
System.out.println(e.getMessage());
}
break;
}
}
}
}
环形队列:
import java.util.Scanner;
public class Test02 {
private Integer maxSize;
private Integer front;
private Integer rear;
private Integer[] arr;
public Test02(Integer maxSize) {
this.maxSize = maxSize;
this.front = 0;
this.rear = 0;
this.arr = new Integer[maxSize];
}
public boolean isNull() {
return front.equals(rear);
}
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
public void addQueue(int n) {
if (isFull()) {
throw new RuntimeException("队列已经满");
}
arr[rear] = n;
rear = (rear + 1) % maxSize;
}
public int getQueue() {
if (isNull()) {
throw new RuntimeException("队列为空,无法取出");
}
int value = arr[front];
front += 1;
return value;
}
public void show() {
if (isNull()) {
throw new RuntimeException("队列为空");
}
for (int i = front; i < front+((rear + maxSize - front) % maxSize); i++) {
System.out.printf("arr[%d]=%d\n", i%maxSize, arr[i%maxSize]);
}
}
public int headHead() {
if (isNull()) {
throw new RuntimeException("队列为空");
}
return arr[front];
}
}
class Main02 {
public static void main(String[] args) {
Test02 test = new Test02(4);
char key = ' ';
Scanner s = new Scanner(System.in);
boolean loop = true;
while (loop) {
System.out.println("1:显示队列");
System.out.println("2:退出程序");
System.out.println("3: 添加队列");
System.out.println("4: 拿出队列");
System.out.println("5: 查看头队列");
char next = s.next().charAt(0);
switch (next) {
case '1':
try {
test.show();
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case '2':
loop = false;
break;
case '3':
System.out.println("输出一个数");
int i = s.nextInt();
try {
test.addQueue(i);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case '4':
try {
int queue = test.getQueue();
System.out.println("你取出的数据是:" + queue);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case '5':
try {
int i1 = test.headHead();
System.out.println("头队列:"+i1);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
}
}
}
}