121 lines
3.2 KiB
Java
121 lines
3.2 KiB
Java
import java.security.cert.TrustAnchor;
|
|
import java.util.*;
|
|
|
|
class Cir_Queue {
|
|
private int[] data;
|
|
private int front, rear, size;
|
|
|
|
Cir_Queue(int size){
|
|
this.data = new int[size];
|
|
this.front = -1;
|
|
this.rear = -1;
|
|
this.size = size;
|
|
}
|
|
|
|
public boolean isEmpty(){
|
|
return this.front == -1;
|
|
}
|
|
|
|
public boolean isFull(){
|
|
return (this.rear + 1)%(this.size) == front;
|
|
}
|
|
|
|
public void enqueue(int value){
|
|
if(this.isFull()){
|
|
System.out.println("Queue is Full!");
|
|
return;
|
|
}
|
|
if(this.isEmpty()){
|
|
this.front = 0;
|
|
}
|
|
this.rear = (this.rear + 1)%(this.size);
|
|
this.data[rear] = value;
|
|
}
|
|
|
|
public int dequeue(){
|
|
if(this.isEmpty()){
|
|
System.out.println("Queue is Empty!");
|
|
return -1;
|
|
}
|
|
int value = this.data[this.front];
|
|
if(this.rear == this.front){
|
|
this.rear = -1;
|
|
this.front = -1;
|
|
}
|
|
else {
|
|
this.front = (this.front + 1)%this.size;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
public int peek(){
|
|
if(this.isEmpty()){
|
|
System.out.println("Queue is Empty!");
|
|
return -1;
|
|
}
|
|
return this.data[this.front];
|
|
}
|
|
|
|
public void info(){
|
|
if(this.isEmpty()){
|
|
System.out.println("큐가 비어있습니다.");
|
|
return;
|
|
}
|
|
|
|
StringBuilder result = new StringBuilder("Queue List : ");
|
|
int i = this.front;
|
|
while(true){
|
|
result.append(this.data[i] + " ");
|
|
if(i==this.rear) break;
|
|
i = (i + 1)%this.size;
|
|
}
|
|
System.out.println(result);
|
|
}
|
|
}
|
|
|
|
public class DS_queue {
|
|
public static void main(String[] args) {
|
|
Scanner sc= new Scanner(System.in);
|
|
System.out.println("큐의 사이즈를 입력해주세요.");
|
|
int size = Integer.parseInt(sc.nextLine());
|
|
Cir_Queue q = new Cir_Queue(size);
|
|
|
|
boolean exitFlag = true;
|
|
while(exitFlag){
|
|
System.out.println("명령어 목록 : enque [value] | deque | peek | info | exit");
|
|
String[] opcode = sc.nextLine().split(" ");
|
|
switch (opcode[0]) {
|
|
case "enque":
|
|
q.enqueue(Integer.parseInt(opcode[1]));
|
|
break;
|
|
case "deque":
|
|
if(q.isEmpty()){
|
|
q.dequeue();
|
|
break;
|
|
}
|
|
System.out.println(q.dequeue());
|
|
break;
|
|
case "peek":
|
|
if(q.isEmpty()){
|
|
q.peek();
|
|
break;
|
|
}
|
|
System.out.println(q.peek());
|
|
break;
|
|
case "info":
|
|
q.info();
|
|
break;
|
|
case "exit":
|
|
exitFlag = false;
|
|
System.out.println("Exit!!");
|
|
break;
|
|
default:
|
|
System.out.println("제대로 입력해주세요.");
|
|
break;
|
|
}
|
|
}
|
|
sc.close();
|
|
}
|
|
}
|