대기열Queue 시뮬레이션 대기열 생성 문제
8083 단어 [데이터 구조와 알고리즘]java 데이터 구조 및 알고리즘
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Utility {
public static String getString() throws IOException{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader bf = new BufferedReader(in);
String s = bf.readLine();
return s;
}
}
import java.io.IOException;
public class SuperMarket {
//
private Queue[] queue = {null,new Queue(20),new Queue(20),new Queue(20),new Queue(20)};
public void simulate() throws IOException{//
long id=0;//
boolean flag=true;
while(flag){
System.out.println(" :");
System.out.print("0. 。");
System.out.print("1. 1 。");
System.out.print("2. 2 。");
System.out.print("3. 3 。");
System.out.print("4. 4 。");
System.out.println("q. !");
String s = Utility.getString();
if(s.length()==0){
continue;
}
char ch = s.charAt(0);
switch(ch){
case '0':
id++;
insertQueue(id);
displayQueue();
break;
case '1':
removeQueue(1);
displayQueue();
break;
case '2':
removeQueue(2);
displayQueue();
break;
case '3':
removeQueue(3);
displayQueue();
break;
case '4':
removeQueue(4);
displayQueue();
break;
case 'q':
flag =false;
System.out.println("byebye!");
break;
default:
break;
}
}
}
private void removeQueue(int queueId){
if(queue[queueId].size()==0){
return;
}
long id = queue[queueId].remove();
System.out.println(" " + id + " " + queueId + " !");
}
public void insertQueue(long id){
int queueId = getMinQueueId();
queue[queueId].insert(id);
System.out.println(" " + id + " " + queueId + " ");
}
private int getMinQueueId(){
int min =1;
for(int i=2;i<5;i++){
if(queue[i].size()return min;
}
public void displayQueue(){
for(int i=1;i<5;i++){
System.out.print(" " + i + " ");
queue[i].display();
}
System.out.println();
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
SuperMarket sm = new SuperMarket();
sm.simulate();
}
}