File tree Expand file tree Collapse file tree 1 file changed +71
-0
lines changed
weekly/week01/BOJ_10828_스택 Expand file tree Collapse file tree 1 file changed +71
-0
lines changed Original file line number Diff line number Diff line change 1+ package stduy .week01 ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .IOException ;
5+ import java .io .InputStreamReader ;
6+
7+ // 스택
8+ public class BOJ_10828 {
9+ static class MyStack {
10+ int [] st ;
11+ int top ;
12+
13+ public MyStack (int size ){
14+ this .st = new int [size ];
15+ this .top = -1 ;
16+ }
17+
18+ public void push (int x ){
19+ st [++top ] = x ;
20+ }
21+
22+ public int pop (){
23+ if (top == -1 ){
24+ return -1 ;
25+ }
26+
27+ return st [top --];
28+ }
29+
30+ public int size (){
31+ return top + 1 ;
32+ }
33+
34+ public int empty (){
35+ if (top == -1 ){
36+ return 1 ;
37+ }
38+
39+ return 0 ;
40+ }
41+
42+ public int top (){
43+ if (top == -1 ){
44+ return -1 ;
45+ }
46+
47+ return st [top ];
48+ }
49+ }
50+
51+ public static void main (String [] args ) throws IOException {
52+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
53+ StringBuilder sb = new StringBuilder ();
54+
55+ int N = Integer .parseInt (br .readLine ());
56+
57+ MyStack stack = new MyStack (N );
58+ for (int i = 0 ; i < N ; i ++) {
59+ String []s = br .readLine ().split (" " );
60+
61+ switch (s [0 ]){
62+ case "push" : stack .push (Integer .parseInt (s [1 ])); break ;
63+ case "pop" : sb .append (stack .pop ()).append ("\n " ); break ;
64+ case "size" : sb .append (stack .size ()).append ("\n " ); break ;
65+ case "empty" : sb .append (stack .empty ()).append ("\n " ); break ;
66+ case "top" : sb .append (stack .top ()).append ("\n " ); break ;
67+ }
68+ }
69+ System .out .println (sb );
70+ }
71+ }
You can’t perform that action at this time.
0 commit comments