오늘은 데이터 구조에 대해서 배우는 시간이었다.
이번 시간에 배운 것은 stack 과 queue 였다.
stack은 LIFO: last in, first out 의 개념으로
정보가 위로 차곡차곡 쌓이며 쌓은 이후 가장 위에 쌓였던 정보가 우선적으로 나오는 형태이다.
접시를 쌓는 거나, 하노이의 탑의 구조를 생각하면 이해하기 쉽다.
stack method가 존재 하며 해당 메소드는 다음과 같다.
- pop — Pulls (removes) the element out of the stack. The location is specified by the pointer
- push — Pushes (inserts) the element in the stack. The location is specified by the pointer.
- peek — returns the item on the top of the stack, without removing it.
- empty — returns true if the stack is empty, false otherwise
- swap — the two top most elements of the stack can be swapped
즉 push를 통해 정보를 쌓고 pop을 통해 정보를 위에서 부터 없앤다.
그리고 peek는 자료 위에 쌓여있는 것이 무엇인지 확인해주고, empty는 stack 비었는지 아닌지 확인해준다.
마지막으로 swap은 두 stack의 제일 위에 쌓인 자료를 서로 바꾸어 주는 역할을 한다.
queue의 형태는 FIFO: first in, first out 형태로 처음 들어간 자료가 먼저 나오는 형태이다.
흔히 생각하면 맛집이나 놀이공원에 줄서서 들어가는 것과 같다고 보면된다.
queue에도 method가 존재 한다.
- enqueue() : Adds a node (value)
- dequeue() : Removes and returns the next node in the queue
- peek() : Returns the node at the front of the queue (without removing)
- isEmpty() : Returns True if the queue is empty, otherwise returns false
enqueue를 통해 자료를 쌓고 dequeue를 통해 처음 들어가 자료 부터 제거 한다.
peek 와 isEmpty는 stack 메소드와 같은 역할을 한다.
여기까지 기본적인 자료 구조로써 stack과 queue를 배웠으며,
다음주 부터는 좀 더 복잡하고 다양한 형태의 자료구조를 배울 예정이다.
'TIL(Today I Learned )' 카테고리의 다른 글
IM 7일차 TIL ( graph, tree & Binary Search Tree) (0) | 2020.05.07 |
---|---|
IM 6일차 초보개발자 TIL(Linked list & Hash table) (0) | 2020.05.05 |
IM 3일차 초보개발자 TIL (Linting & Testing) (0) | 2020.04.29 |
IM 1일차 초보개발자 (0) | 2020.04.28 |
5주차 초보개발자 TIL (0) | 2020.04.23 |