문제

풀이
스택을 구현하면 끝나는 문제다
자바에서는 Stack 클래스를 기본적으로 지원하므로 이를 사용해도 될 것 같지만, 이번에는 스택을 직접 구현해 보았다.
Stack (Java SE 21 & JDK 21)
Type Parameters: E - Type of component elements All Implemented Interfaces: Serializable, Cloneable, Iterable , Collection , List , RandomAccess, SequencedCollection public class Stack extends Vector The Stack class represents a last-in-first-out (LIFO) st
docs.oracle.com
자료구조 스택 설명할 때 C로 구현했었는데, 형식만 조금 변형하여 자바로 그대로 구현하면 된다.
아래와 같이 클래스를 따로 생성하지 않고 배열로만 간단히 구현해도 상관 없지만,
//배열로 구현
int[] stack = new int[STACK_SIZE];
int top = -1;
void push(int item) {
stack[++top] = item;
}
int pop() {
if(top == -1) {
return -1;
}
else
return stack[top--];
}
int size() {
return top;
}
int empty() {
if(top==-1) {
return 1;
}
else {
return 0;
}
}
int top() { //peek()함수와 동일
if(top==-1) {
return -1;
}
else {
return stack[size--];
}
}
일전에 C 구조체를 사용하여 구현한 적이 있으므로 이번에도 똑같이 클래스를 사용했다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
class StackType{
final int MAX_STACK_SIZE=10000;
int[] data=new int[MAX_STACK_SIZE];
int top=-1;
public boolean empty(StackType s) {
return (s.top==-1);
}
public boolean full(StackType s) {
return s.top==(MAX_STACK_SIZE-1);
}
public void push(StackType s, int item) {
if(full(s)) {
System.out.println("스택 포화 오류");
return;
}
else
s.data[++(s.top)]=item;
}
public int pop(StackType s) {
if(empty(s)) {
return -1;
}
else
return s.data[(s.top)--];
}
public int size(StackType s) {
return (s.top+1);
}
public int top(StackType s) {
if(empty(s)) {
return -1;
}
else
return s.data[s.top];
}
}
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StackType s=new StackType();
int N=Integer.parseInt(in.readLine());
for(int i=0; i<N; i++) {
st=new StringTokenizer(in.readLine());
String cmd=st.nextToken();
switch(cmd) {
case "push":
int num=Integer.parseInt(st.nextToken());
s.push(s, num);
break;
case "pop":
System.out.println(s.pop(s));
break;
case "size":
System.out.println(s.size(s));
break;
case "empty":
if(s.empty(s))
System.out.println(1);
else
System.out.println(0);
break;
case "top":
System.out.println(s.top(s));
break;
default:
break;
}
}
}
}
직접 구현하지 않고 기본 제공되는 스택을 사용하면 이렇게 될 듯
Stack<Integer> stack=new Stack<>();
int N=Integer.parseInt(in.readLine());
for(int i=0; i<N; i++) {
st=new StringTokenizer(in.readLine());
String cmd=st.nextToken();
switch(cmd) {
case "push":
int num=Integer.parseInt(st.nextToken());
stack.push(num);
break;
case "pop":
System.out.println(stack.pop());
break;
case "size":
System.out.println(stack.size());
break;
case "empty":
if(stack.empty())
System.out.println(1);
else
System.out.println(0);
break;
case "top":
if(stack.empty())
System.out.println(-1);
else
System.out.println(stack.peek());
break;
default:
break;
}
}
'백준' 카테고리의 다른 글
백준 28278: 스택 2(java) (0) | 2024.07.12 |
---|---|
백준 9012: 괄호(java) (0) | 2024.07.12 |
백준 1676: 팩토리얼 0의 개수(java) (0) | 2024.07.09 |
백준 11650: 좌표 정렬하기(java) (0) | 2024.07.09 |
백준 2798: 블랙잭(java/C) (0) | 2024.07.06 |