Data Stucture
-
[Data Structure] Java ํ(Queue) ์ฌ์ฉData Structure 2021. 1. 15. 14:15
์ด ๊ธ์ "์จ๋ผ์ธ ์๋ฐ ์คํฐ๋ ๋ด์ฉ"์ ์ ๋ฆฌํ ๊ธ์ ๋๋ค. Queue๋? ํ๋ ์ถ๊ตฌ์ ์ ๊ตฌ๊ฐ ๋ช ๋ฐฑํ ์กด์ฌํ์ฌ ๋ฐ์ดํฐ๋ฅผ ๋ด๋ ๋ฐฉ์์ ์๋ฃ๊ตฌ์กฐ์ ๋๋ค. ํ์ ์ฃผ์ ํน์ง์ First In, First Out (FIFO) ๊ตฌ์กฐ๋ฅผ ๊ฐ์ต๋๋ค. ๋ฐฐ์ด๋ก ํ ๊ตฌํ public class Queue { public class Queue { final int MAX_SIZE = 100; int[] arr; int size; public Queue() { arr = new int[MAX_SIZE]; size = 0; } void push(int data){ arr[size++] = data; } int front(){ return arr[0]; } int pop(){ if(size == 0){ return 0; } else{ f..
-
[Data Stucture] map(๋งต) ์ฌ์ฉ๋ฒData Structure 2020. 2. 22. 18:58
C++ STL ์ค ํ๋์ธ map์ ๋ํ ์ค๋ช ์ ๋๋ค. map ์ด๋? map์ key์ value์ ์กฐํฉ์ผ๋ก ๊ตฌ์ฑ๋ container ์ ๋๋ค. map์ key์ compare ํจ์๋ฅผ ํตํด ์ ๋ ฌ๋ฉ๋๋ค. (๊ธฐ๋ณธ์ ์ผ๋ก ์ค๋ฆ์ฐจ์) ์ ๋๋ฆญ์ผ๋ก ๊ตฌํ๋์ด key์ value๋ ์ด๋ ํ type์ด ์ฌ ์ ์์ต๋๋ค. map์ binary search tree๋ก ๊ตฌ์ฑ๋์ด ์์ด์ unordered_map์ ๋นํด ํ์์ด ๋๋ฆฝ๋๋ค. (์๊ฐ ๋ณต์ก๋ O(log n)) map์ ์ค๋ณต์ ํ์ฉํ์ง ์๊ณ ์ค๋ณต key๋ฅผ ๊ฐ์ง๊ธฐ ์ํด์๋ multimap์ ์ฌ์ฉํด์ผ ํฉ๋๋ค. #include ์ ์ ์ธํด์ผ map ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ธฐ๋ณธ ํจ์ empty ( ) ๋งต์ด ๋น์ด์๋์ง ํ์ธํ๋ ํจ์ if map is empty, then r..
-
[Data Stucture] Stack(์คํ) ์ฌ์ฉ๋ฒData Structure 2020. 2. 22. 17:00
C++ STL ์ค ํ๋์ธ Stack ์ฌ์ฉ๋ฒ์ ๋๋ค. Stack ์ด๋? Stack์ LIFO(Last in First Out) ์ผ๋ก ์ค๊ณ๋ container ์ ๋๋ค. ์ ๋๋ฆญํ๊ฒ ๊ตฌํ๋์ด ์์ด ์ด๋ ํ ์์๋ผ๋ ๋ฉค๋ฒ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ push, pop ํ ์ ์์ต๋๋ค. #include ์ ์ ์ธํด์ผ stack ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ธฐ๋ณธ ํจ์ empty ( ) ์คํ์ด ๋น์ด ์๋์ง ํ์ธํ๋ ํจ์ if stack is empty, then return 1 else 0 size ( ) ์คํ์ ํฌ๊ธฐ๋ฅผ ๋ฐํํ๋ ํจ์ return size_type (unsigned int) top ( ) ์คํ์ ๋ง์ง๋ง ์์๋ฅผ ๋ฐํํ๋ ํจ์ return reference top element push ( eleme..