Push와 Pop를 이용해 Stack를 구현
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class Filo { static int[] array = new int[3]; static int tos; public static void main(String[] arvs) { Push(100); System.out.println("Push: " + 100); Push(200); System.out.println("Push: " + 200); Push(300); System.out.println("Push: " + 300); System.out.println(" Pop : " + Pop()); System.out.println(" Pop : " + Pop()); System.out.println(" Pop : " + Pop()); } static int Pop() { if (tos <= 0) tos = 0; return array[tos--]; } static void Push(int num) { array[tos] = num; tos++; if (tos >= 2) tos = 2; } } |
앞으로 도전방향: 배열을 동적으로 할당하여 스캐너에서 입력받기(입력수는 유저마음대로)
'Study > JAVA' 카테고리의 다른 글
Equals 메서드 (0) | 2014.10.13 |
---|---|
java FILO(2) (0) | 2014.10.13 |
Scanner 함수를 이용한 대소문자 변환 소스 (0) | 2014.10.13 |
인터페이스를 이용한 다중상속 (0) | 2014.10.10 |
추상클래스 abstract (0) | 2014.10.10 |