package com.数据结构;
import java.util.Scanner;
public class 环形队列 {
public static void main(String[] args){
LinkedArray queue = new LinkedArray(5);
Scanner scanf = new Scanner(System.in);
boolean flag = true;
while(flag){
System.out.println("L 显示队列");
System.out.println("A 增加数据");
System.out.println("G 取数首数据");
System.out.println("S 链表数据数量");
System.out.println("E 退出程序");
String ch = scanf.next();
try{
switch (ch){
case "L" :{
queue.look();
}break;
case "A" :{
queue.add(scanf.nextInt());
}break;
case "G" :{
System.out.println("数据为"+queue.qv());
}break;
case "E" :{
flag= false;
break;
}
case "S" :System.out.println("元素数量:"+queue.queueSize());
}
}catch(Exception e){
e.printStackTrace();
}
}
scanf.close();
}
}
class LinkedArray{
private int maxSize;
private int head ;
private int tail ;
private int[] array ;
public LinkedArray(int maxSize){
this.maxSize = maxSize;
array = new int[maxSize] ;
}
public boolean max(){
return ((tail+1+maxSize)%maxSize==head);
}
public boolean empty(){
return tail==head;
}
public void add(int data){
if(max()){
throw new RuntimeException("队列已满~~~");
}
array[tail] = data ;
tail = (tail+1)%maxSize;
System.out.println("添加数据成功~~~");
}
public int qv(){
if(empty()){
throw new RuntimeException("队列是空的~~~");
}
int value = array[head];
head = (head +1)%maxSize;
return value;
}
public int queueSize(){
return (this.tail+this.maxSize-this.head)%maxSize;
}
public void look(){
for(int i=head;i!=this.tail;){
System.out.println(array[i]);
i = (i +1)%maxSize;
}
}
}