package stack;
import java.util.Scanner;
import java.util.Vector;
public class Stack {
private int top =-1;
private Vector<String> vc = new Vector<String>();
public Stack() {
// TODO Auto-generated constructor stub
while(true){
String str = "";
System.out.println("1 : push , 2 : pop");
Scanner scan = new Scanner(System.in);
int k = scan.nextInt();
switch (k) {
case 1:
System.out.println(" 데이터 입력 ");
str = scan.next();
push(str);
peek();
break;
case 2:
peek();
pop();
break;
default:
break;
}
}
}
private void push(String data) {
// TODO Auto-generated method stub
vc.add(data);
top++;
}
private void pop() {
// TODO Auto-generated method stub
if(!empty()){
vc.remove(top);
top--;
}
}
private void peek() {
// TODO Auto-generated method stub
System.out.println("top : " + top);
if(!empty()){
String peek = vc.get(top);
System.out.println("peek : " + peek);
}else{
System.out.println("EmptyStackException");
}
}
private boolean empty() {
// TODO Auto-generated method stub
if (top < 0) {
return true;
}
return false;
}
}
'개발자 > Java' 카테고리의 다른 글
Java Queue (0) | 2015.07.01 |
---|---|
jasperreports: Font '바탕' is not available to the JVM. See the Javadoc for more details. (4) | 2014.12.09 |
원하는 일수만큼 입력받아서 현재날짜로부터 이전날짜를 구하기 (0) | 2014.06.23 |
java.lang.NoClassDefFoundError : <wrong name : > (0) | 2014.01.28 |
자바 슈팅 게임 2단계 (2) | 2013.12.08 |