Stack

1 분 소요

Stack이란?

In Java, a stack is a data structure that allows for data to be added or removed only at the top of the stack. This means that the last item added to the stack will be the first item to be removed, known as the Last-In-First-Out (LIFO) principle.

Stack == 자바진영의 자료구조
상자에 물건을 쌓아올리듯이 데이터를 쌓는 자료구조이다.
“쌓는다”라는 표현을 사용한다.


특징

  1. LIFO(Last In First Out)
    먼저들어간자료가 나중에나오는 특징을 가지고있다 (선입선출의 반대개념, 항아리를 생각하면됨)
    항아리에 물건을 적재하고 다시빼낼때는 역순으로 뺀다고 생각하면된다.


용도

  • 브라우저

    Browser history: The browser history is often implemented as a stack, with the current page being at the top of the stack. When the user clicks the back button, the previous page is popped off the stack.

브라우저 히스토리는 종종 스택으로구현된다고한다.
뒤로가기 클릭시, pop() 함수를이용해 이전에있던 페이지가 표출된다.


  • 애플리케이션에서의 실행 혹은 재실행

    Undo/Redo operations: Stacks can be used to implement undo/redo functionality in applications. The actions taken by the user are stored on a stack, and when the user wants to undo an action, the previous action is popped off the stack and reversed.

대충 동작원리는 위에있는 브라우저의 예시와같다.

함수

public Element push(Element item); // 적재 
public Element pop(); // 삭제 (가장 마지막에 적재된 데이터)
public Element peek(); // 조회(가장 마지막에 적재된 데이터) 
public boolean empty(); // 스택 empty 체크 
public int seach(Object o); // 인덱스 반환(인자로받은 데이터)
...

카테고리:

업데이트: