[StackTest.java]
import java.util.Scanner;
class MyStack {
private int[] mystack;
private int size;// 스택의 사이즈
private int top;
public MyStack(int length) {
// this.size = size;
mystack = new int[length];
}
public void push(int data) {
mystack[size] = data;
size++;
}
public int empty() {
int result = 0;
if (size == 0) {
result = 1;
}
return result;
}
public int pop() {
if (empty() == 1) {
return -1;
} else {
size = size - 1;
return mystack[size];
}
}
public int size() {
return this.size;
}
public int top() {
if (size == 0) {
return -1;
} else {
return mystack[size - 1];
}
}
}
public class StackTest {
public static void main(String[] args) throws Exception {
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner key = new Scanner(System.in);
//String str = br.readLine();
int count = key.nextInt();
key.nextLine();
MyStack obj = new MyStack(count);
for (int i = 0; i < count; i++) {
String line = key.nextLine();
String[] resultarr = line.split(" ");
switch (resultarr[0]) {
case "push":
int data = Integer.parseInt(resultarr[1]);
obj.push(data);
break;
case "top":
System.out.println(obj.top());
break;
case "size":
System.out.println(obj.size());
break;
case "empty":
System.out.println(obj.empty());
break;
case "pop":
System.out.println(obj.pop());
break;
}
}
}
}
댓글