스택이란? 후입선출

_top에서 이루어짐

스택구현 프로그램 예시

public class IntStack{

    private int[] stk;  //스택배열선언
    private int capacity;          //스택크기
    private int ptr;    //스택포인터

    public class EmptyIntStackException extends RuntimeException{
        public EmptyIntStackException(){}
    }    //비엇을때

    public class OverflowIntStackException extends RuntimeException{
        public OverflowIntStackException() {}
    }    //꽉찻을때

    //생성자
    public IntStack(int maxlen){    //backjoon -> IntStack
        ptr = 0;
        capacity = maxlen;
        try{
            stk = new int[capacity];
        } catch (OutOfMemoryError e){
            capacity = 0;
        }
    }

    //push
    public int push(int x) throws OverflowIntStackException{
        if (ptr >= capacity)
            throw new OverflowIntStackException();
        return stk[ptr++] = x;
    }

    //pop
    public int pop() throws EmptyIntStackException{
        if (ptr <= 0) {
            throw new EmptyIntStackException();
        }
        return stk[--ptr];
    }

    //peek
    public int peek() throws EmptyIntStackException{
        if (ptr <= 0){
            throw new EmptyIntStackException();
        }
        return stk[ptr - 1];
    }

    //clear
    public void clear() {
        ptr = 0;
    }

    //search
    public int indexOf(int x){
        for (int i = ptr - 1; i >= 0; i--) {
            if (stk[i] == x) {
                return i;
            }
            return -1;
        }
    }

    //배열 용량
    public int getCapacity() {
        return capacity;
    }

    //현재 데이터 량
    public int size(){
        return ptr;
    }

    //스택이 비엇는지
    public boolean isEmpty(){
        return ptr <= 0;
    }

    //데이터 바닥부터 순서대로 모두 출력
    public void dump(){
        if(ptr <= 0){
            System.out.println("스택이 비어 있습니다.");
        } else{
            for (int i = 0; i < ptr; i++) {
                System.out.print(stk[i] + " ");
                System.out.println();
            }
        }
    }

}

-? pop에서 해당값을 출력하고 ptr -1해야되니까 ptr— 해야되는 거아닌가?

사용예시

import java.util.Scanner;

class IntStackTester {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        IntStack s = new IntStack(64);  //생성자 생성(capacity = 64)_용량

        while (true) {
            System.out.println();
            System.out.printf("현재 데이터 개수: %d / %d\\n", s.size(), s.getCapacity());
            System.out.print("(1) 푸시 (2) 팝 (3) 피크 (4) 덤프 (0) 종료: ");

            int menu = sc.nextInt();
            if(menu = 0) break;

            int x;
            switch (menu) {
                case 1:
                System.out.print("데이터: ");
                    x = sc.nextInt();
                    try{
                        s.push(x);
                    } catch (IntStack.OverflowIntStackException e){
                        System.out.println("스택이 가득 찼습니다");
                    }
                    break;
                case 2:
                    try {
                        x = s.pop();
                        System.out.println("팝한 데이터는 " + x + "입니다.");
                    } catch (IntStack.EmptyIntStackException e) {
                        System.out.println("스택이 비어 있습니다.");
                    }
                    break;
                    
                case 3:
                    try {
                        x = s.peek();
                        System.out.println("피크한 데이터는 " + x + "입니다.");
                    } catch (IntStack.EmptyIntStackException e) {
                        System.out.println("스택이 비어 있습니다.");
                    }
                    break;
                    
                case 4:
                    s.dump();
                    break;
            }
        }

    }
}