2025-08-10 02:16:11 +09:00

77 lines
2.1 KiB
TypeScript

class Stack{
private data: number[];
private top: number;
constructor(){
this.data = [];
this.top = -1;
}
public info(){
if(this.top == -1){
console.log("스택이 비어있습니다");
return;
}
let result: string = "Stack List(" + (this.top+1).toString() + " values) : "
for(let i=0; i<=this.top; i++){
result += this.data[i].toString() + " ";
}
console.log(result);
}
public push(value: number){
this.data[++(this.top)] = value;
console.log("push 성공!");
}
public pop(): number | undefined {
if(this.top === -1){
return undefined;
}
return this.data[(this.top)--];
}
public peek(): number | undefined {
if(this.top === -1){
return undefined;
}
return this.data[this.top];
}
}
let stack = new Stack();
let exitFlag = true;
while(exitFlag){
console.log("++++++++++++++++명령어모음++++++++++++++++");
console.log("push <value> | pop | peek | info | exit");
console.log("========================================");
console.log("명령어를 입력해주세요!");
const input:string[] = require("fs").readFileSync(0,"utf8").toString().trim().split(" ");
switch(input[0])
{
case "push":
stack.push(Number(input[1]));
break;
case "pop":
var popValue = stack.pop();
console.log(popValue===undefined ? "스택이 비었습니다." : "pop 완료 : " + popValue.toString());
break;
case "peek":
var peekValue = stack.peek();
console.log(peekValue===undefined ? "스택이 비었습니다." : "최상단 값 : " + peekValue.toString());
break;
case "info":
stack.info();
break;
case "exit":
exitFlag = false;
console.log("종료합니다.")
break;
default:
console.log("제대로 입력해주세요.")
break;
}
}