109 lines
2.9 KiB
TypeScript
109 lines
2.9 KiB
TypeScript
export {};
|
|
|
|
class CircularQueue {
|
|
private data: number[];
|
|
private front: number;
|
|
private rear: number
|
|
private size: number;
|
|
|
|
constructor(size: number){
|
|
this.size = size;
|
|
this.rear = -1;
|
|
this.front = -1;
|
|
this.data = Array.from({length: size}, () => 0);
|
|
}
|
|
|
|
public isEmpty(): boolean{
|
|
return this.front === -1;
|
|
}
|
|
|
|
public isFull(): boolean{
|
|
return (this.rear + 1)%(this.size) === this.front;
|
|
}
|
|
|
|
public enqueue(value: number){
|
|
if(this.isFull()){
|
|
console.log("Queue is full!");
|
|
return;
|
|
}
|
|
if(this.isEmpty()){
|
|
this.front = 0;
|
|
}
|
|
|
|
this.rear = (this.rear + 1)%(this.size);
|
|
this.data[this.rear] = value;
|
|
}
|
|
|
|
public dequeue(): number | undefined {
|
|
if(this.isEmpty()) return undefined;
|
|
const value: number = 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 peek(): number | undefined {
|
|
return (this.isEmpty()) ? undefined : this.data[this.front];
|
|
}
|
|
|
|
public info() {
|
|
if(this.isEmpty()){
|
|
console.log("Empty!");
|
|
return;
|
|
}
|
|
let result = "list : [";
|
|
let i = this.front;
|
|
while(true){
|
|
result += this.data[i].toString();
|
|
if(i === this.rear){
|
|
result += "]";
|
|
break;
|
|
}
|
|
i = (i+1)%(this.size);
|
|
result += ",";
|
|
}
|
|
console.log(result);
|
|
}
|
|
}
|
|
|
|
console.log("원형큐를 생성합니다. 사이즈를 입력해주세요.")
|
|
const size: number = Number(require("fs").readFileSync(0,"utf8").toString().trim());
|
|
let cq = new CircularQueue(size);
|
|
let exitFlag: boolean = true;
|
|
|
|
while(exitFlag){
|
|
console.log("++++++++++++++++명령어모음++++++++++++++++");
|
|
console.log("enqueue <value> | dequeue | peek | info | exit");
|
|
console.log("========================================");
|
|
console.log("명령어를 입력해주세요!");
|
|
const input:string[] = require("fs").readFileSync(0,"utf8").toString().trim().split(" ");
|
|
switch(input[0])
|
|
{
|
|
case "enqueue":
|
|
cq.enqueue(Number(input[1]));
|
|
break;
|
|
case "dequeue":
|
|
const deqvalue = cq.dequeue();
|
|
console.log(deqvalue===undefined ? "큐가 비었습니다." : deqvalue);
|
|
break;
|
|
case "peek":
|
|
const peekValue = cq.peek();
|
|
console.log(peekValue===undefined ? "큐가 비었습니다." : peekValue);
|
|
break;
|
|
case "info":
|
|
cq.info();
|
|
break;
|
|
case "exit":
|
|
exitFlag = false;
|
|
console.log("종료합니다.")
|
|
break;
|
|
default:
|
|
console.log("제대로 입력해주세요.")
|
|
break;
|
|
}
|
|
} |