본문 바로가기
궁금해서 혹은 재밌어서 만들어보는 JAVA

독학사 교재 보다 만들어본 STACK

by 고유빙글 2021. 4. 16.
public class Stack {

	static int top = -1;
	static Object[] arr = new Object[5];

	public static void main(String[] args) {

		stack_in("A");
		stack_in("B");
		stack_in("C");
		stack_in("D");
		stack_in("E");
		System.out.println(stack_out());
		System.out.println(stack_out());
		System.out.println(stack_out());
		System.out.println(stack_out());
		System.out.println(stack_out());

	}

	public static void stack_in(Object o) {

		int n = arr.length;

		if (top >= n) {
			System.out.println("Stack if full");
		} else {
			top += 1;
			arr[top] = o;
		}

	}

	public static Object stack_out() {

		Object o = null;

		int n = arr.length;

		if (top == -1) {
			System.out.println("Stack if empty");
		} else {
			o = arr[top];
			top -= 1;
		}

		return o;
	}
}

자바에서 스택을 만들어보았다.

그냥 궁금해서

 

사실 이름이 puch, pop으로 바뀌어야 정확하다.