본문 바로가기

미분류/Algorithm

JAVA STACK 구현

스택을 구현해보았다.

package array2; import java.util.Stack; public class StackTest { public static int[] stack = new int[1000]; public static int top = -1; public static boolean push(int value) { if(top>=stack.length) return false; top++; stack[top] = value; return true; } public static int pop() { if(top==-1) return -1; int value = stack[top--]; return value; } public static void main(String[] args) { push(1); push(2); push(3); System.out.println(pop()); System.out.println(pop()); System.out.println(pop()); System.out.println(pop()); Stack<String> s = new Stack<>(); s.push("Hello"); s.push("Stack"); s.push("World"); System.out.println(s.pop()); } }