Java Stack

개발자/Java 2015. 7. 1. 13:30

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;

}

}


블로그 이미지

김진리

,