learn/data structure

스택 (Stack)

사겅이 2023. 9. 24. 05:30

출처 : https://www.geeksforgeeks.org/stack-meaning-in-dsa/

데이터를 후입선출(LIFO - Last-In-First-Out) 방식으로 관리하는 자료구조로써 데이터는 스택의 가장 위에 쌓이고, 가장 위에 있는 데이터가 먼저 꺼내짐

 

package datastructure.linear;

import java.util.Stack;

public class StackTest {
    public static void main(String[] args) {
        // 스택 생성
        Stack<Integer> stack = new Stack<>();

        // 데이터 추가 (push)
        stack.push(1);
        stack.push(2);
        stack.push(3);

        // 스택의 상단 데이터 확인 (peek)
        System.out.println("Top element: " + stack.peek());

        // 스택에서 데이터 꺼내기 (pop)
        while (!stack.isEmpty()) {
            int element = stack.pop();
            System.out.println("Popped: " + element);
        }
    }
}

'learn > data structure' 카테고리의 다른 글

데크 (Deque)  (0) 2023.09.24
큐 (Queue)  (0) 2023.09.24
연결 리스트 (Linked List)  (1) 2023.09.24
배열 (Array)  (1) 2023.09.24
자료구조 (Data Structure)  (0) 2023.09.23