stack
문제, 결과
#source
import java.awt.List;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static class Stack
{
private int[] mStack;
final int mMaxCount;
int mPos; // 위치이자 개수
public Stack(int size)
{
mMaxCount = size;
mStack = new int[mMaxCount];
mPos = 0;
}
public void push(int value)
{
if(mPos >= mMaxCount)
return;
mStack[mPos] = value;
++mPos;
}
public int pop()
{
if(0 >= mPos)
{
System.out.println("-1");
return -1;
}
--mPos;
int value = mStack[mPos];
mStack[mPos] = value;
System.out.println(value);
return value;
}
public int size()
{
System.out.println(mPos);
return mPos;
}
public int empty()
{
if(0 == mPos)
{
System.out.println(1);
return 1;
}
System.out.println(0);
return 0;
}
public int top()
{
if(0 >= mPos)
{
System.out.println(-1);
return -1;
}
System.out.println(mStack[mPos-1]);
return mStack[mPos-1];
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int count = Integer.parseInt(sc.nextLine());
Stack stack = new Stack(10000);
String[] str = new String[count];
for(int i = 0; i < count; ++i)
{
str[i] = sc.nextLine();
}
for(int i = 0; i < count; ++i)
{
Parser(stack, str[i]);
}
}
static void Parser(Stack stack, String str)
{
String command = str;
int spaceIndex = str.lastIndexOf(" ");
if(-1 != spaceIndex)
{
command = str.substring(0, spaceIndex);
}
// 비어있는문자가 없다면 하나의 문자로 이루어진 것
if(command.equals(""))
{
System.out.println("equals");
command = str;
}
if(command.equals("push"))
{
int tempSpaceIndex = str.lastIndexOf(" ");
if(-1 == tempSpaceIndex)
{
System.out.println("value를 입력하세요");
return;
}
String temp = str.substring(tempSpaceIndex+1, str.length());
int value = Integer.parseInt(temp);
stack.push(value);
}
else if(command.equals("pop"))
{
stack.pop();
}
else if(command.equals("size"))
{
stack.size();
}
else if(command.equals("empty"))
{
stack.empty();
}
else if(command.equals("top"))
{
stack.top();
}
}
}