├── linked_list ├── singly_linked_list │ ├── list.h │ ├── implementation_example │ │ └── list.h │ └── README.md ├── circular_linked_list │ ├── list.h │ ├── grademe.sh │ ├── implementation_example │ │ └── list.h │ └── README.md ├── doubly_linked_list │ ├── list.h │ ├── implementation_example │ │ └── list.h │ ├── grademe.sh │ ├── README.md │ └── grademe_files │ │ ├── doubly_linked_list_output │ │ └── test.c └── README.md ├── graph ├── directed_list │ ├── directed_graph.jpeg │ ├── implementation │ │ ├── graph.h │ │ └── test_main.c │ └── README.md ├── undirected_matrix │ ├── undirected_graph.jpeg │ ├── implementation │ │ ├── graph.h │ │ └── test_main.c │ └── README.md ├── floyd-warshall │ ├── implementation │ │ ├── fw.h │ │ ├── graph.h │ │ ├── fw.c │ │ └── test_main.c │ └── README.md ├── dijkstra │ ├── README.md │ └── implementation │ │ ├── dijkstra.h │ │ ├── heap.h │ │ ├── graph.h │ │ ├── test_main.c │ │ ├── heap.c │ │ └── dijkstra.c ├── prim │ ├── README.md │ └── implementation │ │ ├── prim.h │ │ ├── heap.h │ │ ├── graph.h │ │ ├── test_main.c │ │ └── heap.c ├── bellman-ford │ ├── implementation │ │ ├── bf.h │ │ ├── graph.h │ │ ├── bf.c │ │ └── test_main.c │ └── README.md ├── kruskal │ ├── README.md │ └── implementation │ │ ├── kruskal.h │ │ ├── heap.h │ │ ├── graph.h │ │ ├── test_main.c │ │ └── heap.c ├── README.md └── undirected_list │ ├── implementation │ ├── graph.h │ └── test_main.c │ └── README.md ├── queue ├── queue_array │ ├── queue.h │ ├── README.md │ └── implementation_example │ │ ├── queue.h │ │ ├── queue_array_implementation.c │ │ └── test_main.c ├── queue_linked_list │ ├── queue.h │ ├── implementation_example │ │ ├── queue.h │ │ └── queue_linked_list_implementation.c │ └── README.md └── README.md ├── stack ├── stack_array │ ├── stack.h │ ├── README.md │ └── implementation_example │ │ ├── stack.h │ │ ├── stack_array_implementation.c │ │ └── test_main.c ├── stack_linked_list │ ├── stack.h │ ├── implementation_example │ │ ├── stack.h │ │ ├── stack_linked_list_implementation.c │ │ └── test_main.c │ └── README.md └── README.md ├── tree ├── heap │ ├── heap.h │ ├── README.md │ └── implementation_example │ │ ├── heap.h │ │ └── heap.c ├── trie │ ├── trie.h │ ├── README.md │ └── implementation_example │ │ ├── trie.h │ │ ├── test_main.c │ │ └── trie.c ├── binary_search_tree │ ├── tree.h │ ├── implementation_example │ │ └── tree.h │ └── README.md ├── union_find │ ├── README.md │ └── implementation_example │ │ ├── union_find.h │ │ ├── union_find.c │ │ └── test_main.c └── README.md ├── deque ├── deque_linked_list │ ├── deque.h │ ├── implementation_example │ │ └── deque.h │ └── README.md └── README.md ├── sort ├── bubble │ ├── README.md │ └── implementation │ │ ├── bubble.h │ │ ├── bubble.c │ │ └── test_main.c ├── heap │ ├── README.md │ └── implementation │ │ ├── heap.h │ │ ├── test_main.c │ │ └── heap.c ├── merge │ ├── README.md │ └── implementation │ │ ├── merge.h │ │ ├── merge.c │ │ └── test_main.c ├── quick │ ├── README.md │ └── implementation │ │ ├── quick.h │ │ ├── quick.c │ │ └── test_main.c └── README.md ├── .gitignore ├── hash ├── rabin_karp │ ├── README.md │ └── implementation │ │ ├── rabin_karp.h │ │ ├── test_main.c │ │ └── rabin_karp.c ├── README.md ├── hash_map │ ├── implementation │ │ └── map.h │ └── README.md └── hash_set │ ├── README.md │ └── implementation │ └── set.h ├── two_pointers_sliding_window └── README.md ├── README.md ├── simulation └── README.md └── brute_force └── README.md /linked_list/singly_linked_list/list.h: -------------------------------------------------------------------------------- 1 | typedef struct s_node 2 | { 3 | int data; 4 | struct s_node *next; 5 | } t_node; 6 | -------------------------------------------------------------------------------- /graph/directed_list/directed_graph.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dev-jko/42-algorithm/HEAD/graph/directed_list/directed_graph.jpeg -------------------------------------------------------------------------------- /queue/queue_array/queue.h: -------------------------------------------------------------------------------- 1 | typedef struct s_queue 2 | { 3 | unsigned int max_size; 4 | int last_index; 5 | void **data; 6 | } t_queue; 7 | -------------------------------------------------------------------------------- /stack/stack_array/stack.h: -------------------------------------------------------------------------------- 1 | typedef struct s_stack 2 | { 3 | unsigned int max_size; 4 | int top_index; 5 | void **data; 6 | } t_stack; 7 | -------------------------------------------------------------------------------- /graph/undirected_matrix/undirected_graph.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dev-jko/42-algorithm/HEAD/graph/undirected_matrix/undirected_graph.jpeg -------------------------------------------------------------------------------- /linked_list/singly_linked_list/implementation_example/list.h: -------------------------------------------------------------------------------- 1 | typedef struct s_node 2 | { 3 | int data; 4 | struct s_node *next; 5 | } t_node; 6 | -------------------------------------------------------------------------------- /tree/heap/heap.h: -------------------------------------------------------------------------------- 1 | typedef struct s_heap 2 | { 3 | unsigned int max_size; 4 | unsigned int size; 5 | int (*cmp)(void *, void *); 6 | void **data; 7 | } t_heap; 8 | -------------------------------------------------------------------------------- /tree/trie/trie.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct s_node 4 | { 5 | struct s_node *next[26]; 6 | bool finish; 7 | } t_node; 8 | 9 | typedef t_node *t_trie[26]; 10 | -------------------------------------------------------------------------------- /stack/stack_linked_list/stack.h: -------------------------------------------------------------------------------- 1 | typedef struct s_node 2 | { 3 | void *data; 4 | struct s_node *next; 5 | } t_node; 6 | 7 | typedef struct s_stack 8 | { 9 | unsigned int size; 10 | t_node *top; 11 | } t_stack; 12 | -------------------------------------------------------------------------------- /queue/queue_linked_list/queue.h: -------------------------------------------------------------------------------- 1 | typedef struct s_node 2 | { 3 | void *data; 4 | struct s_node *next; 5 | } t_node; 6 | 7 | typedef struct s_queue 8 | { 9 | unsigned int size; 10 | t_node *head; 11 | t_node *tail; 12 | } t_queue; 13 | -------------------------------------------------------------------------------- /deque/deque_linked_list/deque.h: -------------------------------------------------------------------------------- 1 | typedef struct s_node 2 | { 3 | void *data; 4 | struct s_node *prev; 5 | struct s_node *next; 6 | } t_node; 7 | 8 | typedef struct s_deque 9 | { 10 | unsigned int size; 11 | t_node *front; 12 | t_node *back; 13 | } t_deque; 14 | -------------------------------------------------------------------------------- /linked_list/circular_linked_list/list.h: -------------------------------------------------------------------------------- 1 | typedef struct s_node 2 | { 3 | void *data; 4 | struct s_node *prev; 5 | struct s_node *next; 6 | } t_node; 7 | 8 | typedef struct s_linked_list 9 | { 10 | unsigned int size; 11 | t_node *head; 12 | } t_linked_list; 13 | -------------------------------------------------------------------------------- /linked_list/doubly_linked_list/list.h: -------------------------------------------------------------------------------- 1 | typedef struct s_node 2 | { 3 | int data; 4 | struct s_node *prev; 5 | struct s_node *next; 6 | } t_node; 7 | 8 | typedef struct s_linked_list 9 | { 10 | unsigned int size; 11 | t_node *head; 12 | t_node *tail; 13 | } t_linked_list; 14 | -------------------------------------------------------------------------------- /tree/binary_search_tree/tree.h: -------------------------------------------------------------------------------- 1 | typedef struct s_node 2 | { 3 | void *data; 4 | struct s_node *left; 5 | struct s_node *right; 6 | } t_node; 7 | 8 | typedef struct s_binary_search_tree 9 | { 10 | t_node *root; 11 | int (*cmp)(void *, void *); 12 | int size; 13 | } t_tree; 14 | -------------------------------------------------------------------------------- /linked_list/doubly_linked_list/implementation_example/list.h: -------------------------------------------------------------------------------- 1 | typedef struct s_node 2 | { 3 | int data; 4 | struct s_node *prev; 5 | struct s_node *next; 6 | } t_node; 7 | 8 | typedef struct s_linked_list 9 | { 10 | unsigned int size; 11 | t_node *head; 12 | t_node *tail; 13 | } t_linked_list; 14 | -------------------------------------------------------------------------------- /sort/bubble/README.md: -------------------------------------------------------------------------------- 1 | # Bubble Sort 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - bubble sort를 구현 합니다. 8 | 9 | ### bubble_sort 10 | - bubble sort를 사용하여 items를 오름차순으로 정렬하는 함수를 작성하세요. 11 | - items는 size 갯수의 item으로 이루어져있습니다. 12 | - cmp는 item을 비교하는데 사용 됩니다. 첫번째 인자가 더 크면 양수, 작으면 음수, 같다면 0을 반환 합니다. 13 | ``` 14 | void bubble_sort(void **items, int size, int (*cmp)(void *, void *)); 15 | ``` 16 | 17 | [뒤로 가기](..) 18 | -------------------------------------------------------------------------------- /sort/heap/README.md: -------------------------------------------------------------------------------- 1 | # Heap Sort 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - heap sort를 구현 합니다. 8 | 9 | ### heap_sort 10 | - heap sort를 사용하여 items를 오름차순으로 정렬하는 함수를 작성하세요. 11 | - items는 size 갯수의 item으로 이루어져있습니다. 12 | - cmp는 item을 비교하는데 사용 됩니다. 첫번째 인자가 더 크면 양수, 작으면 음수, 같다면 0을 반환 합니다. 13 | - 성공 시 1, 실패 시 0을 반환 합니다. 14 | ``` 15 | int heap_sort(void **items, int size, int (*cmp)(void *, void *)); 16 | ``` 17 | 18 | [뒤로 가기](..) 19 | -------------------------------------------------------------------------------- /sort/merge/README.md: -------------------------------------------------------------------------------- 1 | # Merge Sort 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - merge sort를 구현 합니다. 8 | 9 | ### merge_sort 10 | - merge sort를 사용하여 items를 오름차순으로 정렬하는 함수를 작성하세요. 11 | - items는 size 갯수의 item으로 이루어져있습니다. 12 | - cmp는 item을 비교하는데 사용 됩니다. 첫번째 인자가 더 크면 양수, 작으면 음수, 같다면 0을 반환 합니다. 13 | - 성공 시 1, 실패 시 0을 반환 합니다. 14 | ``` 15 | int merge_sort(void **items, int size, int (*cmp)(void *, void *)); 16 | ``` 17 | 18 | [뒤로 가기](..) 19 | -------------------------------------------------------------------------------- /sort/quick/README.md: -------------------------------------------------------------------------------- 1 | # Quick Sort 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - quick sort를 구현 합니다. 8 | 9 | ### quick_sort 10 | - quick sort를 사용하여 items를 오름차순으로 정렬하는 함수를 작성하세요. 11 | - items는 size 갯수의 item으로 이루어져있습니다. 12 | - cmp는 item을 비교하는데 사용 됩니다. 첫번째 인자가 더 크면 양수, 작으면 음수, 같다면 0을 반환 합니다. 13 | - 성공 시 1, 실패 시 0을 반환 합니다. 14 | ``` 15 | int quick_sort(void **items, int size, int (*cmp)(void *, void *)); 16 | ``` 17 | 18 | [뒤로 가기](..) 19 | -------------------------------------------------------------------------------- /deque/README.md: -------------------------------------------------------------------------------- 1 | # Deque 2 | 3 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 4 | 5 | ## index 6 | 1. [deque(linked list)](./deque_linked_list) 7 | 1. [baekjoon](#baekjoon) 8 | 1. [생각해보기](#생각해보기) 9 | 10 | ## baekjoon 11 | - [10866번 덱](https://www.acmicpc.net/problem/10866) 12 | - [1021번 회전하는 큐](https://www.acmicpc.net/problem/1021) 13 | - [5430번 AC](https://www.acmicpc.net/problem/5430) 14 | 15 | ## 생각해보기 16 | - deque가 사용되는 상황 17 | - array로 deque를 구현하려면? 18 | 19 | 20 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 21 | -------------------------------------------------------------------------------- /queue/README.md: -------------------------------------------------------------------------------- 1 | # Queue 2 | 3 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 4 | 5 | ## index 6 | 1. [queue(array)](./queue_array) 7 | 1. [queue(linked list)](./queue_linked_list) 8 | 1. [baekjoon](#baekjoon) 9 | 1. [생각해보기](#생각해보기) 10 | 11 | ## baekjoon 12 | - [18258번 큐 2](https://www.acmicpc.net/problem/18258) 13 | - [2164번 카드2](https://www.acmicpc.net/problem/2164) 14 | - [1966번 프린터 큐](https://www.acmicpc.net/problem/1966) 15 | 16 | ## 생각해보기 17 | - queue를 array로 구현 했을 때와 linked list로 구현 했을 때의 장단점 18 | - queue가 사용되는 상황 19 | - array로 queue를 효율성 있게 구현하려면? 20 | 21 | 22 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 23 | -------------------------------------------------------------------------------- /linked_list/README.md: -------------------------------------------------------------------------------- 1 | # Linked List 2 | 3 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 4 | 5 | ## index 6 | 1. [singly linked list](./singly_linked_list) 7 | 1. [doubly linked list](./doubly_linked_list) 8 | 1. [circular linked list](./circular_linked_list) 9 | 1. [baekjoon](#baekjoon) 10 | 1. [생각해보기](#생각해보기) 11 | 12 | ## baekjoon 13 | array를 사용하면 더 쉬울 수도 있지만 실습을 위해 linked list를 사용해서 풀어봅시다. 14 | 15 | - [2562번 최댓값](https://www.acmicpc.net/problem/2562) 16 | - [1546번 평균](https://www.acmicpc.net/problem/1546) 17 | 18 | ## 생각해보기 19 | - array와 linked list의 차이점 20 | 21 | 22 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 23 | -------------------------------------------------------------------------------- /stack/README.md: -------------------------------------------------------------------------------- 1 | # Stack 2 | 3 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 4 | 5 | ## index 6 | 1. [stack(array)](./stack_array) 7 | 1. [stack(linked list)](./stack_linked_list) 8 | 1. [baekjoon](#baekjoon) 9 | 1. [생각해보기](#생각해보기) 10 | 11 | ## baekjoon 12 | - [10828번 스택](https://www.acmicpc.net/problem/10828) 13 | - [10773번 제로](https://www.acmicpc.net/problem/10773) 14 | - [9012번 괄호](https://www.acmicpc.net/problem/9012) 15 | - [4949번 균형잡힌 세상](https://www.acmicpc.net/problem/4949) 16 | - [1874번 스택 수열](https://www.acmicpc.net/problem/1874) 17 | 18 | ## 생각해보기 19 | - stack을 array로 구현 했을 때와 linked list로 구현 했을 때의 장단점 20 | - stack과 재귀의 관계 21 | - stack이 사용되는 상황 22 | 23 | 24 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | leaks_result 2 | user_output 3 | *.swp 4 | .DS_Store 5 | 6 | # Prerequisites 7 | *.d 8 | 9 | # Object files 10 | *.o 11 | *.ko 12 | *.obj 13 | *.elf 14 | 15 | # Linker output 16 | *.ilk 17 | *.map 18 | *.exp 19 | 20 | # Precompiled Headers 21 | *.gch 22 | *.pch 23 | 24 | # Libraries 25 | *.lib 26 | *.a 27 | *.la 28 | *.lo 29 | 30 | # Shared objects (inc. Windows DLLs) 31 | *.dll 32 | *.so 33 | *.so.* 34 | *.dylib 35 | 36 | # Executables 37 | *.exe 38 | *.out 39 | *.app 40 | *.i*86 41 | *.x86_64 42 | *.hex 43 | 44 | # Debug files 45 | *.dSYM/ 46 | *.su 47 | *.idb 48 | *.pdb 49 | 50 | # Kernel Module Compile Results 51 | *.mod* 52 | *.cmd 53 | .tmp_versions/ 54 | modules.order 55 | Module.symvers 56 | Mkfile.old 57 | dkms.conf 58 | -------------------------------------------------------------------------------- /hash/rabin_karp/README.md: -------------------------------------------------------------------------------- 1 | # Rabin-Karp 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - rabin-karp 알고리즘을 구현 합니다. 8 | - 다음과 같은 해시함수를 사용합니다. 9 | - `H = S[0] * 302^(l - 1) + S[1] * 302^(l - 2) + ••• + S[l - 2] * 302 + S[l - 1]` 10 | - 값이 너무 커지는 것을 막기 위해 modulo 연산을 활용합니다. 11 | - `number % 1000000007` 12 | 13 | 14 | ### get_hash_value 15 | - 문자열 str의 hash 값을 구하는 함수를 작성하세요. 16 | - str의 길이와 len 중 작은 값을 길이로 사용합니다. 17 | ``` 18 | long long get_hash_value(const char *str, unsigned int len); 19 | ``` 20 | 21 | ### find_str 22 | - 문자열 haystack에서 문자열 needle을 찾는 함수 find_str을 작성하세요. 23 | - 일치하는 문자열을 찾으면 시작 주소를, 찾지 못하면 null pointer를 반환 합니다. 24 | ``` 25 | char *find_str(const char *haystack, const char *needle); 26 | ``` 27 | 28 | 29 | [뒤로 가기](..) 30 | -------------------------------------------------------------------------------- /two_pointers_sliding_window/README.md: -------------------------------------------------------------------------------- 1 | # Two Pointers, Sliding Window 2 | 3 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 4 | 5 | ## 투 포인터 / 슬라이딩 윈도우 6 | - 투 포인터와 슬라이딩 윈도우는 비슷한 개념의 알고리즘인데 꽤나 빈번하게 출제됨 7 | - 시간복잡도 O(N^2)나 O(NlogN)으로 구현하면 예시로 주는 테스트케이스는 통과하는데 효율성에서 터져버리는 함정 문제로 많이 출제되는 것 같음 8 | - 개념만 이해하고 있다면 구현도 어렵지 않으니 공부하길 추천 9 | 10 | ## baekjoon 11 | - [수들의 합 2](https://www.acmicpc.net/problem/2003) 12 | - [주몽](https://www.acmicpc.net/problem/1940) 13 | - [게으른 백곰](https://www.acmicpc.net/problem/10025) 14 | - [수열](https://www.acmicpc.net/problem/2559) 15 | - [DNA 비밀번호](https://www.acmicpc.net/problem/12891) 16 | - [회전 초밥](https://www.acmicpc.net/problem/2531) 17 | - [보석 줍기](https://www.acmicpc.net/problem/2208) 18 | - [보석](https://www.acmicpc.net/problem/2492) 19 | 20 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 42-algorithm 2 | 3 | - 자료구조와 알고리즘을 공부하는 저장소 입니다! 4 | - 오타, 추가, 수정 Issue & PR & Feedback 무조건 환영합니다! 5 | - 질문은 Issue로 올려주시면 최대한 답변해드릴께요 :) 6 | 7 | ## 이론 8 | 1. [linked list](./linked_list) 9 | 1. [stack](./stack) 10 | 1. [queue](./queue) 11 | 1. [deque](./deque) 12 | 1. [tree](./tree) 13 | 1. [sort](./sort) 14 | 1. [hash](./hash) 15 | 1. [graph](./graph) 16 | 17 | ## 실전 18 | 1. [brute force](./brute_force) 19 | 1. [simulation](./simulation) 20 | 1. [two pointers, sliding window](./two_pointers_sliding_window) 21 | 1. dynamic programming(DP) 22 | 1. binary search 23 | 1. shortest path 24 | 1. union-find 25 | 1. topological sort 26 | 1. trie 27 | 28 | ## contribution & feedback 29 | [nadarm](https://github.com/nadarm) 30 | , [365kim](https://github.com/365kim) 31 | , [robolovo](https://github.com/robolovo) 32 | , [exgs](https://github.com/exgs) 33 | -------------------------------------------------------------------------------- /sort/README.md: -------------------------------------------------------------------------------- 1 | # Sort 2 | 3 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 4 | 5 | ## index 6 | 1. [bubble sort](./bubble) 7 | 1. [heap sort](./heap) 8 | 1. [quick sort](./quick) 9 | 1. [merge sort](./merge) 10 | 1. [baekjoon](#baekjoon) 11 | 1. [생각해보기](#생각해보기) 12 | 13 | ## baekjoon 14 | sort를 사용해 풀어봅시다. 15 | - [2750번 수 정렬하기](https://www.acmicpc.net/problem/2750) 16 | - [2751번 수 정렬하기2](https://www.acmicpc.net/problem/2751) 17 | - [10989번 수 정렬하기3](https://www.acmicpc.net/problem/10989) 18 | - [1427번 소트인사이드](https://www.acmicpc.net/problem/1427) 19 | - [1181번 단어 정렬](https://www.acmicpc.net/problem/1181) 20 | - [10814번 나이순 정렬](https://www.acmicpc.net/problem/10814) 21 | 22 | ## 생각해보기 23 | - radix sort, counting sort란? 24 | - 어떤 정렬 알고리즘이 가장 빠를까?(시간 복잡도) 25 | - quick sort가 최악의 시간복잡도를 가지는 경우는? 그리고 그걸 방지하기 위한 방법은? 26 | - stable sort, unstable sort란? 27 | 28 | 29 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 30 | -------------------------------------------------------------------------------- /hash/README.md: -------------------------------------------------------------------------------- 1 | # Hash 2 | 3 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 4 | 5 | ## index 6 | 1. [Rabin-Karp](./rabin_karp) 7 | 1. [hash set](./hash_set) 8 | 1. [hash map(table)](./hash_map) 9 | 1. [baekjoon](#baekjoon) 10 | 1. [생각해보기](#생각해보기) 11 | 12 | ## baekjoon 13 | hash를 사용해 풀어봅시다. 14 | - [15829번 Hashing](https://www.acmicpc.net/problem/15829) 15 | - [7785번 회사에 있는 사람](https://www.acmicpc.net/problem/7785) 16 | - [1920번 수 찾기](https://www.acmicpc.net/problem/1920) 17 | - [14425번 문자열 집합](https://www.acmicpc.net/problem/14425) 18 | - [1764번 듣보잡](https://www.acmicpc.net/problem/1764) 19 | - [5052번 전화번호 목록](https://www.acmicpc.net/problem/5052) 20 | - [1786번 찾기](https://www.acmicpc.net/problem/1786) 21 | 22 | ## 생각해보기 23 | - map(table, dictionary), set 자료구조란? 24 | - hash map, tree map, hash set, tree set 자료구조의 비교 25 | - array, list, map, set 자료구조의 비교 26 | - hash collision을 줄이는 방법 그리고 hash collision 발생 시 해결 방법은? 27 | 28 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 29 | -------------------------------------------------------------------------------- /simulation/README.md: -------------------------------------------------------------------------------- 1 | # Simulation 2 | 3 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 4 | 5 | ## 시뮬레이션 / 구현 6 | - 문제에서 말하는데로 잘 구현하는 것 7 | - 문제를 읽고 제대로 이해하는 것이 매우 중요함 8 | - 완전탐색과 함께 코테의 양대산맥 9 | - 실제로 완전탐색과 시뮬레이션 유형만 다 풀어도 합격하는 경우가 꽤 있음 10 | - 프로그래밍 언어에 익숙하고 코딩을 많이 해봤다면 어느정도 쉽게 풀 수 있음 11 | - 문자열을 다루는 문제도 많이 나옴 12 | - 그래서 문자열을 편하게 다룰 수 있는 파이썬 같은 언어가 유리함 13 | 14 | ## baekjoon 15 | - [지능형 기차 2](https://www.acmicpc.net/problem/2460) 16 | - [나무조각](https://www.acmicpc.net/problem/2947) 17 | - [요세푸스 문제 2](https://www.acmicpc.net/problem/1168) 18 | - [후보 추천하기](https://www.acmicpc.net/problem/1713) 19 | - [프린터 큐](https://www.acmicpc.net/problem/1966) 20 | - [로봇 시뮬레이션](https://www.acmicpc.net/problem/2174) 21 | - [삼성 SW 역량 테스트 기출 문제](https://www.acmicpc.net/workbook/view/1152) 22 | - 초보자라면 2~3문제정도 읽어보고 '이런게 나오는 구나'하고, 나중에 다시 풀어보는 것을 추천 23 | - 완전탐색, 백트래킹, 시뮬레이션 등 몇가지 유형이 섞여서 출제 24 | - 구현해야하는 코드의 양이 많아 실수를 줄이는 것이 중요함 25 | - 삼성 코테를 준비한다면 당연히 다 풀어보는게 좋음 26 | 27 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 28 | -------------------------------------------------------------------------------- /linked_list/circular_linked_list/grademe.sh: -------------------------------------------------------------------------------- 1 | subject_output=circular_linked_list_output 2 | 3 | 4 | dir=`dirname $0` 5 | 6 | if [ -e ./a.out ] ; then 7 | rm -rf a.out a.out.dSYM 8 | fi 9 | if [ -e ./user_output ] ; then 10 | rm user_output 11 | fi 12 | if [ -e ./leaks_result ] ; then 13 | rm leaks_result 14 | fi 15 | gcc -Wall -Wextra -Werror -g *.c $dir/grademe_files/test.c -I. 16 | 17 | if [ ! -e ./a.out ] ; then 18 | echo "컴파일 에러" 19 | exit 1 20 | fi 21 | ./a.out > user_output 22 | 23 | echo "============ check diff ================" 24 | if [ ! -e $dir/grademe_files/$subject_output ] ; then 25 | echo "$subject_output 파일이 없습니다." 26 | else 27 | diff -u user_output $dir/grademe_files/$subject_output | less 28 | fi 29 | 30 | echo "============ check leaks ===============" 31 | if [ ! -e ./leaks_result ] ; then 32 | echo "leaks check 결과 파일이 없습니다" 33 | else 34 | cat leaks_result | grep leaked 35 | fi 36 | if [ -e ./a.out ] ; then 37 | rm -rf a.out a.out.dSYM 38 | fi 39 | echo "================ end ===================" 40 | -------------------------------------------------------------------------------- /tree/trie/README.md: -------------------------------------------------------------------------------- 1 | # Trie 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - trie을 구현 합니다. 8 | - 아래와 같은 trie.h를 사용 합니다. 9 | ``` 10 | #include 11 | 12 | typedef struct s_node 13 | { 14 | struct s_node *next[26]; 15 | bool finish; 16 | } t_node; 17 | 18 | typedef t_node *t_trie[26]; 19 | ``` 20 | 21 | ### trie_init 22 | - t_trie형 struct를 반환 하는 함수를 작성하세요. 23 | - 반환되는 t_trie는 메모리 할당과 초기화를 거쳐야합니다. 24 | ``` 25 | t_trie *trie_init(void); 26 | ``` 27 | 28 | ### trie_insert 29 | - trie에 문자열 str을 삽입하는 함수를 작성하세요. 30 | - str은 a ~ z만으로 이루어진 문자열 입니다. 31 | - 성공하면 true, 실패하면 false를 반환 합니다. 32 | ``` 33 | bool trie_insert(t_trie *trie, char *str); 34 | ``` 35 | 36 | ### trie_find 37 | - trie에서 문자열 str이 있는지 검색하는 함수를 작성하세요. 38 | - str은 a ~ z만으로 이루어진 문자열 입니다. 39 | - str이 존재하면 true, 없으면 false를 반환 합니다. 40 | ``` 41 | bool trie_find(t_trie *trie, char *str); 42 | ``` 43 | 44 | ### free_trie 45 | - trie에 있는 요소 전체를 삭제하고 trie의 메모리 할당을 해제하는 함수를 작성하세요. 46 | ``` 47 | void free_trie(t_trie *trie); 48 | ``` 49 | 50 | 51 | [뒤로 가기](..) 52 | -------------------------------------------------------------------------------- /linked_list/doubly_linked_list/grademe.sh: -------------------------------------------------------------------------------- 1 | dir=`dirname $0` 2 | 3 | if [ -e ./a.out ] ; then 4 | rm a.out 5 | fi 6 | if [ -e ./user_output ] ; then 7 | rm user_output 8 | fi 9 | if [ -e ./leaks_result ] ; then 10 | rm leaks_result 11 | fi 12 | gcc -Wall -Wextra -Werror -g *.c $dir/grademe_files/test.c -I. 13 | 14 | if [ ! -e ./a.out ] ; then 15 | echo "컴파일 에러" 16 | exit 1 17 | fi 18 | ./a.out > user_output 19 | 20 | echo "============ check diff ================" 21 | if [ ! -e $dir/grademe_files/doubly_linked_list_output ] ; then 22 | echo "doubly_linked_list_output 파일이 없습니다." 23 | else 24 | diff user_output $dir/grademe_files/doubly_linked_list_output 25 | fi 26 | 27 | echo "============ check leaks ===============" 28 | if [ ! -e ./leaks_result ] ; then 29 | echo "leaks check 결과 파일이 없습니다" 30 | else 31 | cat leaks_result | grep leaked 32 | fi 33 | if [ -e ./a.out ] ; then 34 | rm a.out 35 | fi 36 | if [ -e ./user_output ] ; then 37 | rm user_output 38 | fi 39 | if [ -e ./leaks_result ] ; then 40 | rm leaks_result 41 | fi 42 | echo "================ end ===================" 43 | -------------------------------------------------------------------------------- /tree/union_find/README.md: -------------------------------------------------------------------------------- 1 | # Union-Find 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - union-find를 구현 합니다. 7 | - allowed functions : malloc, free 8 | - 아래와 같은 union_find.h를 사용 합니다. 9 | ``` 10 | #include 11 | 12 | typedef struct s_node 13 | { 14 | struct s_node *parent; 15 | void *data; 16 | } t_node; 17 | ``` 18 | 19 | ### create_elem 20 | - t_node형 새로운 요소를 생성하는 함수를 작성하세요. 21 | ``` 22 | t_node *create_elem(void *data); 23 | ``` 24 | 25 | ### find 26 | - 주어진 node가 속한 집합의 root node를 반환하는 함수를 작성하세요. 27 | ``` 28 | t_node *find(t_node *node); 29 | ``` 30 | 31 | ### is_disjoint 32 | - 두 node가 서로소 집합인지 확인하는 함수를 작성하세요. 33 | - 두 node가 같은 집합에 속해 있다면 false, 아니라면 true를 반환 합니다. 34 | ``` 35 | bool is_disjoint(t_node *node1, t_node *node2); 36 | ``` 37 | 38 | ### union 39 | - 두 node가 속해 있는 집합을 하나로 합하는 함수를 작성하세요. 40 | - 집합을 합한 뒤, 집합의 root node를 반환 합니다. 41 | - 두 집합을 합할 땐, node1의 root node가 두 집합의 root node가 됩니다. 42 | - 두 node가 이미 같은 집합에 속해 있더라도 집합의 root node를 반환합니다. 43 | ``` 44 | t_node *union_func(t_node *node1, t_node *node2); 45 | ``` 46 | 47 | [뒤로 가기](..) 48 | -------------------------------------------------------------------------------- /sort/bubble/implementation/bubble.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* bubble.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/14 00:07:47 by jko #+# #+# */ 9 | /* Updated: 2020/04/14 00:08:11 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef BUBBLE_H 14 | # define BUBBLE_H 15 | 16 | void bubble_sort(void **items, int size, int (*cmp)(void *, void *)); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /sort/merge/implementation/merge.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* merge.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/16 14:19:42 by jko #+# #+# */ 9 | /* Updated: 2020/04/16 14:19:57 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef MERGE_H 14 | # define MERGE_H 15 | 16 | # include 17 | 18 | int merge_sort(void **items, int size, int (*cmp)(void *, void *)); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /sort/quick/implementation/quick.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* quick.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/14 17:54:24 by jko #+# #+# */ 9 | /* Updated: 2020/04/15 23:08:35 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef QUICK_H 14 | # define QUICK_H 15 | 16 | # include 17 | 18 | int quick_sort(void **items, int size, int (*cmp)(void *, void *)); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /graph/floyd-warshall/implementation/fw.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* fw.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/06/09 12:12:00 by jko #+# #+# */ 9 | /* Updated: 2020/06/09 12:12:27 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef FW_H 14 | # define FW_H 15 | 16 | #include "graph.h" 17 | #include 18 | 19 | int **find_shortest_path(const t_graph *src); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /graph/dijkstra/README.md: -------------------------------------------------------------------------------- 1 | # Dijkstra 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - Dijkstra 알고리즘을 구현 합니다. 8 | - 아래와 같은 weighted directed graph(adjacency list)를 사용합니다. 9 | ``` 10 | typedef unsigned int uint; 11 | 12 | typedef struct s_node 13 | { 14 | unsigned int vertex; 15 | int cost; 16 | struct s_node *next; 17 | } t_node; 18 | 19 | typedef struct s_graph 20 | { 21 | unsigned int size; 22 | void **data; 23 | t_node **list; 24 | } t_graph; 25 | 26 | t_graph *graph_init(uint size); 27 | bool graph_set_data(t_graph *graph, uint vertex, void *data); 28 | bool graph_set_edge(t_graph *graph, uint start, uint end, bool state, int cost); 29 | void free_graph(t_graph *graph, void (*free_data)(void *)); 30 | ``` 31 | 32 | ### find_shortest_path 33 | - src의 start번 정점에서 각 정점까지의 최단 거리를 구하는 함수를 작성하세요. 34 | - Dijkstra 알고리즘을 이용하여 최단 거리를 찾습니다. 35 | - 반환되는 int *는 start번 정점에서 각 정점까지의 최단 거리 값을 가진 int 배열 입니다. 36 | - 실패 시 null pointer를 반환합니다. 37 | ``` 38 | unsigned int *find_shortest_path(const t_graph *src, unsigned int start); 39 | ``` 40 | 41 | 42 | [뒤로 가기](..) 43 | -------------------------------------------------------------------------------- /tree/README.md: -------------------------------------------------------------------------------- 1 | # Tree 2 | 3 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 4 | 5 | ## index 6 | 1. [binary search tree](./binary_search_tree) 7 | 1. [heap(priority queue)](./heap) 8 | 1. [trie(prefix tree)](./trie) 9 | 1. [union-find(disjoint-set)](./union_find) 10 | 1. [baekjoon](#baekjoon) 11 | 1. [생각해보기](#생각해보기) 12 | 13 | ## baekjoon 14 | tree를 사용해 풀어봅시다. 15 | - [1920번 수 찾기](https://www.acmicpc.net/problem/1920) 16 | - [5639번 이진 검색 트리](https://www.acmicpc.net/problem/5639) 17 | - [1991번 트리 순회](https://www.acmicpc.net/problem/1991) 18 | - [1927번 최소 힙](https://www.acmicpc.net/problem/1927) 19 | - [11279번 최대 힙](https://www.acmicpc.net/problem/11279) 20 | - [11286번 절댓값 힙](https://www.acmicpc.net/problem/11286) 21 | - [14425번 문자열 집합](https://www.acmicpc.net/problem/14425) 22 | - [4358번 생태학](https://www.acmicpc.net/problem/4358) 23 | - [5052번 전화번호 목록](https://www.acmicpc.net/problem/5052) 24 | - [1717번 집합의 표현](https://www.acmicpc.net/problem/1717) 25 | - [4195번 친구 네트워크](https://www.acmicpc.net/problem/4195) 26 | 27 | 28 | ## 생각해보기 29 | - tree가 사용되는 상황 30 | - array로 tree를 구현하려면? 31 | - 상태공간트리란? 32 | 33 | 34 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 35 | -------------------------------------------------------------------------------- /graph/floyd-warshall/README.md: -------------------------------------------------------------------------------- 1 | # Floyd-Warshall 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - Floyd-Warshall 알고리즘을 구현 합니다. 8 | - 아래와 같은 weighted directed graph(adjacency list)를 사용합니다. 9 | ``` 10 | typedef unsigned int uint; 11 | 12 | typedef struct s_node 13 | { 14 | unsigned int vertex; 15 | int cost; 16 | struct s_node *next; 17 | } t_node; 18 | 19 | typedef struct s_graph 20 | { 21 | unsigned int size; 22 | void **data; 23 | t_node **list; 24 | } t_graph; 25 | 26 | t_graph *graph_init(uint size); 27 | bool graph_set_data(t_graph *graph, uint vertex, void *data); 28 | bool graph_set_edge(t_graph *graph, uint start, uint end, bool state, int cost); 29 | void free_graph(t_graph *graph, void (*free_data)(void *)); 30 | ``` 31 | 32 | ### find_shortest_path 33 | - src의 모든 정점에서 모든 정점까지의 최소 비용 경로를 구하는 함수를 작성하세요. 34 | - Floyd-Warshall 알고리즘을 이용하여 최소 비용 경로를 찾습니다. 35 | - src의 edge에 음수 가중치가 있을 수 있습니다. 36 | - 성공 시 모든 정점에서 모든 정점까지의 경로의 최소 비용을 가진 2차원 int 배열을 반환합니다. 37 | - 실패 시 null-pointer를 반환합니다. 38 | ``` 39 | int **find_shortest_path(const t_graph *src); 40 | ``` 41 | 42 | 43 | [뒤로 가기](..) 44 | -------------------------------------------------------------------------------- /graph/prim/README.md: -------------------------------------------------------------------------------- 1 | # Prim 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - Prim 알고리즘을 구현 합니다. 8 | - 아래와 같은 weighted undirected graph(adjacency list)를 사용합니다. 9 | ``` 10 | typedef unsigned int uint; 11 | 12 | typedef struct s_node 13 | { 14 | unsigned int vertex; 15 | int cost; 16 | struct s_node *next; 17 | } t_node; 18 | 19 | typedef struct s_graph 20 | { 21 | unsigned int size; 22 | void **data; 23 | t_node **list; 24 | } t_graph; 25 | 26 | t_graph *graph_init(uint size); 27 | bool graph_set_data(t_graph *graph, uint vertex, void *data); 28 | void *graph_get_data(t_graph *graph, uint vertex); 29 | bool graph_set_edge(t_graph *graph,uint vertex1, uint vertex2, bool state, int cost); 30 | bool graph_get_edge(t_graph *graph,uint vertex1, uint vertex2, int *cost); 31 | void free_graph(t_graph *graph, void (*free_data)(void *)); 32 | ``` 33 | 34 | ### make_mst 35 | - src의 minimum spanning tree를 만들어 반환하는 함수를 작성하세요. 36 | - Prim algorithm을 이용하여 MST를 찾습니다. 37 | - 반환되는 t_graph는 새로 메모리를 할당하여 MST를 이루는데 필요한 edge만을 연결한 것입니다.(data는 복사하지 않습니다.) 38 | - 실패 시 null pointer를 반환합니다. 39 | ``` 40 | t_graph *make_mst(const t_graph *src); 41 | ``` 42 | 43 | 44 | [뒤로 가기](..) 45 | -------------------------------------------------------------------------------- /graph/prim/implementation/prim.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* prim.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/26 16:56:40 by jko #+# #+# */ 9 | /* Updated: 2020/04/26 16:56:54 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef PRIM_H 14 | # define PRIM_H 15 | 16 | #include "graph.h" 17 | #include "heap.h" 18 | 19 | typedef struct s_edge 20 | { 21 | uint start; 22 | uint end; 23 | int cost; 24 | } t_edge; 25 | 26 | t_graph *make_mst(const t_graph *src); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /graph/bellman-ford/implementation/bf.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* bf.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/06/07 16:12:27 by jko #+# #+# */ 9 | /* Updated: 2020/06/08 21:57:04 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef BF_H 14 | # define BF_H 15 | 16 | # define FAILURE 0 17 | # define SUCCESS 1 18 | # define NEGATIVE_CYCLE 2 19 | 20 | #include "graph.h" 21 | #include 22 | 23 | int find_shortest_path(const t_graph *src, uint start, int **result); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /hash/rabin_karp/implementation/rabin_karp.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* rabin_karp.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/18 14:32:09 by jko #+# #+# */ 9 | /* Updated: 2020/04/19 15:39:02 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef RABIN_KARP_H 14 | # define RABIN_KARP_H 15 | 16 | # define POWER 302 17 | # define BIG_PRIM 1000000007 18 | 19 | long long get_hash_value(const char *str, unsigned int len); 20 | char *find_str(const char *haystack, const char *needle); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /graph/kruskal/README.md: -------------------------------------------------------------------------------- 1 | # Kruskal 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - Kruskal 알고리즘을 구현 합니다. 8 | - 아래와 같은 weighted undirected graph(adjacency list)를 사용합니다. 9 | ``` 10 | typedef unsigned int uint; 11 | 12 | typedef struct s_node 13 | { 14 | unsigned int vertex; 15 | int cost; 16 | struct s_node *next; 17 | } t_node; 18 | 19 | typedef struct s_graph 20 | { 21 | unsigned int size; 22 | void **data; 23 | t_node **list; 24 | } t_graph; 25 | 26 | t_graph *graph_init(uint size); 27 | bool graph_set_data(t_graph *graph, uint vertex, void *data); 28 | void *graph_get_data(t_graph *graph, uint vertex); 29 | bool graph_set_edge(t_graph *graph,uint vertex1, uint vertex2, bool state, int cost); 30 | bool graph_get_edge(t_graph *graph,uint vertex1, uint vertex2, int *cost); 31 | void free_graph(t_graph *graph, void (*free_data)(void *)); 32 | ``` 33 | 34 | ### make_mst 35 | - src의 minimum spanning tree를 만들어 반환하는 함수를 작성하세요. 36 | - Kruskal algorithm을 이용하여 MST를 찾습니다. 37 | - 반환되는 t_graph는 새로 메모리를 할당하여 MST를 이루는데 필요한 edge만을 연결한 것입니다.(data는 복사하지 않습니다.) 38 | - 실패 시 null pointer를 반환합니다. 39 | ``` 40 | t_graph *make_mst(const t_graph *src); 41 | ``` 42 | 43 | 44 | [뒤로 가기](..) 45 | -------------------------------------------------------------------------------- /graph/kruskal/implementation/kruskal.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* kruskal.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/24 22:36:41 by jko #+# #+# */ 9 | /* Updated: 2020/04/25 14:47:11 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef KRUSKAL_H 14 | # define KRUSKAL_H 15 | 16 | #include "graph.h" 17 | #include "heap.h" 18 | 19 | typedef struct s_edge 20 | { 21 | uint start; 22 | uint end; 23 | int cost; 24 | } t_edge; 25 | 26 | t_graph *make_mst(const t_graph *src); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /graph/dijkstra/implementation/dijkstra.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* dijkstra.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/06/05 18:23:45 by jko #+# #+# */ 9 | /* Updated: 2020/06/06 13:03:17 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef DIJKSTRA_H 14 | # define DIJKSTRA_H 15 | 16 | #include "graph.h" 17 | #include "heap.h" 18 | #include 19 | 20 | typedef struct s_path 21 | { 22 | uint vertex; 23 | uint cost; 24 | } t_path; 25 | 26 | uint *find_shortest_path(const t_graph *src, uint start); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /brute_force/README.md: -------------------------------------------------------------------------------- 1 | # Brute force 2 | 3 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 4 | 5 | ## 완전탐색 6 | - `모든 경우`를 확인해보는 것 7 | - 코딩 테스트에 매우 자주 출제되는 유형 8 | - `N과 M` 시리즈 같은 구현이 간단한 문제부터 `삼성 코테식(완전탐색+시뮬레이션)` 등 난이도가 굉장히 다양하게 출제 됨 9 | - 트리 구조(의사결정 트리)를 잘 이해하고 있다면 매우 도움이 됨 10 | 11 | ## 백트래킹 12 | - 완전탐색(DFS)에서 `가지치기`를 통해 업그레이드된 버전 13 | - 완전탐색의 변형인만큼 구현은 거의 같지만 상황에 따라 성능 차이가 클 수 있음 14 | 15 | ## baekjoon 16 | - [N과 M 시리즈](https://www.acmicpc.net/workbook/view/2052) 17 | - 완전탐색의 기초이자 필수, 라이브러리 없이 직접 구현으로 전부 다 풀어보는 것을 추천 18 | - N과 M이 작은 몇가지 경우에 대해 직접 트리를 그려보면 이해하기 좋음 19 | - [DFS와 BFS](https://www.acmicpc.net/problem/1260) 20 | - [바이러스](https://www.acmicpc.net/problem/2606) 21 | - [미로 탐색](https://www.acmicpc.net/problem/2178) 22 | - [토마토](https://www.acmicpc.net/problem/7569) 23 | - [안전 영역](https://www.acmicpc.net/problem/2468) 24 | - [N-Queen](https://www.acmicpc.net/problem/9663) 25 | - `백트래킹의 정석`으로 널리 알려진 문제 26 | - [스도쿠](https://www.acmicpc.net/problem/2580) 27 | - [삼성 SW 역량 테스트 기출 문제](https://www.acmicpc.net/workbook/view/1152) 28 | - 초보자라면 2~3문제정도 읽어보고 '이런게 나오는 구나'하고, 나중에 다시 풀어보는 것을 추천 29 | - 완전탐색, 백트래킹, 시뮬레이션 등 몇가지 유형이 섞여서 출제 30 | - 구현해야하는 코드의 양이 많아 실수를 줄이는 것이 중요함 31 | - 삼성 코테를 준비한다면 당연히 다 풀어보는게 좋음 32 | 33 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 34 | -------------------------------------------------------------------------------- /graph/bellman-ford/README.md: -------------------------------------------------------------------------------- 1 | # Bellman-Ford 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - Bellman-Ford 알고리즘을 구현 합니다. 8 | - 아래와 같은 weighted directed graph(adjacency list)를 사용합니다. 9 | ``` 10 | typedef unsigned int uint; 11 | 12 | typedef struct s_node 13 | { 14 | unsigned int vertex; 15 | int cost; 16 | struct s_node *next; 17 | } t_node; 18 | 19 | typedef struct s_graph 20 | { 21 | unsigned int size; 22 | void **data; 23 | t_node **list; 24 | } t_graph; 25 | 26 | t_graph *graph_init(uint size); 27 | bool graph_set_data(t_graph *graph, uint vertex, void *data); 28 | bool graph_set_edge(t_graph *graph, uint start, uint end, bool state, int cost); 29 | void free_graph(t_graph *graph, void (*free_data)(void *)); 30 | ``` 31 | 32 | ### find_shortest_path 33 | - src의 start번 정점에서 각 정점까지의 최소 비용 경로를 구하는 함수를 작성하세요. 34 | - Bellman-Ford 알고리즘을 이용하여 최소 비용 경로를 찾습니다. 35 | - src의 edge에 음수 가중치가 있을 수 있습니다. 36 | - 성공 시 result에는 start번 정점에서 각 정점까지의 최소 비용을 가진 int 배열의 포인터를 할당합니다. 37 | - 그 외의 경우엔 result에 null-pointer를 할당합니다. 38 | - 실패 시 0을 반환합니다. 39 | - 성공 시 1을 반환합니다. 40 | - 비용이 무한이 작아지는 경우엔 2를 반환합니다. 41 | ``` 42 | int find_shortest_path(const t_graph *src, unsigned int start, int **result); 43 | ``` 44 | 45 | 46 | [뒤로 가기](..) 47 | -------------------------------------------------------------------------------- /tree/heap/README.md: -------------------------------------------------------------------------------- 1 | # Heap 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - heap을 구현 합니다. 8 | - 아래와 같은 heap.h를 사용 합니다. 9 | ``` 10 | typedef struct s_heap 11 | { 12 | unsigned int max_size; 13 | unsigned int size; 14 | int (*cmp)(void *, void *); 15 | void **data; 16 | } t_heap; 17 | ``` 18 | 19 | ### heap_init 20 | - t_heap형 struct를 반환 하는 함수를 작성하세요. 21 | - 반환되는 t_heap는 메모리 할당과 초기화를 거쳐야합니다. 22 | - cmp는 t_node의 data를 비교하는데 사용 됩니다. 첫번째 인자가 더 크면 양수, 작으면 음수, 같다면 0을 반환 합니다. 23 | ``` 24 | t_heap *heap_init(unsigned int max_size, int (*cmp)(void *, void *)); 25 | ``` 26 | 27 | ### heap_push 28 | - heap에 data를 삽입하는 함수를 작성하세요. 29 | - data는 부모의 data보다 크거나 같아야 합니다.(min heap) 30 | - 성공하면 1, 실패하면 0을 반환 합니다. 31 | ``` 32 | int heap_push(t_heap *heap, void *data); 33 | ``` 34 | 35 | ### heap_peek 36 | - heap의 root에 있는 data를 반환하는 함수를 작성하세요. 37 | ``` 38 | void *heap_peek(t_heap *heap); 39 | ``` 40 | 41 | ### heap_pop 42 | - heap의 root에 있는 data를 꺼내는 함수를 작성하세요. 43 | ``` 44 | void *heap_pop(t_heap *heap); 45 | ``` 46 | 47 | ### free_heap 48 | - heap에 있는 data 전체를 삭제하고 heap의 메모리 할당을 해제하는 함수를 작성하세요. 49 | - data는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 50 | ``` 51 | void free_heap(t_heap *heap, void (*free_data)(void *)); 52 | ``` 53 | 54 | 55 | [뒤로 가기](..) 56 | -------------------------------------------------------------------------------- /sort/heap/implementation/heap.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* heap.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/14 17:05:32 by jko #+# #+# */ 9 | /* Updated: 2020/04/14 17:20:27 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef HEAP_H 14 | # define HEAP_H 15 | 16 | # include 17 | 18 | typedef struct s_heap 19 | { 20 | unsigned int max_size; 21 | unsigned int size; 22 | int (*cmp)(void *, void *); 23 | void **data; 24 | } t_heap; 25 | 26 | int heap_sort( 27 | void **items, 28 | int size, 29 | int (*cmp)(void *, void *)); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /sort/bubble/implementation/bubble.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* bubble.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/14 00:07:02 by jko #+# #+# */ 9 | /* Updated: 2020/04/14 00:12:58 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | void bubble_sort(void **items, int size, int (*cmp)(void *, void *)) 14 | { 15 | int i; 16 | int j; 17 | void *temp; 18 | 19 | i = 0; 20 | while (i < size - 1) 21 | { 22 | j = 0; 23 | while (j < size - i - 1) 24 | { 25 | if (cmp(items[j], items[j + 1]) > 0) 26 | { 27 | temp = items[j]; 28 | items[j] = items[j + 1]; 29 | items[j + 1] = temp; 30 | } 31 | j++; 32 | } 33 | i++; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tree/trie/implementation_example/trie.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* trie.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/12 16:21:10 by jko #+# #+# */ 9 | /* Updated: 2020/04/12 17:22:31 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef TRIE_H 14 | # define TRIE_H 15 | 16 | # include 17 | # include 18 | 19 | typedef struct s_node 20 | { 21 | struct s_node *next[26]; 22 | bool finish; 23 | } t_node; 24 | 25 | typedef t_node *t_trie[26]; 26 | 27 | t_trie *trie_init(void); 28 | bool trie_insert(t_trie *trie, char *str); 29 | bool trie_find(t_trie *trie, char *str); 30 | void free_trie(t_trie *trie); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /tree/union_find/implementation_example/union_find.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* union_find.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/12 22:00:14 by jko #+# #+# */ 9 | /* Updated: 2020/04/13 17:15:59 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef UNION_FIND_H 14 | # define UNION_FIND_H 15 | 16 | # include 17 | # include 18 | 19 | typedef struct s_node 20 | { 21 | struct s_node *parent; 22 | void *data; 23 | } t_node; 24 | 25 | t_node *create_elem(void *data); 26 | t_node *find(t_node *node); 27 | bool is_disjoint(t_node *node1, t_node *node2); 28 | t_node *union_func(t_node *node1, t_node *node2); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /graph/prim/implementation/heap.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* heap.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/09 15:38:49 by jko #+# #+# */ 9 | /* Updated: 2020/04/14 17:04:56 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef HEAP_H 14 | # define HEAP_H 15 | 16 | # include 17 | 18 | typedef struct s_heap 19 | { 20 | unsigned int max_size; 21 | unsigned int size; 22 | int (*cmp)(void *, void *); 23 | void **data; 24 | } t_heap; 25 | 26 | t_heap *heap_init(unsigned int max_size, int (*cmp)(void *, void *)); 27 | int heap_push(t_heap *heap, void *data); 28 | void *heap_peek(t_heap *heap); 29 | void *heap_pop(t_heap *heap); 30 | void free_heap(t_heap *heap, void (*free_data)(void *)); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /graph/dijkstra/implementation/heap.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* heap.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/09 15:38:49 by jko #+# #+# */ 9 | /* Updated: 2020/04/14 17:04:56 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef HEAP_H 14 | # define HEAP_H 15 | 16 | # include 17 | 18 | typedef struct s_heap 19 | { 20 | unsigned int max_size; 21 | unsigned int size; 22 | int (*cmp)(void *, void *); 23 | void **data; 24 | } t_heap; 25 | 26 | t_heap *heap_init(unsigned int max_size, int (*cmp)(void *, void *)); 27 | int heap_push(t_heap *heap, void *data); 28 | void *heap_peek(t_heap *heap); 29 | void *heap_pop(t_heap *heap); 30 | void free_heap(t_heap *heap, void (*free_data)(void *)); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /graph/kruskal/implementation/heap.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* heap.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/09 15:38:49 by jko #+# #+# */ 9 | /* Updated: 2020/04/14 17:04:56 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef HEAP_H 14 | # define HEAP_H 15 | 16 | # include 17 | 18 | typedef struct s_heap 19 | { 20 | unsigned int max_size; 21 | unsigned int size; 22 | int (*cmp)(void *, void *); 23 | void **data; 24 | } t_heap; 25 | 26 | t_heap *heap_init(unsigned int max_size, int (*cmp)(void *, void *)); 27 | int heap_push(t_heap *heap, void *data); 28 | void *heap_peek(t_heap *heap); 29 | void *heap_pop(t_heap *heap); 30 | void free_heap(t_heap *heap, void (*free_data)(void *)); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /tree/heap/implementation_example/heap.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* heap.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/09 15:38:49 by jko #+# #+# */ 9 | /* Updated: 2020/04/14 17:04:56 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef HEAP_H 14 | # define HEAP_H 15 | 16 | # include 17 | 18 | typedef struct s_heap 19 | { 20 | unsigned int max_size; 21 | unsigned int size; 22 | int (*cmp)(void *, void *); 23 | void **data; 24 | } t_heap; 25 | 26 | t_heap *heap_init(unsigned int max_size, int (*cmp)(void *, void *)); 27 | int heap_push(t_heap *heap, void *data); 28 | void *heap_peek(t_heap *heap); 29 | void *heap_pop(t_heap *heap); 30 | void free_heap(t_heap *heap, void (*free_data)(void *)); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /queue/queue_array/README.md: -------------------------------------------------------------------------------- 1 | # Queue(Array) 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - array를 사용하여 queue을 구현 합니다. 8 | - 아래와 같은 queue.h를 사용 합니다. 9 | ``` 10 | typedef struct s_queue 11 | { 12 | unsigned int max_size; 13 | int last_index; 14 | void **data; 15 | } t_queue; 16 | ``` 17 | 18 | ### queue_init 19 | - t_queue형 struct를 반환 하는 함수를 작성하세요. 20 | - 반환되는 t_queue는 메모리 할당과 초기화를 거쳐야합니다. 21 | ``` 22 | t_queue *queue_init(unsigned int max_size); 23 | ``` 24 | 25 | ### queue_size 26 | - queue에 있는 요소의 개수를 반환하는 함수를 작성하세요. 27 | ``` 28 | int queue_size(t_queue *queue); 29 | ``` 30 | 31 | ### queue_push 32 | - queue의 가장 뒤에 data를 추가하는 함수를 작성하세요. 33 | - 성공하면 1, 실패하면 0을 반환 합니다. 34 | ``` 35 | int queue_push(t_queue *queue, void *data); 36 | ``` 37 | 38 | ### queue_front 39 | - queue에서 가장 앞에 있는 data를 반환하는 함수를 작성하세요. 40 | ``` 41 | void *queue_front(t_queue *queue); 42 | ``` 43 | 44 | ### queue_pop 45 | - queue에서 가장 앞에 있는 data를 꺼내는 함수를 작성하세요. 46 | ``` 47 | void *queue_pop(t_queue *queue); 48 | ``` 49 | 50 | ### queue_clear 51 | - queue의 data 전체를 삭제하는 함수를 작성하세요. 52 | - data는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 53 | ``` 54 | void queue_clear(t_queue *queue, void (*free_data)(void *)); 55 | ``` 56 | 57 | ### free_queue 58 | - queue에 있는 data 전체를 삭제하고 queue의 메모리 할당을 해제하는 함수를 작성하세요. 59 | - 요소의 데이터는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 60 | ``` 61 | void free_queue(t_queue *queue, void (*free_data)(void *)); 62 | ``` 63 | 64 | 65 | [뒤로 가기](..) 66 | -------------------------------------------------------------------------------- /stack/stack_array/README.md: -------------------------------------------------------------------------------- 1 | # Stack(Array) 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - array를 사용하여 stack을 구현 합니다. 8 | - 아래와 같은 stack.h를 사용 합니다. 9 | ``` 10 | typedef struct s_stack 11 | { 12 | unsigned int max_size; 13 | int top_index; 14 | void **data; 15 | } t_stack; 16 | ``` 17 | 18 | ### stack_init 19 | - t_stack형 struct를 반환 하는 함수를 작성하세요. 20 | - 반환되는 t_stack는 메모리 할당과 초기화를 거쳐야합니다. 21 | ``` 22 | t_stack *stack_init(unsigned int max_size); 23 | ``` 24 | 25 | ### stack_push 26 | - stack의 가장 위에 data를 추가하는 함수를 작성하세요. 27 | - 성공하면 1, 실패하면 0을 반환 합니다. 28 | ``` 29 | int stack_push(t_stack *stack, void *data); 30 | ``` 31 | 32 | ### stack_size 33 | - stack에 있는 요소의 개수를 반환하는 함수를 작성하세요. 34 | ``` 35 | int stack_size(t_stack *stack); 36 | ``` 37 | 38 | ### stack_peek 39 | - stack에서 가장 위에 있는 data를 반환하는 함수를 작성하세요. 40 | ``` 41 | void *stack_peek(t_stack *stack); 42 | ``` 43 | 44 | ### stack_pop 45 | - stack에서 가장 위에 있는 data를 꺼내는 함수를 작성하세요. 46 | ``` 47 | void *stack_pop(t_stack *stack); 48 | ``` 49 | 50 | ### stack_clear 51 | - stack의 data 전체를 삭제하는 함수를 작성하세요. 52 | - data는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 53 | ``` 54 | void stack_clear(t_stack *stack, void (*free_data)(void *)); 55 | ``` 56 | 57 | ### free_stack 58 | - stack에 있는 data 전체를 삭제하고 stack의 메모리 할당을 해제하는 함수를 작성하세요. 59 | - 요소의 데이터는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 60 | ``` 61 | void free_stack(t_stack *stack, void (*free_data)(void *)); 62 | ``` 63 | 64 | 65 | [뒤로 가기](..) 66 | -------------------------------------------------------------------------------- /linked_list/singly_linked_list/README.md: -------------------------------------------------------------------------------- 1 | # Singly Linked List 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - 아래와 같은 list.h를 사용 합니다. 8 | ``` 9 | typedef struct s_node 10 | { 11 | int data; 12 | struct s_node *next; 13 | } t_node; 14 | 15 | ``` 16 | 17 | ### create_elem 18 | - t_node형 새로운 요소를 생성하는 함수를 작성하세요. 19 | ``` 20 | t_node *create_elem(int data); 21 | ``` 22 | 23 | ### list_add1 24 | - t_node형 새로운 요소를 목록의 맨 뒤에 추가하는 함수를 작성하세요. 25 | - 성공 시 인덱스 번호를 반환 합니다.(0부터 시작) 26 | - 실패 시 음수를 반환 합니다. 27 | ``` 28 | int list_add1(t_node **begin_list, int data); 29 | ``` 30 | 31 | ### list_size 32 | - 목록에 있는 요소의 개수를 반환하는 함수를 작성하세요. 33 | ``` 34 | int list_size(t_node *begin_list); 35 | ``` 36 | 37 | ### list_get 38 | - 목록에서 n번 인덱스의 요소를 반환하는 함수를 작성하세요. 39 | - 목록에 있는 요소의 수가 더 적을 땐, 널포인터를 반환 합니다. 40 | ``` 41 | t_node *list_get(t_node *begin_list, int n); 42 | ``` 43 | 44 | ### list_find 45 | - 목록에서 data의 값이 같은 요소의 인덱스를 반환하는 함수를 작성하세요. 46 | - 없을 경우엔 음수를 반환 합니다. 47 | ``` 48 | int list_find(t_node *begin_list, int data); 49 | ``` 50 | 51 | ### list_remove 52 | - 목록에서 n번 인덱스의 요소를 삭제하는 함수를 작성하세요. 53 | - 삭제에 성공 했을 때는 1, 실패 했을 때는 0을 반환 합니다. 54 | ``` 55 | int list_remove(t_node **begin_list, int n); 56 | ``` 57 | 58 | ### list_add 59 | - 목록의 n번 인덱스에 data를 갖는 새로운 요소를 생성하는 함수를 작성하세요. 60 | - n이 목록의 요소의 수보다 클 경우엔 마지막 위치에 생성하세요. 61 | - 생성된 요소의 인덱스를 반환 합니다. 62 | ``` 63 | int list_add(t_node **begin_list, int data, int n); 64 | ``` 65 | 66 | [뒤로 가기](..) 67 | -------------------------------------------------------------------------------- /queue/queue_array/implementation_example/queue.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* queue.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/24 16:10:19 by jko #+# #+# */ 9 | /* Updated: 2020/03/24 16:19:00 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef QUEUE_H 14 | # define QUEUE_H 15 | 16 | # include 17 | 18 | typedef struct s_queue 19 | { 20 | unsigned int max_size; 21 | int last_index; 22 | void **data; 23 | } t_queue; 24 | 25 | t_queue *queue_init(unsigned int max_size); 26 | int queue_push(t_queue *queue, void *data); 27 | int queue_size(t_queue *queue); 28 | void *queue_front(t_queue *queue); 29 | void *queue_pop(t_queue *queue); 30 | void queue_clear(t_queue *queue, void (*free_data)(void *)); 31 | void free_queue(t_queue *queue, void (*free_data)(void *)); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /stack/stack_array/implementation_example/stack.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* stack.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/12 00:48:13 by jko #+# #+# */ 9 | /* Updated: 2020/04/10 23:44:11 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef STACK_H 14 | # define STACK_H 15 | 16 | # include 17 | 18 | typedef struct s_stack 19 | { 20 | unsigned int max_size; 21 | int top_index; 22 | void **data; 23 | } t_stack; 24 | 25 | t_stack *stack_init(unsigned int max_size); 26 | int stack_push(t_stack *stack, void *data); 27 | void *stack_pop(t_stack *stack); 28 | int stack_size(t_stack *stack); 29 | void *stack_peek(t_stack *stack); 30 | void stack_clear(t_stack *stack, void (*free_data)(void *)); 31 | void free_stack(t_stack *stack, void (*free_data)(void *)); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /stack/stack_linked_list/implementation_example/stack.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* stack.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/12 01:05:16 by jko #+# #+# */ 9 | /* Updated: 2020/04/10 23:46:58 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef STACK_H 14 | # define STACK_H 15 | 16 | # include 17 | 18 | typedef struct s_node 19 | { 20 | void *data; 21 | struct s_node *next; 22 | } t_node; 23 | 24 | typedef struct s_stack 25 | { 26 | unsigned int size; 27 | t_node *top; 28 | } t_stack; 29 | 30 | t_stack *stack_init(void); 31 | t_node *create_elem(void *data); 32 | int stack_push(t_stack *stack, void *data); 33 | int stack_size(t_stack *stack); 34 | t_node *stack_peek(t_stack *stack); 35 | t_node *stack_pop(t_stack *stack); 36 | void stack_clear(t_stack *stack, void (*free_data)(void *)); 37 | void free_stack(t_stack *stack, void (*free_data)(void *)); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /queue/queue_linked_list/implementation_example/queue.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* queue.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/24 16:10:19 by jko #+# #+# */ 9 | /* Updated: 2020/03/24 16:57:04 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef QUEUE_H 14 | # define QUEUE_H 15 | 16 | # include 17 | 18 | typedef struct s_node 19 | { 20 | void *data; 21 | struct s_node *next; 22 | } t_node; 23 | 24 | typedef struct s_queue 25 | { 26 | unsigned int size; 27 | t_node *head; 28 | t_node *tail; 29 | } t_queue; 30 | 31 | t_queue *queue_init(void); 32 | t_node *create_elem(void *data); 33 | int queue_push(t_queue *queue, void *data); 34 | int queue_size(t_queue *queue); 35 | t_node *queue_front(t_queue *queue); 36 | t_node *queue_pop(t_queue *queue); 37 | void queue_clear(t_queue *queue, void (*free_data)(void *)); 38 | void free_queue(t_queue *queue, void (*free_data)(void *)); 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /hash/hash_map/implementation/map.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* map.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/20 15:33:46 by jko #+# #+# */ 9 | /* Updated: 2020/04/20 16:19:59 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef MAP_H 14 | # define MAP_H 15 | 16 | # define BIG_PRIM 1000003 17 | # define POWER 109 18 | 19 | # include 20 | 21 | typedef unsigned int uint; 22 | 23 | typedef struct s_node 24 | { 25 | const char *key; 26 | void *value; 27 | struct s_node *next; 28 | } t_node; 29 | 30 | typedef struct s_hash_map 31 | { 32 | unsigned int size; 33 | t_node **map; 34 | } t_hash_map; 35 | 36 | t_hash_map *map_init(void); 37 | int map_insert(t_hash_map *map, const char *key, void *value); 38 | void *map_get(t_hash_map *map, const char *key); 39 | void *map_delete(t_hash_map *map, const char *key); 40 | void free_map(t_hash_map *map, void (*free_data)(void *)); 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /graph/bellman-ford/implementation/graph.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* graph.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/22 20:52:43 by jko #+# #+# */ 9 | /* Updated: 2020/06/08 22:14:04 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GRAPH_H 14 | # define GRAPH_H 15 | 16 | # include 17 | # include 18 | 19 | typedef unsigned int uint; 20 | 21 | typedef struct s_node 22 | { 23 | unsigned int vertex; 24 | int cost; 25 | struct s_node *next; 26 | } t_node; 27 | 28 | typedef struct s_graph 29 | { 30 | unsigned int size; 31 | void **data; 32 | t_node **list; 33 | } t_graph; 34 | 35 | t_graph *graph_init(uint size); 36 | bool graph_set_data( 37 | t_graph *graph, uint vertex, void *data); 38 | bool graph_set_edge(t_graph *graph, 39 | uint start, uint end, bool state, int cost); 40 | void free_graph(t_graph *graph, void (*free_data)(void *)); 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /graph/dijkstra/implementation/graph.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* graph.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/22 20:52:43 by jko #+# #+# */ 9 | /* Updated: 2020/06/08 22:14:04 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GRAPH_H 14 | # define GRAPH_H 15 | 16 | # include 17 | # include 18 | 19 | typedef unsigned int uint; 20 | 21 | typedef struct s_node 22 | { 23 | unsigned int vertex; 24 | int cost; 25 | struct s_node *next; 26 | } t_node; 27 | 28 | typedef struct s_graph 29 | { 30 | unsigned int size; 31 | void **data; 32 | t_node **list; 33 | } t_graph; 34 | 35 | t_graph *graph_init(uint size); 36 | bool graph_set_data( 37 | t_graph *graph, uint vertex, void *data); 38 | bool graph_set_edge(t_graph *graph, 39 | uint start, uint end, bool state, int cost); 40 | void free_graph(t_graph *graph, void (*free_data)(void *)); 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /hash/hash_map/README.md: -------------------------------------------------------------------------------- 1 | # Hash Map 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - hash map을 구현 합니다. 8 | - 편의상 key는 string을 사용합니다. 9 | - 아래와 같은 map.h를 사용합니다. 10 | ``` 11 | typedef struct s_node 12 | { 13 | const char *key; 14 | void *value; 15 | struct s_node *next; 16 | } t_node; 17 | 18 | typedef struct s_hash_map 19 | { 20 | unsigned int size; 21 | t_node **map; 22 | } t_hash_map; 23 | ``` 24 | 25 | ### map_init 26 | - t_hash_map형 struct를 반환 하는 함수를 작성하세요. 27 | - 반환되는 t_hash_map은 메모리 할당과 초기화를 거쳐야합니다. 28 | ``` 29 | t_hash_map *map_init(void); 30 | ``` 31 | 32 | ### map_insert 33 | - hash map에 key, value 쌍을 삽입하는 함수를 작성하세요. 34 | - hash collision 발생 시 linked list를 사용하여 chaining합니다. 35 | - 삽입에 성공하면 1, 실패하면 0을 반환합니다. 36 | - 이미 hash map 안에 존재하는 key를 삽입하면 실패합니다. 37 | ``` 38 | int map_insert(t_hash_map *map, const char *key, void *value); 39 | ``` 40 | 41 | ### map_get 42 | - hash map에서 key와 쌍을 이루는 value를 반환하는 함수를 작성하세요. 43 | - 존재한다면 value를, 없다면 0을 반환합니다. 44 | ``` 45 | void *map_get(t_hash_map *map, const char *key); 46 | ``` 47 | 48 | ### map_delete 49 | - hash map에서 주어진 key와 같은 데이터를 삭제하고 value를 반환하는 함수를 작성하세요. 50 | - 삭제에 성공하면 value를, 실패하면 null pointer를 반환합니다. 51 | - hash map 안에 없는 key를 삭제하려 하면 실패로 간주합니다. 52 | - node->key는 메모리 할당을 해제하지 않습니다. 53 | ``` 54 | void *map_delete(t_hash_map *map, const char *key); 55 | ``` 56 | 57 | ### free_map 58 | - hash map에 있는 key, value 쌍 전체를 삭제하고 hash map의 메모리 할당을 해제하는 함수를 작성하세요. 59 | - node->key는 메모리 할당을 해제하지 않습니다. 60 | - value는 free_data를 사용해서 메모리 할당을 해제해야합니다. 61 | ``` 62 | void free_map(t_hash_map *map, void (*free_data)(void *)); 63 | ``` 64 | 65 | 66 | [뒤로 가기](..) 67 | -------------------------------------------------------------------------------- /graph/README.md: -------------------------------------------------------------------------------- 1 | # Graph 2 | 3 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 4 | 5 | ## index 6 | 1. [Undirected Graph(adjacency matrix)](./undirected_matrix) 7 | 1. [Undirected Graph(adjacency list)](./undirected_list) 8 | 1. [Directed Graph(adjacency list)](./directed_list) 9 | 1. Minimum Spanning Tree(MST, 최소 신장 트리) 10 | - [Kruskal](./kruskal) 11 | - [Prim](./prim) 12 | 1. Shortest Path(최단 경로) 13 | - [Dijkstra](./dijkstra) 14 | - [Bellman-Ford](./bellman-ford) 15 | - [Floyd-Warshall](./floyd-warshall) 16 | 1. [baekjoon](#baekjoon) 17 | 1. [생각해보기](#생각해보기) 18 | 19 | ## baekjoon 20 | graph를 사용해 풀어봅시다. 21 | - [5567번 결혼식](https://www.acmicpc.net/problem/5567) 22 | - [1058번 친구](https://www.acmicpc.net/problem/1058) 23 | - [1197번 최소 스패닝 트리](https://www.acmicpc.net/problem/1197) 24 | - [1774번 우주신과의 교감](https://www.acmicpc.net/problem/1774) 25 | - [1922번 네트워크 연결](https://www.acmicpc.net/problem/1922) 26 | - [1753번 최단경로](https://www.acmicpc.net/problem/1753) 27 | - [1719번 택배](https://www.acmicpc.net/problem/1719) 28 | - [6593번 상범 빌딩](https://www.acmicpc.net/problem/6593) 29 | - [1738번 골목길](https://www.acmicpc.net/problem/1738) 30 | - [11657번 타임머신](https://www.acmicpc.net/problem/11657) 31 | - [1219번 오민식의 고민](https://www.acmicpc.net/problem/1219) 32 | - [11404번 플로이드](https://www.acmicpc.net/problem/11404) 33 | - [1238번 파티](https://www.acmicpc.net/problem/1238) 34 | 35 | ## 생각해보기 36 | - graph와 tree의 비교 37 | - adjacency matrix와 adjacency list 비교 38 | - cyclic graph, acyclic graph란? 39 | - MST 알고리즘의 성능 비교(어떤 상황에서 어떤 알고리즘이 좋을까?) 40 | - 각 각의 최단 경로 알고리즘이 사용되는 경우는? 41 | - 단일 출발, 단일 도착, 단일 쌍, 전체 쌍 최단 경로 42 | - 음수 가중치, 음수 사이클 43 | 44 | 45 | [뒤로 가기](https://github.com/nadarm/42-algorithm) 46 | -------------------------------------------------------------------------------- /graph/floyd-warshall/implementation/graph.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* graph.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/22 20:52:43 by jko #+# #+# */ 9 | /* Updated: 2020/06/08 22:14:04 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GRAPH_H 14 | # define GRAPH_H 15 | 16 | # include 17 | # include 18 | 19 | typedef unsigned int uint; 20 | 21 | typedef struct s_node 22 | { 23 | unsigned int vertex; 24 | int cost; 25 | struct s_node *next; 26 | } t_node; 27 | 28 | typedef struct s_graph 29 | { 30 | unsigned int size; 31 | void **data; 32 | t_node **list; 33 | } t_graph; 34 | 35 | t_graph *graph_init(uint size); 36 | bool graph_set_data( 37 | t_graph *graph, uint vertex, void *data); 38 | bool graph_set_edge(t_graph *graph, 39 | uint start, uint end, bool state, int cost); 40 | void free_graph(t_graph *graph, void (*free_data)(void *)); 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /tree/binary_search_tree/implementation_example/tree.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* tree.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/04 21:11:12 by jko #+# #+# */ 9 | /* Updated: 2020/04/05 16:54:06 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef TREE_H 14 | # define TREE_H 15 | 16 | # include 17 | 18 | typedef struct s_node 19 | { 20 | void *data; 21 | struct s_node *left; 22 | struct s_node *right; 23 | } t_node; 24 | 25 | typedef struct s_binary_search_tree 26 | { 27 | t_node *root; 28 | int (*cmp)(void *, void *); 29 | int size; 30 | } t_tree; 31 | 32 | t_tree *tree_init(int (*cmp)(void *, void *)); 33 | t_node *create_elem(void *data); 34 | int tree_size(t_tree *tree); 35 | int tree_insert(t_tree *tree, void *data); 36 | t_node *tree_find(t_tree *tree, void *data_ref); 37 | int tree_delete(t_tree *tree, void *data_ref, void (*free_data)(void *)); 38 | void free_tree(t_tree *tree, void (*free_data)(void *)); 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /queue/queue_linked_list/README.md: -------------------------------------------------------------------------------- 1 | # Queue(Linked List) 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - linked list를 사용하여 queue를 구현 합니다. 8 | - 아래와 같은 queue.h를 사용 합니다. 9 | ``` 10 | typedef struct s_node 11 | { 12 | void *data; 13 | struct s_node *next; 14 | } t_node; 15 | 16 | typedef struct s_queue 17 | { 18 | unsigned int size; 19 | t_node *head; 20 | t_node *tail; 21 | } t_queue; 22 | ``` 23 | 24 | ### queue_init 25 | - t_queue형 struct를 반환 하는 함수를 작성하세요. 26 | - 반환되는 t_queue는 메모리 할당과 초기화를 거쳐야합니다. 27 | ``` 28 | t_queue *queue_init(void); 29 | ``` 30 | 31 | ### create_elem 32 | - t_node형 새로운 요소를 생성하는 함수를 작성하세요. 33 | ``` 34 | t_node *create_elem(void *data); 35 | ``` 36 | 37 | ### queue_push 38 | - queue의 가장 뒤에 data를 갖는 새로운 요소를 생성하는 함수를 작성하세요. 39 | - 성공하면 1, 실패하면 0을 반환 합니다. 40 | ``` 41 | int queue_push(t_queue *queue, void *data); 42 | ``` 43 | 44 | ### queue_size 45 | - queue에 있는 요소의 개수를 반환하는 함수를 작성하세요. 46 | ``` 47 | int queue_size(t_queue *queue); 48 | ``` 49 | 50 | ### queue_front 51 | - queue에서 가장 앞에 있는 요소를 반환하는 함수를 작성하세요. 52 | ``` 53 | t_node *queue_front(t_queue *queue); 54 | ``` 55 | 56 | ### queue_pop 57 | - queue에서 가장 앞에 있는 요소를 꺼내는 함수를 작성하세요. 58 | ``` 59 | t_node *queue_pop(t_queue *queue); 60 | ``` 61 | 62 | ### queue_clear 63 | - queue의 요소 전체를 삭제하는 함수를 작성하세요. 64 | - 요소의 데이터는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 65 | ``` 66 | void queue_clear(t_queue *queue, void (*free_data)(void *)); 67 | ``` 68 | 69 | ### free_queue 70 | - queue에 있는 요소 전체를 삭제하고 queue의 메모리 할당을 해제하는 함수를 작성하세요. 71 | - 요소의 데이터는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 72 | ``` 73 | void free_queue(t_queue *queue, void (*free_data)(void *)); 74 | ``` 75 | 76 | 77 | [뒤로 가기](..) 78 | -------------------------------------------------------------------------------- /hash/hash_set/README.md: -------------------------------------------------------------------------------- 1 | # Hash Set 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - hash set을 구현 합니다. 8 | - 아래와 같은 set.h를 사용 합니다. 9 | ``` 10 | typedef struct s_node 11 | { 12 | void *data; 13 | struct s_node *next; 14 | } t_node; 15 | 16 | typedef struct s_hash_set 17 | { 18 | unsigned int size; 19 | t_node **set; 20 | int (*cmp)(void *, void *); 21 | } t_hash_set; 22 | ``` 23 | 24 | ### set_init 25 | - t_hash_set형 struct를 반환 하는 함수를 작성하세요. 26 | - 반환되는 t_hash_set은 메모리 할당과 초기화를 거쳐야합니다. 27 | - cmp는 t_node의 data를 비교하는데 사용 됩니다. 첫번째 인자가 더 크면 양수, 작으면 음수, 같다면 0을 반환합니다. 28 | ``` 29 | t_hash_set *set_init(int (*cmp)(void *, void *)); 30 | ``` 31 | 32 | ### set_insert 33 | - hash set에 data를 삽입하는 함수를 작성하세요. 34 | - hash collision 발생 시 linked list를 사용하여 chaining합니다. 35 | - 삽입에 성공하면 1, 실패하면 0을 반환합니다. 36 | - 이미 hash set 안에 존재하는 data를 삽입하면 실패합니다. 37 | ``` 38 | int set_insert(t_hash_set *set, void *data, unsigned int data_size); 39 | ``` 40 | 41 | ### set_exists 42 | - hash set 안에 data가 존재하는지 확인하는 함수를 작성하세요. 43 | - 존재한다면 1, 없다면 0을 반환합니다. 44 | ``` 45 | int set_exists(t_hash_set *set, void *data, unsigned int data_size); 46 | ``` 47 | 48 | ### set_delete 49 | - hash set에서 data를 삭제하는 함수를 작성하세요. 50 | - 삭제에 성공하면 1, 실패하면 0을 반환합니다. 51 | - hash set 안에 없는 data를 삭제하려 하면 성공으로 간주합니다. 52 | - data는 free_data를 사용해서 메모리 할당을 해제해야합니다. 53 | ``` 54 | int set_delete(t_hash_set *set, void *data, unsigned int data_size, void (*free_data)(void *)); 55 | ``` 56 | 57 | ### free_set 58 | - hash set에 있는 data 전체를 삭제하고 hash set의 메모리 할당을 해제하는 함수를 작성하세요. 59 | - data는 free_data를 사용해서 메모리 할당을 해제해야합니다. 60 | ``` 61 | void free_set(t_hash_set *set, void (*free_data)(void *)); 62 | ``` 63 | 64 | 65 | [뒤로 가기](..) 66 | -------------------------------------------------------------------------------- /stack/stack_linked_list/README.md: -------------------------------------------------------------------------------- 1 | # Stack(Linked List) 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - linked list를 사용하여 stack을 구현 합니다. 8 | - 아래와 같은 stack.h를 사용 합니다. 9 | ``` 10 | typedef struct s_node 11 | { 12 | void *data; 13 | struct s_node *next; 14 | } t_node; 15 | 16 | typedef struct s_stack 17 | { 18 | unsigned int size; 19 | t_node *top; 20 | } t_stack; 21 | ``` 22 | 23 | ### stack_init 24 | - t_stack형 struct를 반환 하는 함수를 작성하세요. 25 | - 반환되는 t_stack는 메모리 할당과 초기화를 거쳐야합니다. 26 | ``` 27 | t_stack *stack_init(void); 28 | ``` 29 | 30 | ### create_elem 31 | - t_node형 새로운 요소를 생성하는 함수를 작성하세요. 32 | ``` 33 | t_node *create_elem(void *data); 34 | ``` 35 | 36 | ### stack_push 37 | - stack의 가장 위에 data를 갖는 새로운 요소를 생성하는 함수를 작성하세요. 38 | - 성공하면 1, 실패하면 0을 반환 합니다. 39 | ``` 40 | int stack_push(t_stack *stack, void *data); 41 | ``` 42 | 43 | ### stack_size 44 | - stack에 있는 요소의 개수를 반환하는 함수를 작성하세요. 45 | ``` 46 | int stack_size(t_stack *stack); 47 | ``` 48 | 49 | ### stack_peek 50 | - stack에서 가장 위에 있는 요소를 반환하는 함수를 작성하세요. 51 | ``` 52 | t_node *stack_peek(t_stack *stack); 53 | ``` 54 | 55 | ### stack_pop 56 | - stack에서 가장 위에 있는 요소를 꺼내는 함수를 작성하세요. 57 | - 반환하는 요소의 next를 널포인터로 바꾼후 반환 합니다. 58 | ``` 59 | t_node *stack_pop(t_stack *stack); 60 | ``` 61 | 62 | ### stack_clear 63 | - stack의 요소 전체를 삭제하는 함수를 작성하세요. 64 | - 요소의 데이터는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 65 | ``` 66 | void stack_clear(t_stack *stack, void (*free_data)(void *)); 67 | ``` 68 | 69 | ### free_stack 70 | - stack에 있는 요소 전체를 삭제하고 stack의 메모리 할당을 해제하는 함수를 작성하세요. 71 | - 요소의 데이터는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 72 | ``` 73 | void free_stack(t_stack *stack, void (*free_data)(void *)); 74 | ``` 75 | 76 | 77 | [뒤로 가기](..) 78 | -------------------------------------------------------------------------------- /tree/union_find/implementation_example/union_find.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* union_find.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/12 22:05:26 by jko #+# #+# */ 9 | /* Updated: 2020/04/23 13:39:13 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "union_find.h" 14 | 15 | t_node *create_elem(void *data) 16 | { 17 | t_node *node; 18 | 19 | node = malloc(sizeof(t_node)); 20 | if (!node) 21 | return (0); 22 | node->data = data; 23 | node->parent = 0; 24 | return (node); 25 | } 26 | 27 | t_node *find(t_node *node) 28 | { 29 | if (!node) 30 | return (0); 31 | if (!node->parent) 32 | return (node); 33 | return (node->parent = find(node->parent)); 34 | } 35 | 36 | bool is_disjoint(t_node *node1, t_node *node2) 37 | { 38 | if (!node1 || !node2) 39 | return (true); 40 | return (!(find(node1) == find(node2))); 41 | } 42 | 43 | t_node *union_func(t_node *node1, t_node *node2) 44 | { 45 | if (!node1 || !node2) 46 | return (0); 47 | if (!is_disjoint(node1, node2)) 48 | return (find(node1)); 49 | return (find(node2)->parent = find(node1)); 50 | } 51 | -------------------------------------------------------------------------------- /hash/hash_set/implementation/set.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* set.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/19 20:38:34 by jko #+# #+# */ 9 | /* Updated: 2020/04/19 21:11:55 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef SET_H 14 | # define SET_H 15 | 16 | # define BIG_PRIM 1000003 17 | # define POWER 109 18 | 19 | # include 20 | 21 | typedef unsigned int uint; 22 | 23 | typedef struct s_node 24 | { 25 | void *data; 26 | struct s_node *next; 27 | } t_node; 28 | 29 | typedef struct s_hash_set 30 | { 31 | unsigned int size; 32 | t_node **set; 33 | int (*cmp)(void *, void *); 34 | } t_hash_set; 35 | 36 | t_hash_set *set_init(int (*cmp)(void *, void *)); 37 | int set_insert(t_hash_set *set, 38 | void *data, unsigned int data_size); 39 | int set_exists(t_hash_set *set, 40 | void *data, unsigned int data_size); 41 | int set_delete(t_hash_set *set, void *data, 42 | unsigned int data_size, void (*free_data)(void *)); 43 | void free_set(t_hash_set *set, void (*free_data)(void *)); 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /graph/kruskal/implementation/graph.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* graph.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/22 20:52:43 by jko #+# #+# */ 9 | /* Updated: 2020/04/25 13:31:13 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GRAPH_H 14 | # define GRAPH_H 15 | 16 | # include 17 | # include 18 | 19 | typedef unsigned int uint; 20 | 21 | typedef struct s_node 22 | { 23 | unsigned int vertex; 24 | int cost; 25 | struct s_node *next; 26 | } t_node; 27 | 28 | typedef struct s_graph 29 | { 30 | unsigned int size; 31 | void **data; 32 | t_node **list; 33 | } t_graph; 34 | 35 | t_graph *graph_init(uint size); 36 | bool graph_set_data( 37 | t_graph *graph, uint vertex, void *data); 38 | bool graph_set_edge(t_graph *graph, 39 | uint vertex1, uint vertex2, bool state, int cost); 40 | void *graph_get_data(t_graph *graph, uint vertex); 41 | bool graph_get_edge(t_graph *graph, 42 | uint vertex1, uint vertex2, int *cost); 43 | void free_graph(t_graph *graph, void (*free_data)(void *)); 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /graph/prim/implementation/graph.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* graph.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/22 20:52:43 by jko #+# #+# */ 9 | /* Updated: 2020/04/25 13:31:13 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GRAPH_H 14 | # define GRAPH_H 15 | 16 | # include 17 | # include 18 | 19 | typedef unsigned int uint; 20 | 21 | typedef struct s_node 22 | { 23 | unsigned int vertex; 24 | int cost; 25 | struct s_node *next; 26 | } t_node; 27 | 28 | typedef struct s_graph 29 | { 30 | unsigned int size; 31 | void **data; 32 | t_node **list; 33 | } t_graph; 34 | 35 | t_graph *graph_init(uint size); 36 | bool graph_set_data( 37 | t_graph *graph, uint vertex, void *data); 38 | bool graph_set_edge(t_graph *graph, 39 | uint vertex1, uint vertex2, bool state, int cost); 40 | void *graph_get_data(t_graph *graph, uint vertex); 41 | bool graph_get_edge(t_graph *graph, 42 | uint vertex1, uint vertex2, int *cost); 43 | void free_graph(t_graph *graph, void (*free_data)(void *)); 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /deque/deque_linked_list/implementation_example/deque.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* deque.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/30 18:12:09 by jko #+# #+# */ 9 | /* Updated: 2020/03/30 18:16:43 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef DEQUE_H 14 | # define DEQUE_H 15 | 16 | # include 17 | 18 | typedef struct s_node 19 | { 20 | void *data; 21 | struct s_node *prev; 22 | struct s_node *next; 23 | } t_node; 24 | 25 | typedef struct s_deque 26 | { 27 | unsigned int size; 28 | t_node *front; 29 | t_node *back; 30 | } t_deque; 31 | 32 | t_deque *deque_init(void); 33 | t_node *create_elem(void *data); 34 | int deque_push_front(t_deque *deque, void *data); 35 | int deque_push_back(t_deque *deque, void *data); 36 | int deque_size(t_deque *deque); 37 | t_node *deque_front(t_deque *deque); 38 | t_node *deque_back(t_deque *deque); 39 | t_node *deque_pop_front(t_deque *deque); 40 | t_node *deque_pop_back(t_deque *deque); 41 | void deque_clear(t_deque *deque, void (*free_data)(void *)); 42 | void free_deque(t_deque *deque, void (*free_data)(void *)); 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /graph/undirected_matrix/implementation/graph.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* graph.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/21 16:27:04 by jko #+# #+# */ 9 | /* Updated: 2020/04/21 20:34:44 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GRAPH_H 14 | # define GRAPH_H 15 | 16 | # include 17 | # include 18 | # include 19 | 20 | typedef unsigned int uint; 21 | 22 | typedef struct s_graph 23 | { 24 | unsigned int size; 25 | void **data; 26 | bool **matrix; 27 | } t_graph; 28 | 29 | t_graph *graph_init(uint size); 30 | bool graph_set_data( 31 | t_graph *graph, uint vertex, void *data); 32 | bool graph_set_edge(t_graph *graph, 33 | uint vertex1, uint vertex2, bool state); 34 | void *graph_get_data(t_graph *graph, uint vertex); 35 | bool graph_get_edge(t_graph *graph, 36 | uint vertex1, uint vertex2); 37 | void free_graph(t_graph *graph, void (*free_data)(void *)); 38 | void graph_traverse(t_graph *graph, 39 | void (*print_data)(void *)); 40 | t_graph *make_graph(void); 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /sort/quick/implementation/quick.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* quick.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/14 17:55:48 by jko #+# #+# */ 9 | /* Updated: 2020/04/16 01:23:51 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "quick.h" 14 | 15 | static void swap(void **items, int i, int j) 16 | { 17 | void *temp; 18 | 19 | temp = items[i]; 20 | items[i] = items[j]; 21 | items[j] = temp; 22 | } 23 | 24 | static void quick(void **items, int start, int end, int (*cmp)(void *, void *)) 25 | { 26 | int i; 27 | int j; 28 | 29 | if (start >= end) 30 | return ; 31 | swap(items, start, (start + end) / 2); 32 | i = start; 33 | j = end; 34 | while (i < j) 35 | { 36 | while (cmp(items[start], items[j]) < 0) 37 | j--; 38 | while (i < j && cmp(items[start], items[i]) >= 0) 39 | i++; 40 | swap(items, i, j); 41 | } 42 | swap(items, start, i); 43 | quick(items, start, i - 1, cmp); 44 | quick(items, i + 1, end, cmp); 45 | } 46 | 47 | int quick_sort(void **items, int size, int (*cmp)(void *, void *)) 48 | { 49 | if (!items || size < 0 || !cmp) 50 | return (0); 51 | quick(items, 0, size - 1, cmp); 52 | return (1); 53 | } 54 | -------------------------------------------------------------------------------- /linked_list/doubly_linked_list/README.md: -------------------------------------------------------------------------------- 1 | # Doubly Linked List 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - 아래와 같은 list.h를 사용 합니다. 8 | ``` 9 | typedef struct s_node 10 | { 11 | int data; 12 | struct s_node *prev; 13 | struct s_node *next; 14 | } t_node; 15 | 16 | typedef struct s_linked_list 17 | { 18 | unsigned int size; 19 | t_node *head; 20 | t_node *tail; 21 | } t_linked_list; 22 | ``` 23 | 24 | ### list_init 25 | - t_linked_list형 struct를 반환 하는 함수를 작성하세요. 26 | - 반환되는 t_linked_list는 메모리 할당과 초기화를 거쳐야합니다. 27 | ``` 28 | t_linked_list *list_init(void); 29 | ``` 30 | 31 | ### create_elem 32 | - t_node형 새로운 요소를 생성하는 함수를 작성하세요. 33 | ``` 34 | t_node *create_elem(int data); 35 | ``` 36 | 37 | ### list_size 38 | - 목록에 있는 요소의 개수를 반환하는 함수를 작성하세요. 39 | ``` 40 | int list_size(t_linked_list *list); 41 | ``` 42 | 43 | ### list_get 44 | - 목록에서 n번 인덱스의 요소를 반환하는 함수를 작성하세요. 45 | - 목록에 있는 요소의 수가 더 적을 땐, 널포인터를 반환 합니다. 46 | ``` 47 | t_node *list_get(t_linked_list *list, int n); 48 | ``` 49 | 50 | ### list_add 51 | - 목록의 n번 인덱스에 data를 갖는 새로운 요소를 생성하는 함수를 작성하세요. 52 | - n이 목록의 요소의 수보다 클 경우엔 마지막 위치에 생성하세요. 53 | - 생성된 요소의 인덱스를 반환 합니다. 54 | ``` 55 | int list_add(t_linked_list *list, int data, int n); 56 | ``` 57 | 58 | ### list_remove 59 | - 목록에서 n번 인덱스의 요소를 삭제하는 함수를 작성하세요. 60 | - 삭제에 성공 했을 때는 1, 실패 했을 때는 0을 반환 합니다. 61 | ``` 62 | int list_remove(t_linked_list *list, int n); 63 | ``` 64 | 65 | ### list_clear 66 | - 목록에 있는 전체 요소를 삭제하는 함수를 작성하세요. 67 | ``` 68 | void list_clear(t_linked_list *list); 69 | ``` 70 | 71 | ### free_list 72 | - 목록의 메모리 할당을 해제하는 함수를 작성하세요. 73 | - 목록에 있는 요소와 목록 모두 해제해야 합니다. 74 | ``` 75 | void free_list(t_linked_list *list); 76 | ``` 77 | 78 | 79 | ## grademe 80 | - `grademe.sh`, `grademe_files`가 있는 디렉토리에 소스코드를 넣는다 (main함수는 지우거나 주석처리) 81 | - 터미널 명령어 `sh ./grademe.sh`를 입력 82 | 83 | 84 | [뒤로 가기](..) 85 | -------------------------------------------------------------------------------- /graph/directed_list/implementation/graph.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* graph.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/23 17:52:31 by jko #+# #+# */ 9 | /* Updated: 2020/04/23 17:53:44 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GRAPH_H 14 | # define GRAPH_H 15 | 16 | # include 17 | # include 18 | # include 19 | 20 | typedef unsigned int uint; 21 | 22 | typedef struct s_node 23 | { 24 | unsigned int vertex; 25 | struct s_node *next; 26 | } t_node; 27 | 28 | typedef struct s_graph 29 | { 30 | unsigned int size; 31 | void **data; 32 | t_node **list; 33 | } t_graph; 34 | 35 | t_graph *graph_init(uint size); 36 | bool graph_set_data( 37 | t_graph *graph, uint vertex, void *data); 38 | bool graph_set_edge(t_graph *graph, 39 | uint start, uint end, bool state); 40 | void *graph_get_data(t_graph *graph, uint vertex); 41 | bool graph_get_edge(t_graph *graph, 42 | uint start, uint end); 43 | void free_graph(t_graph *graph, void (*free_data)(void *)); 44 | void graph_traverse(t_graph *graph, 45 | void (*print_data)(void *)); 46 | t_graph *make_graph(void); 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /hash/rabin_karp/implementation/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/09 17:19:10 by jko #+# #+# */ 9 | /* Updated: 2020/04/18 20:57:02 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "rabin_karp.h" 14 | #include 15 | #include 16 | 17 | int main(void) 18 | { 19 | printf("hash = %lld\n", get_hash_value(0, 50)); 20 | printf("hash = %lld\n", get_hash_value("", 50)); 21 | printf("hash = %lld\n", get_hash_value("abcdefg", 0)); 22 | printf("hash = %lld\n", get_hash_value("abcdefg", 1)); 23 | printf("hash = %lld\n", get_hash_value("abcdefg", 50)); 24 | 25 | printf("find result = %s\n", find_str(0, 0)); 26 | printf("find result = %s\n", find_str(0, "")); 27 | printf("find result = %s\n", find_str("", 0)); 28 | printf("find result = %s\n", find_str("", "")); 29 | 30 | printf("find result = %s\n", find_str("abcdefgaabbccddeeffgg", 0)); 31 | printf("find result = %s\n", find_str("abcdefgaabbccddeeffgg", "abcf")); 32 | printf("find result = %s\n", find_str("abcdefgaabbccddeeffgg", "abbc")); 33 | 34 | 35 | system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); 36 | 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /graph/undirected_list/implementation/graph.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* graph.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/22 20:52:43 by jko #+# #+# */ 9 | /* Updated: 2020/04/22 20:53:45 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GRAPH_H 14 | # define GRAPH_H 15 | 16 | # include 17 | # include 18 | # include 19 | 20 | typedef unsigned int uint; 21 | 22 | typedef struct s_node 23 | { 24 | unsigned int vertex; 25 | struct s_node *next; 26 | } t_node; 27 | 28 | typedef struct s_graph 29 | { 30 | unsigned int size; 31 | void **data; 32 | t_node **list; 33 | } t_graph; 34 | 35 | t_graph *graph_init(uint size); 36 | bool graph_set_data( 37 | t_graph *graph, uint vertex, void *data); 38 | bool graph_set_edge(t_graph *graph, 39 | uint vertex1, uint vertex2, bool state); 40 | void *graph_get_data(t_graph *graph, uint vertex); 41 | bool graph_get_edge(t_graph *graph, 42 | uint vertex1, uint vertex2); 43 | void free_graph(t_graph *graph, void (*free_data)(void *)); 44 | void graph_traverse(t_graph *graph, 45 | void (*print_data)(void *)); 46 | t_graph *make_graph(void); 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /linked_list/circular_linked_list/implementation_example/list.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* list.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/08 00:04:33 by jko #+# #+# */ 9 | /* Updated: 2020/04/23 12:29:39 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIST_H 14 | # define LIST_H 15 | 16 | # include 17 | 18 | typedef struct s_node 19 | { 20 | void *data; 21 | struct s_node *prev; 22 | struct s_node *next; 23 | } t_node; 24 | 25 | typedef struct s_linked_list 26 | { 27 | unsigned int size; 28 | t_node *head; 29 | } t_linked_list; 30 | 31 | 32 | t_linked_list *list_init(void); 33 | t_node *create_elem(void *data); 34 | int list_add(t_linked_list *list, void *data, int n); 35 | int list_size(t_linked_list *list); 36 | t_node *list_get(t_linked_list *list, int n); 37 | int list_find(t_linked_list *list, void* data, int (*cmp)(void *, void *)); 38 | void list_remove(t_linked_list *list, int n, void (*free_data)(void *)); 39 | void list_clear(t_linked_list *list, void (*free_data)(void *)); 40 | void free_list(t_linked_list *list, void (*free_data)(void *)); 41 | void list_foreach(t_linked_list *list, void (*f)(void *)); 42 | void list_move_head_to_next(t_linked_list *list); 43 | void list_move_head_to_prev(t_linked_list *list); 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /sort/merge/implementation/merge.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* merge.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/16 14:19:25 by jko #+# #+# */ 9 | /* Updated: 2020/04/16 15:16:00 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "merge.h" 14 | 15 | static void **temps; 16 | 17 | static void merge(void **items, int start, int end, int (*cmp)(void *, void *)) 18 | { 19 | int i; 20 | int j; 21 | int k; 22 | int mid; 23 | 24 | if (start >= end) 25 | return ; 26 | mid = (start + end) / 2; 27 | merge(items, start, mid, cmp); 28 | merge(items, mid + 1, end, cmp); 29 | i = start; 30 | j = mid + 1; 31 | k = start; 32 | while (i <= mid && j <= end) 33 | { 34 | if (cmp(items[i], items[j]) <= 0) 35 | temps[k++] = items[i++]; 36 | else 37 | temps[k++] = items[j++]; 38 | } 39 | while (i <= mid) 40 | temps[k++] = items[i++]; 41 | while (j <= end) 42 | temps[k++] = items[j++]; 43 | while (start <= end) 44 | { 45 | items[start] = temps[start]; 46 | start++; 47 | } 48 | } 49 | 50 | int merge_sort(void **items, int size, int (*cmp)(void *, void *)) 51 | { 52 | if (!items || size < 0 || !cmp || !(temps = malloc(sizeof(void *) * size))) 53 | return (0); 54 | merge(items, 0, size - 1, cmp); 55 | free(temps); 56 | temps = 0; 57 | return (1); 58 | } 59 | -------------------------------------------------------------------------------- /sort/heap/implementation/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/14 00:16:57 by jko #+# #+# */ 9 | /* Updated: 2020/04/14 17:36:56 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "heap.h" 14 | #include 15 | #include 16 | 17 | typedef struct s_item 18 | { 19 | int data; 20 | int data2; 21 | } t_item; 22 | 23 | int cmp(void *a, void *b) 24 | { 25 | t_item *aa = a; 26 | t_item *bb = b; 27 | return (aa->data - bb->data); 28 | } 29 | 30 | void print_array(void **array, int size) 31 | { 32 | for (int i = 0; i < size; ++i) { 33 | printf("data = % 4d, data2 = % 4d\n", ((t_item *)array[i])->data, ((t_item *)array[i])->data2); 34 | } 35 | printf("\n"); 36 | } 37 | 38 | int main(void) 39 | { 40 | int size = 25; 41 | int data[25] = {1, 8, 0, 0, 3, 42 | 3, 5, -1, -3, 2, 43 | 32, 12, 1, 4, -3, 44 | -13, 1, 5, 4, 9, 45 | 8, 7, 6, 5, 4}; 46 | t_item items[size]; 47 | 48 | void **array = malloc(sizeof(void *) * size); 49 | for (int i = 0; i < size; ++i) { 50 | items[i].data = data[i]; 51 | items[i].data2 = i; 52 | array[i] = &items[i]; 53 | } 54 | 55 | print_array(array, size); 56 | heap_sort(array, size, cmp); 57 | print_array(array, size); 58 | 59 | free(array); 60 | 61 | system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); 62 | 63 | return (0); 64 | } 65 | -------------------------------------------------------------------------------- /sort/merge/implementation/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/16 14:20:07 by jko #+# #+# */ 9 | /* Updated: 2020/04/16 14:20:26 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "merge.h" 14 | #include 15 | #include 16 | 17 | typedef struct s_item 18 | { 19 | int data; 20 | int data2; 21 | } t_item; 22 | 23 | int cmp(void *a, void *b) 24 | { 25 | t_item *aa = a; 26 | t_item *bb = b; 27 | return (aa->data - bb->data); 28 | } 29 | 30 | void print_array(void **array, int size) 31 | { 32 | for (int i = 0; i < size; ++i) { 33 | printf("data = % 4d, data2 = % 4d\n", ((t_item *)array[i])->data, ((t_item *)array[i])->data2); 34 | } 35 | printf("\n"); 36 | } 37 | 38 | int main(void) 39 | { 40 | int size = 25; 41 | int data[25] = {1, 8, 0, 0, 3, 42 | 3, 5, -1, -3, 2, 43 | 32, 12, 1, 4, -3, 44 | -13, 1, 5, 4, 9, 45 | 8, 7, 6, 5, 4}; 46 | t_item items[size]; 47 | 48 | void **array = malloc(sizeof(void *) * size); 49 | for (int i = 0; i < size; ++i) { 50 | items[i].data = data[i]; 51 | items[i].data2 = i; 52 | array[i] = &items[i]; 53 | } 54 | 55 | print_array(array, size); 56 | merge_sort(array, size, cmp); 57 | print_array(array, size); 58 | 59 | free(array); 60 | 61 | system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); 62 | 63 | return (0); 64 | } 65 | -------------------------------------------------------------------------------- /sort/quick/implementation/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/14 00:16:57 by jko #+# #+# */ 9 | /* Updated: 2020/04/15 23:08:00 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "quick.h" 14 | #include 15 | #include 16 | 17 | typedef struct s_item 18 | { 19 | int data; 20 | int data2; 21 | } t_item; 22 | 23 | int cmp(void *a, void *b) 24 | { 25 | t_item *aa = a; 26 | t_item *bb = b; 27 | return (aa->data - bb->data); 28 | } 29 | 30 | void print_array(void **array, int size) 31 | { 32 | for (int i = 0; i < size; ++i) { 33 | printf("data = % 4d, data2 = % 4d\n", ((t_item *)array[i])->data, ((t_item *)array[i])->data2); 34 | } 35 | printf("\n"); 36 | } 37 | 38 | int main(void) 39 | { 40 | int size = 25; 41 | int data[25] = {1, 8, 0, 0, 3, 42 | 3, 5, -1, -3, 2, 43 | 32, 12, 1, 4, -3, 44 | -13, 1, 5, 4, 9, 45 | 8, 7, 6, 5, 4}; 46 | t_item items[size]; 47 | 48 | void **array = malloc(sizeof(void *) * size); 49 | for (int i = 0; i < size; ++i) { 50 | items[i].data = data[i]; 51 | items[i].data2 = i; 52 | array[i] = &items[i]; 53 | } 54 | 55 | print_array(array, size); 56 | quick_sort(array, size, cmp); 57 | print_array(array, size); 58 | 59 | free(array); 60 | 61 | system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); 62 | 63 | return (0); 64 | } 65 | -------------------------------------------------------------------------------- /sort/bubble/implementation/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/14 00:16:57 by jko #+# #+# */ 9 | /* Updated: 2020/04/14 00:58:38 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "bubble.h" 14 | #include 15 | #include 16 | 17 | typedef struct s_item 18 | { 19 | int data; 20 | int data2; 21 | } t_item; 22 | 23 | int cmp(void *a, void *b) 24 | { 25 | t_item *aa = a; 26 | t_item *bb = b; 27 | return (aa->data - bb->data); 28 | } 29 | 30 | void print_array(void **array, int size) 31 | { 32 | for (int i = 0; i < size; ++i) { 33 | printf("data = % 4d, data2 = % 4d\n", ((t_item *)array[i])->data, ((t_item *)array[i])->data2); 34 | } 35 | printf("\n"); 36 | } 37 | 38 | int main(void) 39 | { 40 | int size = 25; 41 | int data[25] = {1, 8, 0, 0, 3, 42 | 3, 5, -1, -3, 2, 43 | 32, 12, 1, 4, -3, 44 | -13, 1, 5, 4, 9, 45 | 8, 7, 6, 5, 4}; 46 | t_item items[size]; 47 | 48 | void **array = malloc(sizeof(void *) * size); 49 | for (int i = 0; i < size; ++i) { 50 | items[i].data = data[i]; 51 | items[i].data2 = i; 52 | array[i] = &items[i]; 53 | } 54 | 55 | print_array(array, size); 56 | bubble_sort(array, size, cmp); 57 | print_array(array, size); 58 | 59 | free(array); 60 | 61 | system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); 62 | 63 | return (0); 64 | } 65 | -------------------------------------------------------------------------------- /tree/binary_search_tree/README.md: -------------------------------------------------------------------------------- 1 | # Binary Search Tree 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - binary search tree을 구현 합니다. 8 | - 아래와 같은 tree.h를 사용 합니다. 9 | ``` 10 | typedef struct s_node 11 | { 12 | void *data; 13 | struct s_node *left; 14 | struct s_node *right; 15 | } t_node; 16 | 17 | typedef struct s_binary_search_tree 18 | { 19 | t_node *root; 20 | int (*cmp)(void *, void *); 21 | int size; 22 | } t_tree; 23 | ``` 24 | 25 | ### tree_init 26 | - t_tree형 struct를 반환 하는 함수를 작성하세요. 27 | - 반환되는 t_tree는 메모리 할당과 초기화를 거쳐야합니다. 28 | - cmp는 t_node의 data를 비교하는데 사용 됩니다. 첫번째 인자가 더 크면 양수, 작으면 음수, 같다면 0을 반환 합니다. 29 | ``` 30 | t_tree *tree_init(int (*cmp)(void *, void *)); 31 | ``` 32 | 33 | ### create_elem 34 | - t_node형 새로운 요소를 생성하는 함수를 작성하세요. 35 | ``` 36 | t_node *create_elem(void *data); 37 | ``` 38 | 39 | ### tree_size 40 | - tree에 존재하는 요소의 갯수를 반환하는 함수를 작성하세요. 41 | ``` 42 | int tree_size(t_tree *tree); 43 | ``` 44 | 45 | ### tree_find 46 | - tree에 data_ref와 같은 data를 갖는 요소를 찾아 반환하는 함수를 작성하세요. 47 | - 같은 요소가 없다면 널 포인터를 반환 합니다. 48 | ``` 49 | t_node *tree_find(t_tree *tree, void *data_ref); 50 | ``` 51 | 52 | ### tree_insert 53 | - tree에 data를 갖는 새로운 요소를 삽입하는 함수를 작성하세요. 54 | - node의 data는 left의 data보다 크고 right의 data보다 작아야 합니다. 55 | - 성공하면 1, 실패하면 0을 반환 합니다. 56 | - 이미 같은 data를 가진 요소가 있다면 실패 합니다. 57 | ``` 58 | int tree_insert(t_tree *tree, void *data); 59 | ``` 60 | 61 | ### tree_delete 62 | - tree에서 data_ref와 같은 data를 가진 요소를 삭제하는 함수를 작성하세요. 63 | - 요소의 데이터는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 64 | - 삭제하려는 node의 child가 2개일 경우에는 node의 오른쪽 subtree 중 가장 작은 data를 가진 child가 node를 대체하여야 합니다. 65 | - node의 왼쪽 subtree 중 가장 큰 data를 가진 child가 node를 대체하는 방법도 가능하지만 본 예제에서는 위의 방법으로 구현하도록 합니다. 66 | - 성공하면 1, 실패하면 0을 반환 합니다. 67 | - data_ref와 같은 data를 가진 요소가 없다면 실패 합니다. 68 | ``` 69 | int tree_delete(t_tree *tree, void *data_ref, void (*free_data)(void *)); 70 | ``` 71 | 72 | ### free_tree 73 | - tree에 있는 요소 전체를 삭제하고 tree의 메모리 할당을 해제하는 함수를 작성하세요. 74 | - 요소의 데이터는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 75 | ``` 76 | void free_tree(t_tree *tree, void (*free_data)(void *)); 77 | ``` 78 | 79 | 80 | [뒤로 가기](..) 81 | -------------------------------------------------------------------------------- /deque/deque_linked_list/README.md: -------------------------------------------------------------------------------- 1 | # Deque(Linked List) 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - linked list를 사용하여 deque을 구현 합니다. 8 | - 아래와 같은 deque.h를 사용 합니다. 9 | ``` 10 | typedef struct s_node 11 | { 12 | void *data; 13 | struct s_node *prev; 14 | struct s_node *next; 15 | } t_node; 16 | 17 | typedef struct s_deque 18 | { 19 | unsigned int size; 20 | t_node *front; 21 | t_node *back; 22 | } t_deque; 23 | ``` 24 | 25 | ### deque_init 26 | - t_deque형 struct를 반환 하는 함수를 작성하세요. 27 | - 반환되는 t_deque는 메모리 할당과 초기화를 거쳐야합니다. 28 | ``` 29 | t_deque *deque_init(void); 30 | ``` 31 | 32 | ### create_elem 33 | - t_node형 새로운 요소를 생성하는 함수를 작성하세요. 34 | ``` 35 | t_node *create_elem(void *data); 36 | ``` 37 | 38 | ### deque_push_front 39 | - deque의 가장 앞에 data를 갖는 새로운 요소를 생성하는 함수를 작성하세요. 40 | - 성공하면 1, 실패하면 0을 반환 합니다. 41 | ``` 42 | int deque_push_front(t_deque *deque, void *data); 43 | ``` 44 | 45 | ### deque_push_back 46 | - deque의 가장 뒤에 data를 갖는 새로운 요소를 생성하는 함수를 작성하세요. 47 | - 성공하면 1, 실패하면 0을 반환 합니다. 48 | ``` 49 | int deque_push_back(t_deque *deque, void *data); 50 | ``` 51 | 52 | ### deque_size 53 | - deque에 있는 요소의 개수를 반환하는 함수를 작성하세요. 54 | ``` 55 | int deque_size(t_deque *deque); 56 | ``` 57 | 58 | ### deque_front 59 | - deque에서 가장 앞에 있는 요소를 반환하는 함수를 작성하세요. 60 | ``` 61 | t_node *deque_front(t_deque *deque); 62 | ``` 63 | 64 | ### deque_back 65 | - deque에서 가장 뒤에 있는 요소를 반환하는 함수를 작성하세요. 66 | ``` 67 | t_node *deque_back(t_deque *deque); 68 | ``` 69 | 70 | ### deque_pop_front 71 | - deque에서 가장 앞에 있는 요소를 꺼내는 함수를 작성하세요. 72 | ``` 73 | t_node *deque_pop_front(t_deque *deque); 74 | ``` 75 | 76 | ### deque_pop_back 77 | - deque에서 가장 뒤에 있는 요소를 꺼내는 함수를 작성하세요. 78 | ``` 79 | t_node *deque_pop_back(t_deque *deque); 80 | ``` 81 | 82 | ### deque_clear 83 | - deque의 요소 전체를 삭제하는 함수를 작성하세요. 84 | - 요소의 데이터는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 85 | ``` 86 | void deque_clear(t_deque *deque, void (*free_data)(void *)); 87 | ``` 88 | 89 | ### free_deque 90 | - deque에 있는 요소 전체를 삭제하고 deque의 메모리 할당을 해제하는 함수를 작성하세요. 91 | - 요소의 데이터는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 92 | ``` 93 | void free_deque(t_deque *deque, void (*free_data)(void *)); 94 | ``` 95 | 96 | 97 | [뒤로 가기](..) 98 | -------------------------------------------------------------------------------- /tree/trie/implementation_example/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/12 16:21:01 by jko #+# #+# */ 9 | /* Updated: 2020/04/12 20:44:42 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "trie.h" 14 | #include 15 | 16 | static void print_str(t_node *node, char buf[4096], int index) 17 | { 18 | if (!node) 19 | return ; 20 | if (node->finish) 21 | { 22 | buf[index] = 0; 23 | printf("%s\n", buf); 24 | } 25 | for (int i = 0; i < 26; ++i) { 26 | buf[index] = i + 'a'; 27 | print_str(node->next[i], buf, index + 1); 28 | } 29 | } 30 | 31 | void print_trie(t_trie *trie) 32 | { 33 | char buf[4096]; 34 | 35 | for (int i = 0; i < 4096; ++i) { 36 | buf[i] = 0; 37 | } 38 | 39 | if (!trie) 40 | { 41 | printf("error\n"); 42 | return ; 43 | } 44 | for (int i = 0; i < 26; ++i) { 45 | buf[0] = i + 'a'; 46 | print_str((*trie)[i], buf, 1); 47 | } 48 | } 49 | 50 | int main(void) 51 | { 52 | char *strs[100] = { 53 | "abc", 54 | "z", 55 | "abz", 56 | "bb", 57 | "f", 58 | "qwertyuiopasdfghjklzxcvbnm" 59 | "zzzz", 60 | "wnoane", 61 | "", 62 | "acccc", 63 | 0 64 | }; 65 | 66 | t_trie *trie = trie_init(); 67 | print_trie(trie); 68 | 69 | for (int i = 0; strs[i]; ++i) { 70 | printf("insert result = %d\n", trie_insert(trie, strs[i])); 71 | print_trie(trie); 72 | printf("\n"); 73 | } 74 | 75 | for (int i = 0; strs[i]; ++i) { 76 | printf("%s find result = %d\n", strs[i], trie_find(trie, strs[i])); 77 | printf("\n"); 78 | } 79 | 80 | 81 | free_trie(trie); 82 | 83 | trie = 0; 84 | system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); 85 | 86 | return (0); 87 | } 88 | -------------------------------------------------------------------------------- /graph/undirected_list/README.md: -------------------------------------------------------------------------------- 1 | # Undirected Graph(adjacency list) 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free, printf 7 | - adjacency list를 이용하여 undirected graph를 구현 합니다. 8 | - 아래와 같은 graph.h를 사용 합니다. 9 | ``` 10 | #include 11 | 12 | typedef struct s_node 13 | { 14 | unsigned int vertex; 15 | struct s_node *next; 16 | } t_node; 17 | 18 | typedef struct s_graph 19 | { 20 | unsigned int size; 21 | void **data; 22 | t_node **list; 23 | } t_graph; 24 | ``` 25 | 26 | ### graph_init 27 | - t_graph형 struct를 반환 하는 함수를 작성하세요. 28 | - 반환되는 t_graph는 메모리 할당과 초기화를 거쳐야합니다. 29 | - size는 vertex의 갯수를 나타냅니다. 30 | ``` 31 | t_graph *graph_init(unsigned int size); 32 | ``` 33 | 34 | ### graph_set_data 35 | - graph에 data를 저장하는 함수를 작성하세요. 36 | - graph의 vertex에 data를 저장합니다. 37 | - 성공하면 true, 실패하면 false을 반환 합니다. 38 | ``` 39 | bool graph_set_data(t_graph *graph, unsigned int vertex, void *data); 40 | ``` 41 | 42 | ### graph_get_data 43 | - graph에 저장된 data를 반환하는 함수를 작성하세요. 44 | - 성공하면 vertex의 data, 실패하면 null pointer를 반환합니다. 45 | ``` 46 | void *graph_get_data(t_graph *graph, unsigned int vertex); 47 | ``` 48 | 49 | ### graph_set_edge 50 | - graph의 vertex 간 edge의 연결 상태를 변경하는 함수를 작성하세요. 51 | - state의 true는 연결된 상태, false는 연결되지 않은 상태를 의미합니다. 52 | - 성공하면 true, 실패하면 false을 반환 합니다. 53 | - true->true, false->false로의 상태 변화도 성공으로 간주합니다. 54 | ``` 55 | bool graph_set_edge(t_graph *graph, unsigned int vertex1, unsigned int vertex2, bool state); 56 | ``` 57 | 58 | ### graph_get_edge 59 | - graph의 vertex 간 edge의 연결 상태를 반환하는 함수를 작성하세요. 60 | - 두 vertex가 연결되어 있다면 true, 아니면 false을 반환 합니다. 61 | ``` 62 | bool graph_get_edge(t_graph *graph, unsigned int vertex1, unsigned int vertex2); 63 | ``` 64 | 65 | ### free_graph 66 | - graph에 있는 요소 전체를 삭제하고 graph의 메모리 할당을 해제하는 함수를 작성하세요. 67 | - data는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 68 | ``` 69 | void free_graph(t_graph *graph, void (*free_data)(void *)); 70 | ``` 71 | 72 | ### graph_traverse 73 | - graph를 순회하며 data를 출력하는 함수를 작성하세요. 74 | - 다음과 같은 형식으로 출력합니다. 75 | - `i번 vertex, data = i_data\n` 76 | - ex) 77 | ``` 78 | 0번 vertex, data = a 79 | 1번 vertex, data = b 80 | ``` 81 | - `stack`을 이용해 `DFS`을 구현합니다. 82 | - 순회의 시작은 항상 0번 vertex 입니다. 83 | - 하나의 vertex는 단 1번만 출력해야합니다. 84 | - print_data는 data를 출력하는 함수입니다.(print_data는 마지막에 개행을 포함하고 있습니다.) 85 | ``` 86 | void graph_traverse(t_graph *graph, void (*print_data)(void *)); 87 | ``` 88 | 89 | 90 | [뒤로 가기](..) 91 | -------------------------------------------------------------------------------- /linked_list/doubly_linked_list/grademe_files/doubly_linked_list_output: -------------------------------------------------------------------------------- 1 | index 0 -> 0 2 | 3 | index 0 -> 1 4 | index 1 -> 0 5 | 6 | index 0 -> 1 7 | index 1 -> 0 8 | index 2 -> 2 9 | 10 | index 0 -> 1 11 | index 1 -> 0 12 | index 2 -> 2 13 | index 3 -> 3 14 | 15 | index 0 -> 1 16 | index 1 -> 4 17 | index 2 -> 0 18 | index 3 -> 2 19 | index 4 -> 3 20 | 21 | index 0 -> 1 22 | index 1 -> 5 23 | index 2 -> 4 24 | index 3 -> 0 25 | index 4 -> 2 26 | index 5 -> 3 27 | 28 | 1 29 | 5 30 | 4 31 | 0 32 | index 0 -> 1 33 | index 1 -> 5 34 | index 2 -> 4 35 | index 3 -> 0 36 | index 4 -> 2 37 | index 5 -> 3 38 | index 6 -> 6 39 | 40 | index 0 -> 1 41 | index 1 -> 5 42 | index 2 -> 4 43 | index 3 -> 0 44 | index 4 -> 2 45 | index 5 -> 3 46 | index 6 -> 6 47 | index 7 -> 7 48 | 49 | index 0 -> 1 50 | index 1 -> 8 51 | index 2 -> 5 52 | index 3 -> 4 53 | index 4 -> 0 54 | index 5 -> 2 55 | index 6 -> 3 56 | index 7 -> 6 57 | index 8 -> 7 58 | 59 | index 0 -> 1 60 | index 1 -> 9 61 | index 2 -> 8 62 | index 3 -> 5 63 | index 4 -> 4 64 | index 5 -> 0 65 | index 6 -> 2 66 | index 7 -> 3 67 | index 8 -> 6 68 | index 9 -> 7 69 | 70 | 1 71 | index 0 -> 9 72 | index 1 -> 8 73 | index 2 -> 5 74 | index 3 -> 4 75 | index 4 -> 0 76 | index 5 -> 2 77 | index 6 -> 3 78 | index 7 -> 6 79 | index 8 -> 7 80 | 81 | rev index 8 -> 7 82 | rev index 7 -> 6 83 | rev index 6 -> 3 84 | rev index 5 -> 2 85 | rev index 4 -> 0 86 | rev index 3 -> 4 87 | rev index 2 -> 5 88 | rev index 1 -> 8 89 | rev index 0 -> 9 90 | 91 | 1 92 | index 0 -> 9 93 | index 1 -> 8 94 | index 2 -> 5 95 | index 3 -> 4 96 | index 4 -> 0 97 | index 5 -> 2 98 | index 6 -> 3 99 | index 7 -> 6 100 | 101 | rev index 7 -> 6 102 | rev index 6 -> 3 103 | rev index 5 -> 2 104 | rev index 4 -> 0 105 | rev index 3 -> 4 106 | rev index 2 -> 5 107 | rev index 1 -> 8 108 | rev index 0 -> 9 109 | 110 | 1 111 | index 0 -> 9 112 | index 1 -> 5 113 | index 2 -> 4 114 | index 3 -> 0 115 | index 4 -> 2 116 | index 5 -> 3 117 | index 6 -> 6 118 | 119 | rev index 6 -> 6 120 | rev index 5 -> 3 121 | rev index 4 -> 2 122 | rev index 3 -> 0 123 | rev index 2 -> 4 124 | rev index 1 -> 5 125 | rev index 0 -> 9 126 | 127 | 1 128 | index 0 -> 9 129 | index 1 -> 5 130 | index 2 -> 0 131 | index 3 -> 2 132 | index 4 -> 3 133 | index 5 -> 6 134 | 135 | rev index 5 -> 6 136 | rev index 4 -> 3 137 | rev index 3 -> 2 138 | rev index 2 -> 0 139 | rev index 1 -> 5 140 | rev index 0 -> 9 141 | 142 | index 0 -> 123 143 | 144 | -------------------------------------------------------------------------------- /graph/undirected_matrix/README.md: -------------------------------------------------------------------------------- 1 | # Undirected Graph(adjacency matrix) 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free, printf 7 | - adjacency matrix를 이용하여 undirected graph를 구현 합니다. 8 | - 아래와 같은 graph.h를 사용 합니다. 9 | ``` 10 | #include 11 | 12 | typedef struct s_graph 13 | { 14 | unsigned int size; 15 | void **data; 16 | bool **matrix; 17 | } t_graph; 18 | ``` 19 | 20 | ### graph_init 21 | - t_graph형 struct를 반환 하는 함수를 작성하세요. 22 | - 반환되는 t_graph는 메모리 할당과 초기화를 거쳐야합니다. 23 | - size는 vertex의 갯수를 나타냅니다. 24 | ``` 25 | t_graph *graph_init(unsigned int size); 26 | ``` 27 | 28 | ### graph_set_data 29 | - graph에 data를 저장하는 함수를 작성하세요. 30 | - graph의 vertex에 data를 저장합니다. 31 | - 성공하면 true, 실패하면 false을 반환 합니다. 32 | ``` 33 | bool graph_set_data(t_graph *graph, unsigned int vertex, void *data); 34 | ``` 35 | 36 | ### graph_get_data 37 | - graph에 저장된 data를 반환하는 함수를 작성하세요. 38 | - 성공하면 vertex의 data, 실패하면 null pointer를 반환합니다. 39 | ``` 40 | void *graph_get_data(t_graph *graph, unsigned int vertex); 41 | ``` 42 | 43 | ### graph_set_edge 44 | - graph의 vertex 간 edge의 연결 상태를 변경하는 함수를 작성하세요. 45 | - state의 true는 연결된 상태, false는 연결되지 않은 상태를 의미합니다. 46 | - 성공하면 true, 실패하면 false을 반환 합니다. 47 | - true->true, false->false로의 상태 변화도 성공으로 간주합니다. 48 | ``` 49 | bool graph_set_edge(t_graph *graph, unsigned int vertex1, unsigned int vertex2, bool state); 50 | ``` 51 | 52 | ### graph_get_edge 53 | - graph의 vertex 간 edge의 연결 상태를 반환하는 함수를 작성하세요. 54 | - 두 vertex가 연결되어 있다면 true, 아니면 false을 반환 합니다. 55 | ``` 56 | bool graph_get_edge(t_graph *graph, unsigned int vertex1, unsigned int vertex2); 57 | ``` 58 | 59 | ### free_graph 60 | - graph에 있는 요소 전체를 삭제하고 graph의 메모리 할당을 해제하는 함수를 작성하세요. 61 | - data는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 62 | ``` 63 | void free_graph(t_graph *graph, void (*free_data)(void *)); 64 | ``` 65 | 66 | ### graph_traverse 67 | - graph를 순회하며 data를 출력하는 함수를 작성하세요. 68 | - 다음과 같은 형식으로 출력합니다. 69 | - `i번 vertex, data = i_data\n` 70 | - ex) 71 | ``` 72 | 0번 vertex, data = a 73 | 1번 vertex, data = b 74 | ``` 75 | - `재귀 함수`를 이용해 `DFS`을 구현합니다. 76 | - 순회의 시작은 항상 0번 vertex 입니다. 77 | - 하나의 vertex는 단 1번만 출력해야합니다. 78 | - print_data는 data를 출력하는 함수입니다.(print_data는 마지막에 개행을 포함하고 있습니다.) 79 | ``` 80 | void graph_traverse(t_graph *graph, void (*print_data)(void *)); 81 | ``` 82 | 83 | ### make_graph 84 | - 다음과 같은 그래프를 생성하여 반환하는 함수를 작성하세요. 85 | ![undirected_graph_img](./undirected_graph.jpeg) 86 | - 성공하면 생성된 t_graph의 주소 값을, 실패하면 null pointer를 반환 합니다. 87 | - data들은 free()가 가능한 문자열입니다. 88 | ``` 89 | t_graph *make_graph(void); 90 | ``` 91 | 92 | 93 | [뒤로 가기](..) 94 | -------------------------------------------------------------------------------- /linked_list/doubly_linked_list/grademe_files/test.c: -------------------------------------------------------------------------------- 1 | #include "list.h" 2 | #include 3 | #include 4 | 5 | 6 | t_linked_list *list_init(void); 7 | t_node *create_elem(int data); 8 | int list_size(t_linked_list *list); 9 | t_node *list_get(t_linked_list *list, int n); 10 | int list_add(t_linked_list *list, int data, int n); 11 | int list_remove(t_linked_list *list, int n); 12 | void list_clear(t_linked_list *list); 13 | void free_list(t_linked_list *list); 14 | 15 | 16 | void print_list(t_linked_list *list); 17 | void print_list_rev(t_linked_list *list); 18 | 19 | 20 | int main(void) 21 | { 22 | t_linked_list *list = list_init(); 23 | 24 | int i = 0; 25 | list_add(list, i++, 0); 26 | print_list(list); 27 | list_add(list, i++, 0); 28 | print_list(list); 29 | list_add(list, i++, 12421321); 30 | print_list(list); 31 | list_add(list, i++, 3); 32 | print_list(list); 33 | 34 | 35 | list_add(list, i++, 1); 36 | print_list(list); 37 | list_add(list, i++, 1); 38 | print_list(list); 39 | 40 | printf("%d\n", list_get(list, 0)->data); 41 | printf("%d\n", list_get(list, 1)->data); 42 | printf("%d\n", list_get(list, 2)->data); 43 | printf("%d\n", list_get(list, 3)->data); 44 | 45 | list_add(list, i++, 1121242121); 46 | print_list(list); 47 | 48 | list_add(list, i++, list->size); 49 | print_list(list); 50 | 51 | list_add(list, i++, 1); 52 | print_list(list); 53 | 54 | list_add(list, i++, 1); 55 | print_list(list); 56 | 57 | printf("%d\n", list_remove(list, 0)); 58 | print_list(list); 59 | print_list_rev(list); 60 | printf("%d\n", list_remove(list, list->size - 1)); 61 | print_list(list); 62 | print_list_rev(list); 63 | printf("%d\n", list_remove(list, 1)); 64 | print_list(list); 65 | print_list_rev(list); 66 | printf("%d\n", list_remove(list, 2)); 67 | print_list(list); 68 | print_list_rev(list); 69 | 70 | list_clear(list); 71 | list_add(list, 123, 5); 72 | print_list(list); 73 | 74 | free_list(list); 75 | 76 | list = 0; 77 | system("leaks a.out > leaks_result"); 78 | return (0); 79 | } 80 | 81 | 82 | void print_list(t_linked_list *list) 83 | { 84 | t_node *curr = list->head; 85 | int i = 0; 86 | while (curr) 87 | { 88 | printf("index %d -> %d\n", i, curr->data); 89 | i++; 90 | curr = curr->next; 91 | } 92 | printf("\n"); 93 | } 94 | 95 | void print_list_rev(t_linked_list *list) 96 | { 97 | t_node *curr = list->tail; 98 | int i = list->size - 1; 99 | while (curr) 100 | { 101 | printf("rev index %d -> %d\n", i, curr->data); 102 | i--; 103 | curr = curr->prev; 104 | } 105 | printf("\n"); 106 | } 107 | -------------------------------------------------------------------------------- /queue/queue_array/implementation_example/queue_array_implementation.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* queue_array_implementation.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/24 16:09:35 by jko #+# #+# */ 9 | /* Updated: 2020/03/24 16:48:13 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | # include "queue.h" 14 | 15 | t_queue *queue_init(unsigned int max_size) 16 | { 17 | t_queue *queue; 18 | 19 | queue = (t_queue *)malloc(sizeof(t_queue)); 20 | if (!queue) 21 | return (0); 22 | queue->data = (void **)malloc(sizeof(void *) * max_size); 23 | if (!(queue->data)) 24 | { 25 | free(queue); 26 | return (0); 27 | } 28 | queue->max_size = max_size; 29 | queue->last_index = -1; 30 | return (queue); 31 | } 32 | 33 | int queue_size(t_queue *queue) 34 | { 35 | if (!queue || queue->last_index < 0) 36 | return (0); 37 | return (queue->last_index + 1); 38 | } 39 | 40 | int queue_push(t_queue *queue, void *data) 41 | { 42 | if (!queue || (unsigned int)queue_size(queue) >= queue->max_size) 43 | return (0); 44 | queue->last_index++; 45 | queue->data[queue->last_index] = data; 46 | return (1); 47 | } 48 | 49 | void *queue_front(t_queue *queue) 50 | { 51 | if (!queue || queue_size(queue) < 1) 52 | return (0); 53 | return (queue->data[0]); 54 | } 55 | 56 | void *queue_pop(t_queue *queue) 57 | { 58 | void *result; 59 | int i; 60 | 61 | if (!queue || queue_size(queue) < 1) 62 | return (0); 63 | result = queue->data[0]; 64 | i = 0; 65 | while (i < queue->last_index) 66 | { 67 | queue->data[i] = queue->data[i + 1]; 68 | i++; 69 | } 70 | queue->last_index--; 71 | return (result); 72 | } 73 | 74 | void queue_clear(t_queue *queue, void (*free_data)(void *)) 75 | { 76 | if (!queue || !free_data) 77 | return ; 78 | while (queue->last_index >= 0) 79 | { 80 | free_data(queue->data[queue->last_index]); 81 | queue->last_index--; 82 | } 83 | } 84 | 85 | void free_queue(t_queue *queue, void (*free_data)(void *)) 86 | { 87 | if (!queue || !free_data) 88 | return ; 89 | queue_clear(queue, free_data); 90 | free(queue->data); 91 | free(queue); 92 | } 93 | -------------------------------------------------------------------------------- /stack/stack_array/implementation_example/stack_array_implementation.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* stack_array_implementation.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/12 00:50:01 by jko #+# #+# */ 9 | /* Updated: 2020/04/10 23:45:33 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "stack.h" 14 | 15 | t_stack *stack_init(unsigned int max_size) 16 | { 17 | t_stack *stack; 18 | 19 | stack = (t_stack *)malloc(sizeof(t_stack)); 20 | if (stack == 0) 21 | return (0); 22 | stack->max_size = max_size; 23 | stack->top_index = -1; 24 | stack->data = (void **)malloc(sizeof(void *) * max_size); 25 | if (stack->data == 0) 26 | { 27 | free(stack); 28 | return (0); 29 | } 30 | return (stack); 31 | } 32 | 33 | int stack_push(t_stack *stack, void *data) 34 | { 35 | if (stack == 0 || stack->data == 0 36 | || (unsigned int)stack->top_index + 1 >= stack->max_size) 37 | return (0); 38 | stack->top_index++; 39 | stack->data[stack->top_index] = data; 40 | return (1); 41 | } 42 | 43 | int stack_size(t_stack *stack) 44 | { 45 | if (stack == 0) 46 | return (0); 47 | return (stack->top_index + 1); 48 | } 49 | 50 | void *stack_peek(t_stack *stack) 51 | { 52 | if (stack == 0 || stack->top_index < 0) 53 | return (0); 54 | return (stack->data[stack->top_index]); 55 | } 56 | 57 | void *stack_pop(t_stack *stack) 58 | { 59 | void *result; 60 | 61 | if (stack == 0 || stack->data == 0 || stack->top_index < 0) 62 | return (0); 63 | result = stack->data[stack->top_index]; 64 | stack->data[stack->top_index] = 0; 65 | stack->top_index--; 66 | return (result); 67 | } 68 | 69 | void stack_clear(t_stack *stack, void (*free_data)(void *)) 70 | { 71 | int i; 72 | 73 | if (stack == 0 || free_data == 0) 74 | return ; 75 | i = 0; 76 | while (i <= stack->top_index) 77 | { 78 | free_data(stack->data[i]); 79 | i++; 80 | } 81 | stack->top_index = -1; 82 | } 83 | 84 | void free_stack(t_stack *stack, void (*free_data)(void *)) 85 | { 86 | if (stack == 0 || free_data == 0) 87 | return ; 88 | stack_clear(stack, free_data); 89 | free(stack->data); 90 | } 91 | -------------------------------------------------------------------------------- /graph/directed_list/README.md: -------------------------------------------------------------------------------- 1 | # Directed Graph(adjacency list) 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free, printf 7 | - adjacency list를 이용하여 directed graph를 구현 합니다. 8 | - 아래와 같은 graph.h를 사용 합니다. 9 | ``` 10 | #include 11 | 12 | typedef struct s_node 13 | { 14 | unsigned int vertex; 15 | struct s_node *next; 16 | } t_node; 17 | 18 | typedef struct s_graph 19 | { 20 | unsigned int size; 21 | void **data; 22 | t_node **list; 23 | } t_graph; 24 | ``` 25 | 26 | ### graph_init 27 | - t_graph형 struct를 반환 하는 함수를 작성하세요. 28 | - 반환되는 t_graph는 메모리 할당과 초기화를 거쳐야합니다. 29 | - size는 vertex의 갯수를 나타냅니다. 30 | ``` 31 | t_graph *graph_init(unsigned int size); 32 | ``` 33 | 34 | ### graph_set_data 35 | - graph에 data를 저장하는 함수를 작성하세요. 36 | - graph의 vertex에 data를 저장합니다. 37 | - 성공하면 true, 실패하면 false을 반환 합니다. 38 | ``` 39 | bool graph_set_data(t_graph *graph, unsigned int vertex, void *data); 40 | ``` 41 | 42 | ### graph_get_data 43 | - graph에 저장된 data를 반환하는 함수를 작성하세요. 44 | - 성공하면 vertex의 data, 실패하면 null pointer를 반환합니다. 45 | ``` 46 | void *graph_get_data(t_graph *graph, unsigned int vertex); 47 | ``` 48 | 49 | ### graph_set_edge 50 | - graph의 `start->end`의 연결 상태를 변경하는 함수를 작성하세요. 51 | - state의 true는 연결된 상태, false는 연결되지 않은 상태를 의미합니다. 52 | - 성공하면 true, 실패하면 false을 반환 합니다. 53 | - true->true, false->false로의 상태 변화도 성공으로 간주합니다. 54 | ``` 55 | bool graph_set_edge(t_graph *graph, unsigned int start, unsigned int end, bool state); 56 | ``` 57 | 58 | ### graph_get_edge 59 | - graph의 vertex 간 edge의 연결 상태를 반환하는 함수를 작성하세요. 60 | - `start->end`가 연결되어 있다면 true, 아니면 false을 반환 합니다. 61 | ``` 62 | bool graph_get_edge(t_graph *graph, unsigned int start, unsigned int end); 63 | ``` 64 | 65 | ### free_graph 66 | - graph에 있는 요소 전체를 삭제하고 graph의 메모리 할당을 해제하는 함수를 작성하세요. 67 | - data는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 68 | ``` 69 | void free_graph(t_graph *graph, void (*free_data)(void *)); 70 | ``` 71 | 72 | ### graph_traverse 73 | - graph를 순회하며 data를 출력하는 함수를 작성하세요. 74 | - 다음과 같은 형식으로 출력합니다. 75 | - `i번 vertex, data = i_data\n` 76 | - ex) 77 | ``` 78 | 0번 vertex, data = a 79 | 1번 vertex, data = b 80 | ``` 81 | - `queue`을 이용해 `BFS`을 구현합니다. 82 | - 순회의 시작은 항상 0번 vertex 입니다. 83 | - 하나의 vertex는 단 1번만 출력해야합니다. 84 | - print_data는 data를 출력하는 함수입니다.(print_data는 마지막에 개행을 포함하고 있습니다.) 85 | ``` 86 | void graph_traverse(t_graph *graph, void (*print_data)(void *)); 87 | ``` 88 | 89 | ### make_graph 90 | - 다음과 같은 그래프를 생성하여 반환하는 함수를 작성하세요. 91 | ![directed_graph_img](./directed_graph.jpeg) 92 | - 성공하면 생성된 t_graph의 주소 값을, 실패하면 null pointer를 반환 합니다. 93 | - data들은 free()가 가능한 문자열입니다. 94 | ``` 95 | t_graph *make_graph(void); 96 | ``` 97 | 98 | 99 | [뒤로 가기](..) 100 | -------------------------------------------------------------------------------- /hash/rabin_karp/implementation/rabin_karp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* rabin_karp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/18 14:31:41 by jko #+# #+# */ 9 | /* Updated: 2020/04/19 15:41:50 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "rabin_karp.h" 14 | 15 | static unsigned int get_strlen(const char *str) 16 | { 17 | unsigned int len; 18 | 19 | if (!str) 20 | return (0); 21 | len = 0; 22 | while (str[len]) 23 | len++; 24 | return (len); 25 | } 26 | 27 | long long get_hash_value(const char *str, unsigned int len) 28 | { 29 | long long result; 30 | unsigned int str_len; 31 | unsigned int i; 32 | 33 | str_len = get_strlen(str); 34 | len = len < str_len ? len : str_len; 35 | result = 0; 36 | i = 0; 37 | while (i < len) 38 | { 39 | result = (result * POWER + str[i]) % BIG_PRIM; 40 | i++; 41 | } 42 | return (result); 43 | } 44 | 45 | static char *find( 46 | const char *str, 47 | unsigned int str_len, 48 | unsigned int pattern, 49 | unsigned int p_len) 50 | { 51 | long long hash; 52 | unsigned int i; 53 | unsigned int pow; 54 | 55 | hash = get_hash_value(str, p_len); 56 | if (hash == pattern) 57 | return ((char *)str); 58 | pow = 1; 59 | i = 1; 60 | while (i++ < p_len) 61 | pow = pow * POWER % BIG_PRIM; 62 | i = 1; 63 | while (i + p_len <= str_len) 64 | { 65 | hash = (hash - str[i - 1] * pow) % BIG_PRIM; 66 | if (hash < 0) 67 | hash += BIG_PRIM; 68 | hash = POWER * hash % BIG_PRIM; 69 | hash = (hash + str[i + p_len - 1]) % BIG_PRIM; 70 | if (hash == pattern) 71 | return ((char *)(str + i)); 72 | i++; 73 | } 74 | return (0); 75 | } 76 | 77 | char *find_str(const char *haystack, const char *needle) 78 | { 79 | unsigned int haystack_len; 80 | unsigned int needle_len; 81 | long long p; 82 | 83 | if (!haystack) 84 | return (0); 85 | if (!needle) 86 | return ((char *)haystack); 87 | haystack_len = get_strlen(haystack); 88 | needle_len = get_strlen(needle); 89 | if (haystack_len < needle_len) 90 | return (0); 91 | p = get_hash_value(needle, needle_len); 92 | return (find(haystack, haystack_len, p, needle_len)); 93 | } 94 | -------------------------------------------------------------------------------- /graph/prim/implementation/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/26 16:56:23 by jko #+# #+# */ 9 | /* Updated: 2020/04/26 16:56:30 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "graph.h" 14 | #include "prim.h" 15 | #include 16 | #include 17 | 18 | void print_graph(t_graph *graph) 19 | { 20 | uint i; 21 | t_node *curr; 22 | 23 | if (!graph) 24 | return ; 25 | i = 0; 26 | while (i < graph->size) 27 | { 28 | curr = graph->list[i]; 29 | while (curr) 30 | { 31 | printf("%2u -> %2u, %d\n", i, curr->vertex, curr->cost); 32 | curr = curr->next; 33 | } 34 | i++; 35 | } 36 | printf("\n"); 37 | } 38 | 39 | int main(void) 40 | { 41 | t_graph *graph = graph_init(6); 42 | t_graph *mst = 0; 43 | 44 | graph_set_data(graph, 0, strdup("a")); 45 | graph_set_data(graph, 1, strdup("b")); 46 | graph_set_data(graph, 2, strdup("c")); 47 | graph_set_data(graph, 3, strdup("d")); 48 | graph_set_data(graph, 4, strdup("e")); 49 | graph_set_data(graph, 5, strdup("f")); 50 | 51 | graph_set_edge(graph, 0, 1, true, 1); 52 | graph_set_edge(graph, 0, 2, true, 5); 53 | graph_set_edge(graph, 0, 3, true, 8); 54 | graph_set_edge(graph, 0, 4, true, 1); 55 | graph_set_edge(graph, 0, 5, true, 10); 56 | 57 | graph_set_edge(graph, 1, 2, true, 11); 58 | graph_set_edge(graph, 1, 3, true, 2); 59 | graph_set_edge(graph, 1, 4, true, 6); 60 | graph_set_edge(graph, 1, 5, true, 4); 61 | 62 | graph_set_edge(graph, 2, 3, true, 9); 63 | graph_set_edge(graph, 2, 4, true, 9); 64 | graph_set_edge(graph, 2, 5, true, 10); 65 | 66 | graph_set_edge(graph, 3, 4, true, 3); 67 | graph_set_edge(graph, 3, 5, true, 0); 68 | 69 | graph_set_edge(graph, 4, 5, true, 7); 70 | 71 | print_graph(graph); 72 | printf("\n"); 73 | 74 | 75 | mst = make_mst(graph); 76 | 77 | if (mst) 78 | { 79 | printf("** mst **\n"); 80 | print_graph(mst); 81 | } 82 | else 83 | printf("mst error\n"); 84 | 85 | free_graph(graph, free); 86 | free_graph(mst, free); 87 | 88 | graph = 0; 89 | mst = 0; 90 | 91 | system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); 92 | 93 | return (0); 94 | } 95 | -------------------------------------------------------------------------------- /graph/kruskal/implementation/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/25 15:02:58 by jko #+# #+# */ 9 | /* Updated: 2020/04/25 16:16:02 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "graph.h" 14 | #include "kruskal.h" 15 | #include 16 | #include 17 | 18 | void print_graph(t_graph *graph) 19 | { 20 | uint i; 21 | t_node *curr; 22 | 23 | if (!graph) 24 | return ; 25 | i = 0; 26 | while (i < graph->size) 27 | { 28 | curr = graph->list[i]; 29 | while (curr) 30 | { 31 | printf("%2u -> %2u, %d\n", i, curr->vertex, curr->cost); 32 | curr = curr->next; 33 | } 34 | i++; 35 | } 36 | printf("\n"); 37 | } 38 | 39 | int main(void) 40 | { 41 | t_graph *graph = graph_init(6); 42 | t_graph *mst = 0; 43 | 44 | graph_set_data(graph, 0, strdup("a")); 45 | graph_set_data(graph, 1, strdup("b")); 46 | graph_set_data(graph, 2, strdup("c")); 47 | graph_set_data(graph, 3, strdup("d")); 48 | graph_set_data(graph, 4, strdup("e")); 49 | graph_set_data(graph, 5, strdup("f")); 50 | 51 | graph_set_edge(graph, 0, 1, true, 1); 52 | graph_set_edge(graph, 0, 2, true, 5); 53 | graph_set_edge(graph, 0, 3, true, 8); 54 | graph_set_edge(graph, 0, 4, true, 1); 55 | graph_set_edge(graph, 0, 5, true, 10); 56 | 57 | graph_set_edge(graph, 1, 2, true, 11); 58 | graph_set_edge(graph, 1, 3, true, 2); 59 | graph_set_edge(graph, 1, 4, true, 6); 60 | graph_set_edge(graph, 1, 5, true, 4); 61 | 62 | graph_set_edge(graph, 2, 3, true, 9); 63 | graph_set_edge(graph, 2, 4, true, 9); 64 | graph_set_edge(graph, 2, 5, true, 10); 65 | 66 | graph_set_edge(graph, 3, 4, true, 3); 67 | graph_set_edge(graph, 3, 5, true, 0); 68 | 69 | graph_set_edge(graph, 4, 5, true, 7); 70 | 71 | print_graph(graph); 72 | printf("\n"); 73 | 74 | 75 | mst = make_mst(graph); 76 | 77 | if (mst) 78 | { 79 | printf("** mst **\n"); 80 | print_graph(mst); 81 | } 82 | else 83 | printf("mst error\n"); 84 | 85 | free_graph(graph, free); 86 | free_graph(mst, free); 87 | 88 | graph = 0; 89 | mst = 0; 90 | 91 | system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); 92 | 93 | return (0); 94 | } 95 | -------------------------------------------------------------------------------- /stack/stack_array/implementation_example/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/12 00:50:22 by jko #+# #+# */ 9 | /* Updated: 2020/04/10 23:45:57 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "stack.h" 14 | #include 15 | #include 16 | #include 17 | 18 | int main(void) 19 | { 20 | char *temp; 21 | t_stack *stack = stack_init(5); 22 | printf("size = %d\n", stack_size(stack)); 23 | printf("peek->data = %s\n", stack_peek(stack)); 24 | 25 | stack_push(stack, strdup("aa")); 26 | printf("size = %d\n", stack_size(stack)); 27 | printf("peek->data = %s\n", stack_peek(stack)); 28 | 29 | stack_push(stack, strdup("b")); 30 | printf("size = %d\n", stack_size(stack)); 31 | printf("peek->data = %s\n", stack_peek(stack)); 32 | 33 | stack_push(stack, strdup("ccc")); 34 | printf("size = %d\n", stack_size(stack)); 35 | printf("peek->data = %s\n", stack_peek(stack)); 36 | 37 | stack_push(stack, strdup("ddddd")); 38 | printf("size = %d\n", stack_size(stack)); 39 | printf("peek->data = %s\n", stack_peek(stack)); 40 | 41 | stack_push(stack, strdup("ee")); 42 | printf("size = %d\n", stack_size(stack)); 43 | printf("peek->data = %s\n", stack_peek(stack)); 44 | 45 | temp = strdup("f"); 46 | stack_push(stack, temp); 47 | printf("size = %d\n", stack_size(stack)); 48 | printf("peek->data = %s\n", stack_peek(stack)); 49 | free(temp); 50 | 51 | printf("=============== pop ===============\n"); 52 | for (int i = 0; i < 9; i++) 53 | { 54 | temp = stack_pop(stack); 55 | printf("pop = %s\n", temp); 56 | free(temp); 57 | printf("size = %d\n", stack_size(stack)); 58 | printf("peek->data = %s\n", stack_peek(stack)); 59 | } 60 | 61 | stack_clear(stack, free); 62 | 63 | stack_push(stack, strdup("e22")); 64 | printf("size = %d\n", stack_size(stack)); 65 | printf("peek->data = %s\n", stack_peek(stack)); 66 | 67 | stack_push(stack, strdup("f22")); 68 | printf("size = %d\n", stack_size(stack)); 69 | printf("peek->data = %s\n", stack_peek(stack)); 70 | 71 | free_stack(stack, free); 72 | 73 | stack = 0; 74 | system("leaks a.out > leaks_result; cat leaks_result | grep leaked"); 75 | return (0); 76 | } 77 | -------------------------------------------------------------------------------- /graph/undirected_list/implementation/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/22 22:05:16 by jko #+# #+# */ 9 | /* Updated: 2020/04/23 00:49:10 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "graph.h" 14 | #include 15 | 16 | void print_data(void *data) 17 | { 18 | printf("%s\n", (char *)data); 19 | } 20 | 21 | int main(void) 22 | { 23 | 24 | t_graph *graph = graph_init(5); 25 | graph_traverse(graph, print_data); 26 | printf("\n"); 27 | 28 | graph_set_data(graph, 0, strdup("a")); 29 | graph_set_data(graph, 1, strdup("b")); 30 | graph_set_data(graph, 2, strdup("c")); 31 | graph_set_data(graph, 3, strdup("d")); 32 | graph_set_data(graph, 4, strdup("e")); 33 | graph_traverse(graph, print_data); 34 | printf("\n"); 35 | 36 | graph_set_edge(graph, 0, 1, true); 37 | graph_traverse(graph, print_data); 38 | printf("\n"); 39 | 40 | graph_set_edge(graph, 0, 2, true); 41 | graph_traverse(graph, print_data); 42 | printf("\n"); 43 | 44 | graph_set_edge(graph, 1, 2, true); 45 | graph_traverse(graph, print_data); 46 | printf("\n"); 47 | 48 | graph_set_edge(graph, 3, 2, true); 49 | graph_traverse(graph, print_data); 50 | printf("\n"); 51 | 52 | graph_set_edge(graph, 3, 4, true); 53 | graph_traverse(graph, print_data); 54 | printf("\n"); 55 | 56 | graph_set_edge(graph, 1, 0, false); 57 | graph_traverse(graph, print_data); 58 | printf("\n"); 59 | 60 | graph_set_edge(graph, 3, 0, true); 61 | graph_traverse(graph, print_data); 62 | printf("\n"); 63 | 64 | graph_set_edge(graph, 3, 2, false); 65 | graph_traverse(graph, print_data); 66 | printf("\n"); 67 | 68 | graph_set_edge(graph, 2, 4, true); 69 | graph_traverse(graph, print_data); 70 | printf("\n"); 71 | 72 | printf("0번 data = %s\n", graph_get_data(graph, 0)); 73 | printf("1번 data = %s\n", graph_get_data(graph, 1)); 74 | printf("1, 4번 edge = %d\n", graph_get_edge(graph, 1, 4)); 75 | printf("2, 3번 edge = %d\n", graph_get_edge(graph, 2, 3)); 76 | printf("0, 1번 edge = %d\n\n", graph_get_edge(graph, 0, 1)); 77 | 78 | free_graph(graph, free); 79 | 80 | 81 | system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); 82 | 83 | return (0); 84 | } 85 | -------------------------------------------------------------------------------- /stack/stack_linked_list/implementation_example/stack_linked_list_implementation.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* stack_linked_list_implementation.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/12 01:05:04 by jko #+# #+# */ 9 | /* Updated: 2020/05/14 11:46:55 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "stack.h" 14 | 15 | t_stack *stack_init(void) 16 | { 17 | t_stack *stack; 18 | 19 | stack = (t_stack *)malloc(sizeof(t_stack)); 20 | if (stack == 0) 21 | return (0); 22 | stack->size = 0; 23 | stack->top = 0; 24 | return (stack); 25 | } 26 | 27 | t_node *create_elem(void *data) 28 | { 29 | t_node *node; 30 | 31 | node = (t_node *)malloc(sizeof(t_node)); 32 | if (node == 0) 33 | return (0); 34 | node->data = data; 35 | node->next = 0; 36 | return (node); 37 | } 38 | 39 | int stack_push(t_stack *stack, void *data) 40 | { 41 | t_node *new_node; 42 | 43 | if (stack == 0 || (new_node = create_elem(data)) == 0) 44 | return (0); 45 | new_node->next = stack->top; 46 | stack->top = new_node; 47 | stack->size++; 48 | return (1); 49 | } 50 | 51 | int stack_size(t_stack *stack) 52 | { 53 | if (stack == 0) 54 | return (0); 55 | return (stack->size); 56 | } 57 | 58 | t_node *stack_peek(t_stack *stack) 59 | { 60 | if (stack == 0) 61 | return (0); 62 | return (stack->top); 63 | } 64 | 65 | t_node *stack_pop(t_stack *stack) 66 | { 67 | t_node *node; 68 | 69 | if (stack == 0 || stack->size <= 0) 70 | return (0); 71 | stack->size--; 72 | node = stack->top; 73 | stack->top = node->next; 74 | node->next = 0; 75 | return (node); 76 | } 77 | 78 | void stack_clear(t_stack *stack, void (*free_data)(void *)) 79 | { 80 | t_node *curr; 81 | t_node *temp; 82 | 83 | if (stack == 0 || free_data == 0) 84 | return ; 85 | curr = stack->top; 86 | stack->top = 0; 87 | stack->size = 0; 88 | while (curr) 89 | { 90 | temp = curr; 91 | curr = curr->next; 92 | temp->next = 0; 93 | free_data(temp->data); 94 | free(temp); 95 | } 96 | } 97 | 98 | void free_stack(t_stack *stack, void (*free_data)(void *)) 99 | { 100 | if (stack == 0 || free_data == 0) 101 | return ; 102 | stack_clear(stack, free_data); 103 | free(stack); 104 | } 105 | -------------------------------------------------------------------------------- /graph/dijkstra/implementation/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/06/06 12:07:30 by jko #+# #+# */ 9 | /* Updated: 2020/06/08 22:25:19 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "graph.h" 14 | #include "dijkstra.h" 15 | #include 16 | #include 17 | 18 | void print_graph(t_graph *graph) 19 | { 20 | uint i; 21 | t_node *curr; 22 | 23 | if (!graph) 24 | return ; 25 | i = 0; 26 | while (i < graph->size) 27 | { 28 | curr = graph->list[i]; 29 | while (curr) 30 | { 31 | printf("%2u -> %2u, %d\n", i, curr->vertex, curr->cost); 32 | curr = curr->next; 33 | } 34 | i++; 35 | } 36 | printf("\n"); 37 | } 38 | 39 | int main(void) 40 | { 41 | t_graph *graph = graph_init(6); 42 | uint *path = 0; 43 | 44 | graph_set_data(graph, 0, strdup("a")); 45 | graph_set_data(graph, 1, strdup("b")); 46 | graph_set_data(graph, 2, strdup("c")); 47 | graph_set_data(graph, 3, strdup("d")); 48 | graph_set_data(graph, 4, strdup("e")); 49 | graph_set_data(graph, 5, strdup("f")); 50 | 51 | graph_set_edge(graph, 0, 1, true, 1); 52 | graph_set_edge(graph, 0, 2, true, 5); 53 | graph_set_edge(graph, 0, 3, true, 8); 54 | 55 | graph_set_edge(graph, 1, 2, true, 11); 56 | graph_set_edge(graph, 1, 4, true, 6); 57 | graph_set_edge(graph, 1, 5, true, 4); 58 | 59 | graph_set_edge(graph, 2, 3, true, 9); 60 | graph_set_edge(graph, 2, 5, true, 10); 61 | 62 | graph_set_edge(graph, 3, 4, true, 3); 63 | graph_set_edge(graph, 3, 5, true, 0); 64 | 65 | graph_set_edge(graph, 4, 5, true, 7); 66 | 67 | graph_set_edge(graph, 5, 0, true, 3); 68 | 69 | print_graph(graph); 70 | printf("\n"); 71 | 72 | 73 | for (uint i = 0; i < graph->size; ++i) { 74 | path = find_shortest_path(graph, i); 75 | 76 | if (path) 77 | { 78 | printf("** %d start shortest path cost **\n", i); 79 | for (uint j = 0; j < graph->size; ++j) { 80 | printf("vertex %d : cost %d\n", j, path[j]); 81 | } 82 | } 83 | else 84 | printf("shortest path error\n"); 85 | 86 | free(path); 87 | path = 0; 88 | } 89 | 90 | free_graph(graph, free); 91 | graph = 0; 92 | 93 | system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); 94 | 95 | return (0); 96 | } 97 | -------------------------------------------------------------------------------- /queue/queue_linked_list/implementation_example/queue_linked_list_implementation.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* queue_linked_list_implementation.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/24 16:09:35 by jko #+# #+# */ 9 | /* Updated: 2020/03/24 17:46:26 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | # include "queue.h" 14 | 15 | t_queue *queue_init(void) 16 | { 17 | t_queue *queue; 18 | 19 | queue = (t_queue *)malloc(sizeof(t_queue)); 20 | if (!queue) 21 | return (0); 22 | queue->size = 0; 23 | queue->head = 0; 24 | queue->tail = 0; 25 | return (queue); 26 | } 27 | 28 | t_node *create_elem(void *data) 29 | { 30 | t_node *new; 31 | 32 | new = (t_node *)malloc(sizeof(t_node)); 33 | if (!new) 34 | return (0); 35 | new->data = data; 36 | new->next = 0; 37 | return (new); 38 | } 39 | 40 | int queue_push(t_queue *queue, void *data) 41 | { 42 | t_node *new; 43 | 44 | if (!queue || !(new = create_elem(data))) 45 | return (0); 46 | if (queue->size == 0) 47 | queue->head = new; 48 | else 49 | queue->tail->next = new; 50 | queue->tail = new; 51 | queue->size++; 52 | return (1); 53 | } 54 | 55 | int queue_size(t_queue *queue) 56 | { 57 | if (!queue) 58 | return (0); 59 | return (queue->size); 60 | } 61 | 62 | t_node *queue_front(t_queue *queue) 63 | { 64 | if (!queue) 65 | return (0); 66 | return (queue->head); 67 | } 68 | 69 | t_node *queue_pop(t_queue *queue) 70 | { 71 | t_node *result; 72 | 73 | if (!queue || queue_size(queue) < 1) 74 | return (0); 75 | result = queue->head; 76 | queue->head = result->next; 77 | if (queue_size(queue) == 1) 78 | queue->tail = 0; 79 | queue->size--; 80 | return (result); 81 | } 82 | 83 | void queue_clear(t_queue *queue, void (*free_data)(void *)) 84 | { 85 | t_node *curr; 86 | t_node *temp; 87 | 88 | if (!queue || !free_data) 89 | return ; 90 | curr = queue->head; 91 | while (curr) 92 | { 93 | temp = curr; 94 | curr = curr->next; 95 | free_data(temp->data); 96 | free(temp); 97 | } 98 | queue->size = 0; 99 | queue->head = 0; 100 | queue->tail = 0; 101 | } 102 | 103 | void free_queue(t_queue *queue, void (*free_data)(void *)) 104 | { 105 | if (!queue || !free_data) 106 | return ; 107 | queue_clear(queue, free_data); 108 | free(queue); 109 | } 110 | -------------------------------------------------------------------------------- /graph/undirected_matrix/implementation/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/21 21:15:35 by jko #+# #+# */ 9 | /* Updated: 2020/04/22 22:41:41 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "graph.h" 14 | #include 15 | 16 | void print_data(void *data) 17 | { 18 | printf("%s\n", (char *)data); 19 | } 20 | 21 | int main(void) 22 | { 23 | 24 | t_graph *graph = graph_init(5); 25 | graph_traverse(graph, print_data); 26 | printf("\n"); 27 | 28 | graph_set_data(graph, 0, strdup("a")); 29 | graph_set_data(graph, 1, strdup("b")); 30 | graph_set_data(graph, 2, strdup("c")); 31 | graph_set_data(graph, 3, strdup("d")); 32 | graph_set_data(graph, 4, strdup("e")); 33 | graph_traverse(graph, print_data); 34 | printf("\n"); 35 | 36 | graph_set_edge(graph, 0, 1, true); 37 | graph_traverse(graph, print_data); 38 | printf("\n"); 39 | 40 | graph_set_edge(graph, 0, 2, true); 41 | graph_traverse(graph, print_data); 42 | printf("\n"); 43 | 44 | graph_set_edge(graph, 1, 2, true); 45 | graph_traverse(graph, print_data); 46 | printf("\n"); 47 | 48 | graph_set_edge(graph, 3, 2, true); 49 | graph_traverse(graph, print_data); 50 | printf("\n"); 51 | 52 | graph_set_edge(graph, 1, 4, true); 53 | graph_traverse(graph, print_data); 54 | printf("\n"); 55 | 56 | graph_set_edge(graph, 1, 2, false); 57 | graph_traverse(graph, print_data); 58 | printf("\n"); 59 | 60 | printf("0번 data = %s\n", graph_get_data(graph, 0)); 61 | printf("1번 data = %s\n", graph_get_data(graph, 1)); 62 | printf("1, 4번 edge = %d\n", graph_get_edge(graph, 1, 4)); 63 | printf("2, 3번 edge = %d\n", graph_get_edge(graph, 2, 3)); 64 | printf("0, 1번 edge = %d\n\n", graph_get_edge(graph, 0, 1)); 65 | 66 | free_graph(graph, free); 67 | 68 | 69 | graph = make_graph(); 70 | graph_traverse(graph, print_data); 71 | printf("\n"); 72 | 73 | graph_set_edge(graph, 1, 0, false); 74 | graph_traverse(graph, print_data); 75 | printf("\n"); 76 | 77 | graph_set_edge(graph, 1, 2, false); 78 | graph_traverse(graph, print_data); 79 | printf("\n"); 80 | 81 | graph_set_edge(graph, 4, 2, true); 82 | graph_traverse(graph, print_data); 83 | printf("\n"); 84 | 85 | free_graph(graph, free); 86 | 87 | 88 | system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); 89 | 90 | return (0); 91 | } 92 | -------------------------------------------------------------------------------- /graph/directed_list/implementation/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/23 18:38:06 by jko #+# #+# */ 9 | /* Updated: 2020/04/23 20:31:02 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "graph.h" 14 | #include 15 | 16 | void print_data(void *data) 17 | { 18 | printf("%s\n", (char *)data); 19 | } 20 | 21 | int main(void) 22 | { 23 | 24 | t_graph *graph = graph_init(5); 25 | graph_traverse(graph, print_data); 26 | printf("\n"); 27 | 28 | graph_set_data(graph, 0, strdup("a")); 29 | graph_set_data(graph, 1, strdup("b")); 30 | graph_set_data(graph, 2, strdup("c")); 31 | graph_set_data(graph, 3, strdup("d")); 32 | graph_set_data(graph, 4, strdup("e")); 33 | graph_traverse(graph, print_data); 34 | printf("\n"); 35 | 36 | graph_set_edge(graph, 0, 1, true); 37 | graph_traverse(graph, print_data); 38 | printf("\n"); 39 | 40 | graph_set_edge(graph, 0, 2, true); 41 | graph_traverse(graph, print_data); 42 | printf("\n"); 43 | 44 | graph_set_edge(graph, 1, 2, true); 45 | graph_traverse(graph, print_data); 46 | printf("\n"); 47 | 48 | graph_set_edge(graph, 3, 2, true); 49 | graph_traverse(graph, print_data); 50 | printf("\n"); 51 | 52 | graph_set_edge(graph, 3, 4, true); 53 | graph_traverse(graph, print_data); 54 | printf("\n"); 55 | 56 | graph_set_edge(graph, 1, 0, false); 57 | graph_traverse(graph, print_data); 58 | printf("\n"); 59 | 60 | graph_set_edge(graph, 3, 0, true); 61 | graph_traverse(graph, print_data); 62 | printf("\n"); 63 | 64 | graph_set_edge(graph, 3, 2, false); 65 | graph_traverse(graph, print_data); 66 | printf("\n"); 67 | 68 | graph_set_edge(graph, 2, 4, true); 69 | graph_traverse(graph, print_data); 70 | printf("\n"); 71 | 72 | printf("0번 data = %s\n", graph_get_data(graph, 0)); 73 | printf("1번 data = %s\n", graph_get_data(graph, 1)); 74 | printf("1, 4번 edge = %d\n", graph_get_edge(graph, 1, 4)); 75 | printf("2, 3번 edge = %d\n", graph_get_edge(graph, 2, 3)); 76 | printf("0, 1번 edge = %d\n\n", graph_get_edge(graph, 0, 1)); 77 | 78 | free_graph(graph, free); 79 | 80 | 81 | graph = make_graph(); 82 | if (graph) 83 | graph_traverse(graph, print_data); 84 | free_graph(graph, free); 85 | 86 | 87 | graph = 0; 88 | 89 | system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); 90 | 91 | return (0); 92 | } 93 | -------------------------------------------------------------------------------- /graph/floyd-warshall/implementation/fw.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* fw.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/06/09 12:12:38 by jko #+# #+# */ 9 | /* Updated: 2020/06/09 13:39:35 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "fw.h" 14 | 15 | static int **free_result(int **result, uint size) 16 | { 17 | uint i; 18 | 19 | i = 0; 20 | while (i < size) 21 | { 22 | free(result[i]); 23 | i++; 24 | } 25 | free(result); 26 | return (0); 27 | } 28 | 29 | static void fill_initial_cost(const t_graph *src, int **result) 30 | { 31 | t_node *node; 32 | uint i; 33 | 34 | i = 0; 35 | while (i < src->size) 36 | { 37 | node = src->list[i]; 38 | while (node) 39 | { 40 | result[i][node->vertex] = node->cost; 41 | node = node->next; 42 | } 43 | if (result[i][i] > 0) 44 | result[i][i] = 0; 45 | i++; 46 | } 47 | } 48 | 49 | static int **init_result(const t_graph *src) 50 | { 51 | int **result; 52 | uint i; 53 | uint j; 54 | 55 | if (!(result = malloc(sizeof(int *) * src->size))) 56 | return (0); 57 | i = 0; 58 | while (i < src->size) 59 | { 60 | if (!(result[i] = malloc(sizeof(int) * src->size))) 61 | return (free_result(result, i)); 62 | i++; 63 | } 64 | i = 0; 65 | while (i < src->size) 66 | { 67 | j = 0; 68 | while (j < src->size) 69 | { 70 | result[i][j] = INT_MAX; 71 | j++; 72 | } 73 | i++; 74 | } 75 | fill_initial_cost(src, result); 76 | return (result); 77 | } 78 | 79 | static int check_int_range(int a, int b) 80 | { 81 | int temp; 82 | 83 | temp = a + b; 84 | if (temp < 0 && a >= 0 && b >= 0) 85 | temp = INT_MAX; 86 | else if (temp >= 0 && a < 0 && b < 0) 87 | temp = INT_MIN; 88 | return (temp); 89 | } 90 | 91 | int **find_shortest_path(const t_graph *src) 92 | { 93 | int **result; 94 | uint i; 95 | uint j; 96 | uint k; 97 | int temp; 98 | 99 | if (!src || !(result = init_result(src))) 100 | return (0); 101 | k = 0; 102 | while (k < src->size) 103 | { 104 | i = 0; 105 | while (i < src->size) 106 | { 107 | j = 0; 108 | while (j < src->size) 109 | { 110 | temp = check_int_range(result[i][k], result[k][j]); 111 | if (temp < result[i][j]) 112 | result[i][j] = temp; 113 | j++; 114 | } 115 | i++; 116 | } 117 | k++; 118 | } 119 | return (result); 120 | } 121 | -------------------------------------------------------------------------------- /tree/trie/implementation_example/trie.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* trie.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/12 16:24:17 by jko #+# #+# */ 9 | /* Updated: 2020/04/12 19:30:52 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "trie.h" 14 | 15 | t_trie *trie_init(void) 16 | { 17 | t_trie *trie; 18 | int i; 19 | 20 | trie = malloc(sizeof(t_trie)); 21 | if (!trie) 22 | return (0); 23 | i = 0; 24 | while (i < 26) 25 | { 26 | (*trie)[i] = 0; 27 | i++; 28 | } 29 | return (trie); 30 | } 31 | 32 | static t_node *create_node(void) 33 | { 34 | t_node *node; 35 | int i; 36 | 37 | node = malloc(sizeof(t_node)); 38 | if (!node) 39 | return (0); 40 | i = 0; 41 | while (i < 26) 42 | { 43 | node->next[i] = 0; 44 | i++; 45 | } 46 | node->finish = false; 47 | return (node); 48 | } 49 | 50 | static bool insert_str(t_node *node, char *str) 51 | { 52 | if (!node || !str) 53 | return (false); 54 | if (!str[0]) 55 | { 56 | node->finish = true; 57 | return (true); 58 | } 59 | if (!node->next[str[0] - 'a']) 60 | node->next[str[0] - 'a'] = create_node(); 61 | return (insert_str(node->next[str[0] - 'a'], str + 1)); 62 | } 63 | 64 | bool trie_insert(t_trie *trie, char *str) 65 | { 66 | if (!trie || !str || !str[0]) 67 | return (false); 68 | if (!(*trie)[str[0] - 'a']) 69 | (*trie)[str[0] - 'a'] = create_node(); 70 | return (insert_str((*trie)[str[0] - 'a'], str + 1)); 71 | } 72 | 73 | static bool find_str(t_node *node, char *str) 74 | { 75 | if (!node || !str) 76 | return (false); 77 | if (!str[0]) 78 | return (node->finish); 79 | if (!node->next[str[0] - 'a']) 80 | return (false); 81 | return (find_str(node->next[str[0] -'a'], str + 1)); 82 | } 83 | 84 | bool trie_find(t_trie *trie, char *str) 85 | { 86 | if (!trie || !str || !str[0]) 87 | return (false); 88 | return (find_str((*trie)[str[0] - 'a'], str + 1)); 89 | } 90 | 91 | static void free_node(t_node *node) 92 | { 93 | int i; 94 | 95 | if (!node) 96 | return ; 97 | i = 0; 98 | while (i < 26) 99 | { 100 | free_node(node->next[i]); 101 | i++; 102 | } 103 | free(node); 104 | } 105 | 106 | void free_trie(t_trie *trie) 107 | { 108 | int i; 109 | 110 | if (!trie) 111 | return ; 112 | i = 0; 113 | while (i < 26) 114 | { 115 | free_node((*trie)[i]); 116 | i++; 117 | } 118 | free(trie); 119 | } 120 | -------------------------------------------------------------------------------- /graph/bellman-ford/implementation/bf.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* bf.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/06/07 16:13:32 by jko #+# #+# */ 9 | /* Updated: 2020/06/09 11:34:29 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "bf.h" 14 | 15 | static int *init_result(const t_graph *src, uint start) 16 | { 17 | int *result; 18 | t_node *node; 19 | uint i; 20 | 21 | if (!(result = malloc(sizeof(int) * src->size))) 22 | return (0); 23 | i = 0; 24 | while (i < src->size) 25 | { 26 | result[i] = INT_MAX; 27 | i++; 28 | } 29 | node = src->list[start]; 30 | while (node) 31 | { 32 | result[node->vertex] = node->cost; 33 | node = node->next; 34 | } 35 | result[start] = 0; 36 | return (result); 37 | } 38 | 39 | static void relax_edge(const t_graph *src, int **result) 40 | { 41 | uint i; 42 | uint j; 43 | int temp; 44 | t_node *node; 45 | 46 | i = 0; 47 | while (i < src->size - 1) 48 | { 49 | j = 0; 50 | while (j < src->size) 51 | { 52 | node = src->list[j]; 53 | while (node) 54 | { 55 | temp = (*result)[j] + node->cost; 56 | if ((*result)[j] >= 0 && node->cost >= 0 && temp < 0) 57 | temp = INT_MAX; 58 | else if ((*result)[j] < 0 && node->cost < 0 && temp >= 0) 59 | temp = INT_MIN; 60 | if ((*result)[node->vertex] > temp) 61 | (*result)[node->vertex] = temp; 62 | node = node->next; 63 | } 64 | j++; 65 | } 66 | i++; 67 | } 68 | } 69 | 70 | static int check_negative_cycle(const t_graph *src, int **result) 71 | { 72 | t_node *node; 73 | uint i; 74 | int temp; 75 | 76 | i = 0; 77 | while (i < src->size) 78 | { 79 | node = src->list[i]; 80 | while (node) 81 | { 82 | temp = (*result)[i] + node->cost; 83 | if ((*result)[i] >= 0 && node->cost >= 0 && temp < 0) 84 | temp = INT_MAX; 85 | else if ((*result)[i] < 0 && node->cost < 0 && temp >= 0) 86 | temp = INT_MIN; 87 | if ((*result)[node->vertex] > temp) 88 | { 89 | free(*result); 90 | *result = 0; 91 | return (NEGATIVE_CYCLE); 92 | } 93 | node = node->next; 94 | } 95 | i++; 96 | } 97 | return (SUCCESS); 98 | } 99 | 100 | int find_shortest_path(const t_graph *src, uint start, int **result) 101 | { 102 | if (!src || src->size <= start || !(*result = init_result(src, start))) 103 | { 104 | *result = 0; 105 | return (FAILURE); 106 | } 107 | relax_edge(src, result); 108 | return (check_negative_cycle(src, result)); 109 | } 110 | -------------------------------------------------------------------------------- /stack/stack_linked_list/implementation_example/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/12 00:50:22 by jko #+# #+# */ 9 | /* Updated: 2020/06/05 16:07:47 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "stack.h" 14 | #include 15 | #include 16 | #include 17 | 18 | int main(void) 19 | { 20 | t_node *temp; 21 | t_stack *stack = stack_init(); 22 | printf("size = %d\n", stack_size(stack)); 23 | printf("peek->data = %s\n", stack_peek(stack) ? stack_peek(stack)->data : 0); 24 | 25 | stack_push(stack, strdup("aa")); 26 | printf("size = %d\n", stack_size(stack)); 27 | printf("peek->data = %s\n", stack_peek(stack) ? stack_peek(stack)->data : 0); 28 | 29 | stack_push(stack, strdup("b")); 30 | printf("size = %d\n", stack_size(stack)); 31 | printf("peek->data = %s\n", stack_peek(stack) ? stack_peek(stack)->data : 0); 32 | 33 | stack_push(stack, strdup("ccc")); 34 | printf("size = %d\n", stack_size(stack)); 35 | printf("peek->data = %s\n", stack_peek(stack) ? stack_peek(stack)->data : 0); 36 | 37 | stack_push(stack, strdup("ddddd")); 38 | printf("size = %d\n", stack_size(stack)); 39 | printf("peek->data = %s\n", stack_peek(stack) ? stack_peek(stack)->data : 0); 40 | 41 | stack_push(stack, strdup("ee")); 42 | printf("size = %d\n", stack_size(stack)); 43 | printf("peek->data = %s\n", stack_peek(stack) ? stack_peek(stack)->data : 0); 44 | 45 | stack_push(stack, strdup("f")); 46 | printf("size = %d\n", stack_size(stack)); 47 | printf("peek->data = %s\n", stack_peek(stack) ? stack_peek(stack)->data : 0); 48 | 49 | printf("=============== pop ===============\n"); 50 | for (int i = 0; i < 9; i++) 51 | { 52 | temp = stack_pop(stack); 53 | printf("pop = %s\n", temp ? temp->data : 0); 54 | free(temp ? temp->data : 0); 55 | free(temp); 56 | printf("size = %d\n", stack_size(stack)); 57 | printf("peek->data = %s\n", stack_peek(stack) ? stack_peek(stack)->data : 0); 58 | } 59 | 60 | stack_clear(stack, free); 61 | 62 | stack_push(stack, strdup("e22")); 63 | printf("size = %d\n", stack_size(stack)); 64 | printf("peek->data = %s\n", stack_peek(stack) ? stack_peek(stack)->data : 0); 65 | 66 | stack_push(stack, strdup("f22")); 67 | printf("size = %d\n", stack_size(stack)); 68 | printf("peek->data = %s\n", stack_peek(stack) ? stack_peek(stack)->data : 0); 69 | 70 | free_stack(stack, free); 71 | 72 | temp = 0; 73 | stack = 0; 74 | 75 | system("leaks a.out > leaks_result; cat leaks_result | grep leaked"); 76 | 77 | return (0); 78 | } 79 | -------------------------------------------------------------------------------- /graph/kruskal/implementation/heap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* heap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/09 15:49:29 by jko #+# #+# */ 9 | /* Updated: 2020/04/11 00:03:44 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "heap.h" 14 | 15 | t_heap *heap_init(unsigned int max_size, int (*cmp)(void *, void *)) 16 | { 17 | t_heap *heap; 18 | 19 | if (max_size < 1 || !cmp) 20 | return (0); 21 | if (!(heap = malloc(sizeof(t_heap)))) 22 | return (0); 23 | if (!(heap->data = malloc(sizeof(void *) * (max_size + 1)))) 24 | { 25 | free(heap); 26 | return (0); 27 | } 28 | heap->max_size = max_size; 29 | heap->cmp = cmp; 30 | heap->size = 0; 31 | return (heap); 32 | } 33 | 34 | int heap_push(t_heap *heap, void *data) 35 | { 36 | unsigned int i; 37 | void *temp; 38 | 39 | if (!heap || heap->size >= heap->max_size) 40 | return (0); 41 | heap->size++; 42 | i = heap->size; 43 | heap->data[i] = data; 44 | while (i / 2 > 0 && heap->cmp(heap->data[i / 2], heap->data[i]) > 0) 45 | { 46 | temp = heap->data[i / 2]; 47 | heap->data[i / 2] = heap->data[i]; 48 | heap->data[i] = temp; 49 | i /= 2; 50 | } 51 | return (1); 52 | } 53 | 54 | void *heap_peek(t_heap *heap) 55 | { 56 | if (!heap || heap->size < 1) 57 | return (0); 58 | return (heap->data[1]); 59 | } 60 | 61 | static void fill_node(t_heap *heap) 62 | { 63 | unsigned int i; 64 | unsigned int j; 65 | unsigned int k; 66 | void *temp; 67 | 68 | j = 1; 69 | while ((i = j)) 70 | { 71 | j = i * 2 <= heap->size && 72 | heap->cmp(heap->data[i], heap->data[i * 2]) > 0; 73 | k = i * 2 + 1 <= heap->size 74 | && heap->cmp(heap->data[i], heap->data[i * 2 + 1]) > 0; 75 | if (j && k) 76 | j = heap->cmp(heap->data[i * 2], heap->data[i * 2 + 1]) 77 | < 0 ? i * 2 : i * 2 + 1; 78 | else if (j) 79 | j = i * 2; 80 | else if (k) 81 | j = i * 2 + 1; 82 | else 83 | break; 84 | temp = heap->data[i]; 85 | heap->data[i] = heap->data[j]; 86 | heap->data[j] = temp; 87 | } 88 | } 89 | 90 | void *heap_pop(t_heap *heap) 91 | { 92 | void *result; 93 | 94 | if (!heap || heap->size < 1) 95 | return (0); 96 | result = heap->data[1]; 97 | heap->data[1] = heap->data[heap->size--]; 98 | fill_node(heap); 99 | return (result); 100 | } 101 | 102 | void free_heap(t_heap *heap, void (*free_data)(void *)) 103 | { 104 | if (!heap || !free_data) 105 | return ; 106 | while (heap->size > 0) 107 | { 108 | free_data(heap->data[heap->size]); 109 | heap->size--; 110 | } 111 | free(heap->data); 112 | free(heap); 113 | } 114 | -------------------------------------------------------------------------------- /graph/prim/implementation/heap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* heap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/09 15:49:29 by jko #+# #+# */ 9 | /* Updated: 2020/04/11 00:03:44 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "heap.h" 14 | 15 | t_heap *heap_init(unsigned int max_size, int (*cmp)(void *, void *)) 16 | { 17 | t_heap *heap; 18 | 19 | if (max_size < 1 || !cmp) 20 | return (0); 21 | if (!(heap = malloc(sizeof(t_heap)))) 22 | return (0); 23 | if (!(heap->data = malloc(sizeof(void *) * (max_size + 1)))) 24 | { 25 | free(heap); 26 | return (0); 27 | } 28 | heap->max_size = max_size; 29 | heap->cmp = cmp; 30 | heap->size = 0; 31 | return (heap); 32 | } 33 | 34 | int heap_push(t_heap *heap, void *data) 35 | { 36 | unsigned int i; 37 | void *temp; 38 | 39 | if (!heap || heap->size >= heap->max_size) 40 | return (0); 41 | heap->size++; 42 | i = heap->size; 43 | heap->data[i] = data; 44 | while (i / 2 > 0 && heap->cmp(heap->data[i / 2], heap->data[i]) > 0) 45 | { 46 | temp = heap->data[i / 2]; 47 | heap->data[i / 2] = heap->data[i]; 48 | heap->data[i] = temp; 49 | i /= 2; 50 | } 51 | return (1); 52 | } 53 | 54 | void *heap_peek(t_heap *heap) 55 | { 56 | if (!heap || heap->size < 1) 57 | return (0); 58 | return (heap->data[1]); 59 | } 60 | 61 | static void fill_node(t_heap *heap) 62 | { 63 | unsigned int i; 64 | unsigned int j; 65 | unsigned int k; 66 | void *temp; 67 | 68 | j = 1; 69 | while ((i = j)) 70 | { 71 | j = i * 2 <= heap->size && 72 | heap->cmp(heap->data[i], heap->data[i * 2]) > 0; 73 | k = i * 2 + 1 <= heap->size 74 | && heap->cmp(heap->data[i], heap->data[i * 2 + 1]) > 0; 75 | if (j && k) 76 | j = heap->cmp(heap->data[i * 2], heap->data[i * 2 + 1]) 77 | < 0 ? i * 2 : i * 2 + 1; 78 | else if (j) 79 | j = i * 2; 80 | else if (k) 81 | j = i * 2 + 1; 82 | else 83 | break; 84 | temp = heap->data[i]; 85 | heap->data[i] = heap->data[j]; 86 | heap->data[j] = temp; 87 | } 88 | } 89 | 90 | void *heap_pop(t_heap *heap) 91 | { 92 | void *result; 93 | 94 | if (!heap || heap->size < 1) 95 | return (0); 96 | result = heap->data[1]; 97 | heap->data[1] = heap->data[heap->size--]; 98 | fill_node(heap); 99 | return (result); 100 | } 101 | 102 | void free_heap(t_heap *heap, void (*free_data)(void *)) 103 | { 104 | if (!heap || !free_data) 105 | return ; 106 | while (heap->size > 0) 107 | { 108 | free_data(heap->data[heap->size]); 109 | heap->size--; 110 | } 111 | free(heap->data); 112 | free(heap); 113 | } 114 | -------------------------------------------------------------------------------- /graph/dijkstra/implementation/heap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* heap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/09 15:49:29 by jko #+# #+# */ 9 | /* Updated: 2020/04/11 00:03:44 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "heap.h" 14 | 15 | t_heap *heap_init(unsigned int max_size, int (*cmp)(void *, void *)) 16 | { 17 | t_heap *heap; 18 | 19 | if (max_size < 1 || !cmp) 20 | return (0); 21 | if (!(heap = malloc(sizeof(t_heap)))) 22 | return (0); 23 | if (!(heap->data = malloc(sizeof(void *) * (max_size + 1)))) 24 | { 25 | free(heap); 26 | return (0); 27 | } 28 | heap->max_size = max_size; 29 | heap->cmp = cmp; 30 | heap->size = 0; 31 | return (heap); 32 | } 33 | 34 | int heap_push(t_heap *heap, void *data) 35 | { 36 | unsigned int i; 37 | void *temp; 38 | 39 | if (!heap || heap->size >= heap->max_size) 40 | return (0); 41 | heap->size++; 42 | i = heap->size; 43 | heap->data[i] = data; 44 | while (i / 2 > 0 && heap->cmp(heap->data[i / 2], heap->data[i]) > 0) 45 | { 46 | temp = heap->data[i / 2]; 47 | heap->data[i / 2] = heap->data[i]; 48 | heap->data[i] = temp; 49 | i /= 2; 50 | } 51 | return (1); 52 | } 53 | 54 | void *heap_peek(t_heap *heap) 55 | { 56 | if (!heap || heap->size < 1) 57 | return (0); 58 | return (heap->data[1]); 59 | } 60 | 61 | static void fill_node(t_heap *heap) 62 | { 63 | unsigned int i; 64 | unsigned int j; 65 | unsigned int k; 66 | void *temp; 67 | 68 | j = 1; 69 | while ((i = j)) 70 | { 71 | j = i * 2 <= heap->size && 72 | heap->cmp(heap->data[i], heap->data[i * 2]) > 0; 73 | k = i * 2 + 1 <= heap->size 74 | && heap->cmp(heap->data[i], heap->data[i * 2 + 1]) > 0; 75 | if (j && k) 76 | j = heap->cmp(heap->data[i * 2], heap->data[i * 2 + 1]) 77 | < 0 ? i * 2 : i * 2 + 1; 78 | else if (j) 79 | j = i * 2; 80 | else if (k) 81 | j = i * 2 + 1; 82 | else 83 | break; 84 | temp = heap->data[i]; 85 | heap->data[i] = heap->data[j]; 86 | heap->data[j] = temp; 87 | } 88 | } 89 | 90 | void *heap_pop(t_heap *heap) 91 | { 92 | void *result; 93 | 94 | if (!heap || heap->size < 1) 95 | return (0); 96 | result = heap->data[1]; 97 | heap->data[1] = heap->data[heap->size--]; 98 | fill_node(heap); 99 | return (result); 100 | } 101 | 102 | void free_heap(t_heap *heap, void (*free_data)(void *)) 103 | { 104 | if (!heap || !free_data) 105 | return ; 106 | while (heap->size > 0) 107 | { 108 | free_data(heap->data[heap->size]); 109 | heap->size--; 110 | } 111 | free(heap->data); 112 | free(heap); 113 | } 114 | -------------------------------------------------------------------------------- /tree/heap/implementation_example/heap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* heap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/09 15:49:29 by jko #+# #+# */ 9 | /* Updated: 2020/04/11 00:03:44 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "heap.h" 14 | 15 | t_heap *heap_init(unsigned int max_size, int (*cmp)(void *, void *)) 16 | { 17 | t_heap *heap; 18 | 19 | if (max_size < 1 || !cmp) 20 | return (0); 21 | if (!(heap = malloc(sizeof(t_heap)))) 22 | return (0); 23 | if (!(heap->data = malloc(sizeof(void *) * (max_size + 1)))) 24 | { 25 | free(heap); 26 | return (0); 27 | } 28 | heap->max_size = max_size; 29 | heap->cmp = cmp; 30 | heap->size = 0; 31 | return (heap); 32 | } 33 | 34 | int heap_push(t_heap *heap, void *data) 35 | { 36 | unsigned int i; 37 | void *temp; 38 | 39 | if (!heap || heap->size >= heap->max_size) 40 | return (0); 41 | heap->size++; 42 | i = heap->size; 43 | heap->data[i] = data; 44 | while (i / 2 > 0 && heap->cmp(heap->data[i / 2], heap->data[i]) > 0) 45 | { 46 | temp = heap->data[i / 2]; 47 | heap->data[i / 2] = heap->data[i]; 48 | heap->data[i] = temp; 49 | i /= 2; 50 | } 51 | return (1); 52 | } 53 | 54 | void *heap_peek(t_heap *heap) 55 | { 56 | if (!heap || heap->size < 1) 57 | return (0); 58 | return (heap->data[1]); 59 | } 60 | 61 | static void fill_node(t_heap *heap) 62 | { 63 | unsigned int i; 64 | unsigned int j; 65 | unsigned int k; 66 | void *temp; 67 | 68 | j = 1; 69 | while ((i = j)) 70 | { 71 | j = i * 2 <= heap->size && 72 | heap->cmp(heap->data[i], heap->data[i * 2]) > 0; 73 | k = i * 2 + 1 <= heap->size 74 | && heap->cmp(heap->data[i], heap->data[i * 2 + 1]) > 0; 75 | if (j && k) 76 | j = heap->cmp(heap->data[i * 2], heap->data[i * 2 + 1]) 77 | < 0 ? i * 2 : i * 2 + 1; 78 | else if (j) 79 | j = i * 2; 80 | else if (k) 81 | j = i * 2 + 1; 82 | else 83 | break; 84 | temp = heap->data[i]; 85 | heap->data[i] = heap->data[j]; 86 | heap->data[j] = temp; 87 | } 88 | } 89 | 90 | void *heap_pop(t_heap *heap) 91 | { 92 | void *result; 93 | 94 | if (!heap || heap->size < 1) 95 | return (0); 96 | result = heap->data[1]; 97 | heap->data[1] = heap->data[heap->size--]; 98 | fill_node(heap); 99 | return (result); 100 | } 101 | 102 | void free_heap(t_heap *heap, void (*free_data)(void *)) 103 | { 104 | if (!heap || !free_data) 105 | return ; 106 | while (heap->size > 0) 107 | { 108 | free_data(heap->data[heap->size]); 109 | heap->size--; 110 | } 111 | free(heap->data); 112 | free(heap); 113 | } 114 | -------------------------------------------------------------------------------- /tree/union_find/implementation_example/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/12 16:21:01 by jko #+# #+# */ 9 | /* Updated: 2020/04/13 17:55:07 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "union_find.h" 14 | #include 15 | 16 | void print_nodes(t_node *nodes[20]) 17 | { 18 | for (int i = 0; i < 20; ++i) { 19 | printf("index[%02d] parent = %2d\n", i, *(int *)find(nodes[i])->data); 20 | } 21 | printf("\n"); 22 | } 23 | 24 | int main(void) 25 | { 26 | int data[20]; 27 | t_node *nodes[20]; 28 | 29 | for (int i = 0; i < 20; ++i) { 30 | data[i] = i; 31 | nodes[i] = create_elem(&data[i]); 32 | } 33 | 34 | print_nodes(nodes); 35 | 36 | printf("1, 2 is disjoint = %d\n", is_disjoint(nodes[1], nodes[2])); 37 | printf("union result root = %d\n", *(int *)union_func(nodes[1], nodes[2])->data); 38 | printf("1, 2 is disjoint = %d\n", is_disjoint(nodes[1], nodes[2])); 39 | print_nodes(nodes); 40 | 41 | printf("3, 4 is disjoint = %d\n", is_disjoint(nodes[3], nodes[4])); 42 | printf("union result root = %d\n", *(int *)union_func(nodes[3], nodes[4])->data); 43 | printf("3, 4 is disjoint = %d\n", is_disjoint(nodes[3], nodes[4])); 44 | print_nodes(nodes); 45 | 46 | printf("4, 1 is disjoint = %d\n", is_disjoint(nodes[4], nodes[1])); 47 | printf("union result root = %d\n", *(int *)union_func(nodes[4], nodes[1])->data); 48 | printf("4, 1 is disjoint = %d\n", is_disjoint(nodes[4], nodes[1])); 49 | print_nodes(nodes); 50 | 51 | printf("5, 15 is disjoint = %d\n", is_disjoint(nodes[5], nodes[15])); 52 | printf("union result root = %d\n", *(int *)union_func(nodes[5], nodes[15])->data); 53 | printf("5, 15 is disjoint = %d\n", is_disjoint(nodes[5], nodes[15])); 54 | print_nodes(nodes); 55 | 56 | printf("8, 4 is disjoint = %d\n", is_disjoint(nodes[8], nodes[4])); 57 | printf("union result root = %d\n", *(int *)union_func(nodes[8], nodes[4])->data); 58 | printf("8, 4 is disjoint = %d\n", is_disjoint(nodes[8], nodes[4])); 59 | print_nodes(nodes); 60 | 61 | printf("8, 15 is disjoint = %d\n", is_disjoint(nodes[8], nodes[15])); 62 | printf("union result root = %d\n", *(int *)union_func(nodes[8], nodes[15])->data); 63 | printf("8, 15 is disjoint = %d\n", is_disjoint(nodes[8], nodes[15])); 64 | print_nodes(nodes); 65 | 66 | printf("18, 11 is disjoint = %d\n", is_disjoint(nodes[18], nodes[11])); 67 | printf("union result root = %d\n", *(int *)union_func(nodes[18], nodes[11])->data); 68 | printf("18, 11 is disjoint = %d\n", is_disjoint(nodes[18], nodes[11])); 69 | print_nodes(nodes); 70 | 71 | 72 | for (int i = 0; i < 20; ++i) { 73 | free(nodes[i]); 74 | } 75 | 76 | system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); 77 | 78 | return (0); 79 | } 80 | -------------------------------------------------------------------------------- /linked_list/circular_linked_list/README.md: -------------------------------------------------------------------------------- 1 | # Circular Linked List 2 | 3 | [뒤로 가기](..) 4 | 5 | ## exercise 00 6 | - allowed functions : malloc, free 7 | - list의 head가 바뀌는 경우는 4가지 입니다. 8 | 1. 빈 list에 노드를 추가할 때(list의 size가 0에서 1이 될 때) 9 | 1. head가 가리키는 노드가 삭제될 때(해당 노드의 next가 head가 됩니다.) 10 | 1. head를 이동하는 함수 list_move_head_to_next를 사용할 때 11 | 1. head를 이동하는 함수 list_move_head_to_prev를 사용할 때 12 | - list의 인덱스는 항상 head가 가리키는 노드가 0번 입니다. 13 | - circular linked list에서는 인덱스가 순환합니다. 14 | - ex) 사이즈가 5일 때, 0-1-2-3-4-0-1-2-3-4-... 15 | - 사이즈가 5이고 인덱스 5번 -> 0 (한바퀴 돌아서 다시 0번으로 옴) 16 | - 사이즈가 5이고 인덱스가 -2번 -> 3 (뒤에서 2번째) 17 | - 아래와 같은 list.h를 사용 합니다. 18 | ``` 19 | typedef struct s_node 20 | { 21 | void *data; 22 | struct s_node *prev; 23 | struct s_node *next; 24 | } t_node; 25 | 26 | typedef struct s_linked_list 27 | { 28 | unsigned int size; 29 | t_node *head; 30 | } t_linked_list; 31 | ``` 32 | 33 | ### list_init 34 | - t_linked_list형 struct를 반환 하는 함수를 작성하세요. 35 | - 반환되는 t_linked_list는 메모리 할당과 초기화를 거쳐야합니다. 36 | ``` 37 | t_linked_list *list_init(void); 38 | ``` 39 | 40 | ### create_elem 41 | - t_node형 새로운 요소를 생성하는 함수를 작성하세요. 42 | ``` 43 | t_node *create_elem(void *data); 44 | ``` 45 | 46 | ### list_add 47 | - 목록의 n번 인덱스에 data를 갖는 새로운 요소를 생성하는 함수를 작성하세요. 48 | - 생성된 요소의 인덱스를 반환 합니다. (0 혹은 양수) 49 | - 실패 시 -1을 반환 합니다. 50 | ``` 51 | int list_add(t_linked_list *list, void *data, int n); 52 | ``` 53 | 54 | ### list_size 55 | - 목록에 있는 요소의 개수를 반환하는 함수를 작성하세요. 56 | ``` 57 | int list_size(t_linked_list *list); 58 | ``` 59 | 60 | ### list_move_head_to_next 61 | - list의 head를 head의 next로 옮기는 함수를 작성하세요. 62 | ``` 63 | void list_move_head_to_next(t_linked_list *list); 64 | ``` 65 | 66 | ### list_move_head_to_prev 67 | - list의 head를 head의 prev로 옮기는 함수를 작성하세요. 68 | ``` 69 | void list_move_head_to_prev(t_linked_list *list); 70 | ``` 71 | 72 | ### list_get 73 | - 목록에서 n번 인덱스의 요소를 반환하는 함수를 작성하세요. 74 | - 실패 시 널포인터를 반환 합니다. 75 | ``` 76 | t_node *list_get(t_linked_list *list, int n); 77 | ``` 78 | 79 | ### list_find 80 | - 목록에서 data의 값이 같은 요소의 인덱스를 반환하는 함수를 작성하세요. (0 혹은 양수) 81 | - 실패 시 -1를 반환 합니다. 82 | - 매개변수로 주어진 cmp 함수는 2개의 data를 받고 두 data가 같으면 0, 다르면 1을 반환 합니다. 83 | ``` 84 | int list_find(t_linked_list *list, void* data, int (*cmp)(void *, void *)); 85 | ``` 86 | 87 | ### list_remove 88 | - 목록에서 n번 인덱스의 요소를 삭제하는 함수를 작성하세요. 89 | - 요소의 데이터는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 90 | ``` 91 | void list_remove(t_linked_list *list, int n, void (*free_data)(void *)); 92 | ``` 93 | 94 | ### list_clear 95 | - 목록 전체를 삭제하는 함수를 작성하세요. 96 | - 요소의 데이터는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 97 | ``` 98 | void list_clear(t_linked_list *list, void (*free_data)(void *)); 99 | ``` 100 | 101 | ### free_list 102 | - 목록의 메모리 할당을 해제하는 함수를 작성하세요. 103 | - 요소의 데이터는 free_data를 사용해서 메모리 할당을 해제해야 합니다. 104 | - 목록에 있는 요소와 목록 모두 해제해야 합니다. 105 | ``` 106 | void free_list(t_linked_list *list, void (*free_data)(void *)); 107 | ``` 108 | 109 | ### list_foreach 110 | - 목록에 있는 모든 요소의 data에 매개변수로 주어진 함수 f를 적용하는 함수를 작성하세요. 111 | ``` 112 | void list_foreach(t_linked_list *list, void (*f)(void *)); 113 | ``` 114 | 115 | 116 | ## grademe 117 | - `grademe.sh`, `grademe_files`가 있는 디렉토리에 소스코드를 넣는다 (main함수는 지우거나 주석처리) 118 | - 터미널 명령어 `sh ./grademe.sh`를 입력 119 | - 테스트 코드의 출력을 `user_output` 파일로, memory leaks 체크 결과를 `leaks_result` 파일로 생성합니다. 120 | - 결과를 diff -u 명령어로 비교하고 less 명령어로 출력해줍니다.(`u` 위로, `d` 아래로, `q` 종료) 121 | 122 | [뒤로 가기](..) 123 | -------------------------------------------------------------------------------- /sort/heap/implementation/heap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* heap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/04/14 17:07:18 by jko #+# #+# */ 9 | /* Updated: 2020/04/14 17:40:47 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "heap.h" 14 | 15 | static int heap_init(t_heap *heap, int max_size, int (*cmp)(void *, void *)) 16 | { 17 | if (!(heap->data = malloc(sizeof(void *) * (max_size + 1)))) 18 | return (0); 19 | heap->max_size = (unsigned int)max_size; 20 | heap->cmp = cmp; 21 | heap->size = 0; 22 | return (1); 23 | } 24 | 25 | static int heap_push(t_heap *heap, void *data) 26 | { 27 | unsigned int i; 28 | void *temp; 29 | 30 | if (!heap || heap->size >= heap->max_size) 31 | return (0); 32 | heap->size++; 33 | i = heap->size; 34 | heap->data[i] = data; 35 | while (i / 2 > 0 && heap->cmp(heap->data[i / 2], heap->data[i]) > 0) 36 | { 37 | temp = heap->data[i / 2]; 38 | heap->data[i / 2] = heap->data[i]; 39 | heap->data[i] = temp; 40 | i /= 2; 41 | } 42 | return (1); 43 | } 44 | 45 | static void fill_node(t_heap *heap) 46 | { 47 | unsigned int i; 48 | unsigned int j; 49 | unsigned int k; 50 | void *temp; 51 | 52 | j = 1; 53 | while ((i = j)) 54 | { 55 | j = i * 2 <= heap->size && 56 | heap->cmp(heap->data[i], heap->data[i * 2]) > 0; 57 | k = i * 2 + 1 <= heap->size 58 | && heap->cmp(heap->data[i], heap->data[i * 2 + 1]) > 0; 59 | if (j && k) 60 | j = heap->cmp(heap->data[i * 2], heap->data[i * 2 + 1]) 61 | < 0 ? i * 2 : i * 2 + 1; 62 | else if (j) 63 | j = i * 2; 64 | else if (k) 65 | j = i * 2 + 1; 66 | else 67 | break; 68 | temp = heap->data[i]; 69 | heap->data[i] = heap->data[j]; 70 | heap->data[j] = temp; 71 | } 72 | } 73 | 74 | static void *heap_pop(t_heap *heap) 75 | { 76 | void *result; 77 | 78 | if (!heap || heap->size < 1) 79 | return (0); 80 | result = heap->data[1]; 81 | heap->data[1] = heap->data[heap->size--]; 82 | fill_node(heap); 83 | return (result); 84 | } 85 | 86 | static int free_heap(t_heap *heap, int return_value) 87 | { 88 | free(heap->data); 89 | return (return_value); 90 | } 91 | 92 | int heap_sort(void **items, int size, int (*cmp)(void *, void *)) 93 | { 94 | t_heap heap; 95 | int i; 96 | 97 | if (!items || size < 2 || !cmp || !heap_init(&heap, size, cmp)) 98 | return (0); 99 | i = 0; 100 | while (i < size) 101 | { 102 | if (!heap_push(&heap, items[i])) 103 | return (free_heap(&heap, 0)); 104 | i++; 105 | } 106 | i = 0; 107 | while (i < size) 108 | { 109 | if (!(items[i] = heap_pop(&heap))) 110 | return (free_heap(&heap, 0)); 111 | i++; 112 | } 113 | return (free_heap(&heap, 1)); 114 | } 115 | -------------------------------------------------------------------------------- /graph/floyd-warshall/implementation/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/06/09 12:55:42 by jko #+# #+# */ 9 | /* Updated: 2020/06/09 13:00:39 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "graph.h" 14 | #include "fw.h" 15 | #include 16 | #include 17 | 18 | void print_graph(t_graph *graph) 19 | { 20 | uint i; 21 | t_node *curr; 22 | 23 | if (!graph) 24 | return ; 25 | i = 0; 26 | while (i < graph->size) 27 | { 28 | curr = graph->list[i]; 29 | while (curr) 30 | { 31 | printf("%2u -> %2u, %d\n", i, curr->vertex, curr->cost); 32 | curr = curr->next; 33 | } 34 | i++; 35 | } 36 | printf("\n"); 37 | } 38 | 39 | void print_sp(t_graph *graph) 40 | { 41 | int **result = 0; 42 | 43 | if (!(result = find_shortest_path(graph))) 44 | { 45 | printf("shortest path error\n"); 46 | return ; 47 | } 48 | 49 | printf("** shortest path cost **\n"); 50 | for (uint i = 0; i < graph->size; ++i) { 51 | for (uint j = 0; j < graph->size; ++j) { 52 | printf("%d\t", result[i][j]); 53 | } 54 | printf("\n"); 55 | free(result[i]); 56 | result[i] = 0; 57 | } 58 | free(result); 59 | result = 0; 60 | } 61 | 62 | int main(void) 63 | { 64 | t_graph *graph = graph_init(6); 65 | 66 | graph_set_data(graph, 0, strdup("a")); 67 | graph_set_data(graph, 1, strdup("b")); 68 | graph_set_data(graph, 2, strdup("c")); 69 | graph_set_data(graph, 3, strdup("d")); 70 | graph_set_data(graph, 4, strdup("e")); 71 | graph_set_data(graph, 5, strdup("f")); 72 | 73 | graph_set_edge(graph, 0, 1, true, 1); 74 | graph_set_edge(graph, 0, 2, true, 5); 75 | graph_set_edge(graph, 0, 3, true, 8); 76 | 77 | graph_set_edge(graph, 1, 2, true, 11); 78 | graph_set_edge(graph, 1, 4, true, 6); 79 | graph_set_edge(graph, 1, 5, true, 4); 80 | 81 | graph_set_edge(graph, 2, 3, true, 9); 82 | graph_set_edge(graph, 2, 5, true, 10); 83 | 84 | graph_set_edge(graph, 3, 4, true, 3); 85 | graph_set_edge(graph, 3, 5, true, 0); 86 | 87 | graph_set_edge(graph, 4, 5, true, 7); 88 | 89 | graph_set_edge(graph, 5, 0, true, 3); 90 | 91 | 92 | print_graph(graph); 93 | printf("\n"); 94 | 95 | print_sp(graph); 96 | printf("\n"); 97 | 98 | 99 | 100 | graph_set_edge(graph, 3, 0, true, -1); 101 | 102 | print_graph(graph); 103 | printf("\n"); 104 | 105 | print_sp(graph); 106 | printf("\n"); 107 | 108 | 109 | 110 | graph_set_edge(graph, 2, 0, true, -9); 111 | 112 | print_graph(graph); 113 | printf("\n"); 114 | 115 | print_sp(graph); 116 | printf("\n"); 117 | 118 | 119 | 120 | 121 | free_graph(graph, free); 122 | graph = 0; 123 | 124 | system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); 125 | 126 | return (0); 127 | } 128 | -------------------------------------------------------------------------------- /queue/queue_array/implementation_example/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/24 16:36:35 by jko #+# #+# */ 9 | /* Updated: 2020/04/05 19:18:55 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | 14 | #include "queue.h" 15 | #include 16 | #include 17 | #include 18 | 19 | void print_queue(t_queue *queue) 20 | { 21 | if (!queue) 22 | { 23 | printf("queue == null_ptr\n"); 24 | return ; 25 | } 26 | printf("print queue start\n"); 27 | for (int i = 0; i <= queue->last_index; ++i) { 28 | printf("q [%d] -> %s\n", i, queue->data[i]); 29 | } 30 | printf("end\n\n"); 31 | } 32 | 33 | int main(void) 34 | { 35 | char *temp; 36 | t_queue *queue = queue_init(5); 37 | printf("size = %d\n", queue_size(queue)); 38 | printf("front->data = %s\n", queue_front(queue)); 39 | print_queue(queue); 40 | 41 | queue_push(queue, strdup("aa")); 42 | printf("size = %d\n", queue_size(queue)); 43 | printf("front->data = %s\n", queue_front(queue)); 44 | print_queue(queue); 45 | 46 | queue_push(queue, strdup("b")); 47 | printf("size = %d\n", queue_size(queue)); 48 | printf("front->data = %s\n", queue_front(queue)); 49 | print_queue(queue); 50 | 51 | queue_push(queue, strdup("ccc")); 52 | printf("size = %d\n", queue_size(queue)); 53 | printf("front->data = %s\n", queue_front(queue)); 54 | print_queue(queue); 55 | 56 | queue_push(queue, strdup("ddddd")); 57 | printf("size = %d\n", queue_size(queue)); 58 | printf("front->data = %s\n", queue_front(queue)); 59 | print_queue(queue); 60 | 61 | queue_push(queue, strdup("ee")); 62 | printf("size = %d\n", queue_size(queue)); 63 | printf("front->data = %s\n", queue_front(queue)); 64 | print_queue(queue); 65 | 66 | temp = strdup("f"); 67 | queue_push(queue, temp); 68 | printf("size = %d\n", queue_size(queue)); 69 | printf("front->data = %s\n", queue_front(queue)); 70 | print_queue(queue); 71 | free(temp); 72 | 73 | printf("=============== pop ===============\n"); 74 | for (int i = 0; i < 9; i++) 75 | { 76 | temp = queue_pop(queue); 77 | printf("pop = %s\n", temp); 78 | free(temp); 79 | printf("size = %d\n", queue_size(queue)); 80 | printf("front->data = %s\n", queue_front(queue)); 81 | print_queue(queue); 82 | } 83 | 84 | queue_clear(queue, free); 85 | print_queue(queue); 86 | 87 | queue_push(queue, strdup("e22")); 88 | printf("size = %d\n", queue_size(queue)); 89 | printf("front->data = %s\n", queue_front(queue)); 90 | print_queue(queue); 91 | 92 | queue_push(queue, strdup("f22")); 93 | printf("size = %d\n", queue_size(queue)); 94 | printf("front->data = %s\n", queue_front(queue)); 95 | print_queue(queue); 96 | 97 | free_queue(queue, free); 98 | 99 | queue = 0; 100 | system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); 101 | return (0); 102 | } 103 | -------------------------------------------------------------------------------- /graph/bellman-ford/implementation/test_main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* test_main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/06/08 13:59:57 by jko #+# #+# */ 9 | /* Updated: 2020/06/09 11:21:26 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "graph.h" 14 | #include "bf.h" 15 | #include 16 | #include 17 | 18 | void print_graph(t_graph *graph) 19 | { 20 | uint i; 21 | t_node *curr; 22 | 23 | if (!graph) 24 | return ; 25 | i = 0; 26 | while (i < graph->size) 27 | { 28 | curr = graph->list[i]; 29 | while (curr) 30 | { 31 | printf("%2u -> %2u, %d\n", i, curr->vertex, curr->cost); 32 | curr = curr->next; 33 | } 34 | i++; 35 | } 36 | printf("\n"); 37 | } 38 | 39 | void print_sp(t_graph *graph) 40 | { 41 | int result; 42 | int *path = 0; 43 | 44 | for (uint i = 0; i < graph->size; ++i) { 45 | result = find_shortest_path(graph, i, &path); 46 | 47 | if (result == SUCCESS) 48 | { 49 | printf("** %d start shortest path cost **\n", i); 50 | for (uint j = 0; j < graph->size; ++j) { 51 | printf("vertex %d : cost %d\n", j, path[j]); 52 | } 53 | } 54 | else if (result == FAILURE) 55 | printf("shortest path error\n"); 56 | else 57 | printf("negative cycle, %d\n", result); 58 | 59 | free(path); 60 | path = 0; 61 | } 62 | } 63 | 64 | int main(void) 65 | { 66 | t_graph *graph = graph_init(6); 67 | 68 | graph_set_data(graph, 0, strdup("a")); 69 | graph_set_data(graph, 1, strdup("b")); 70 | graph_set_data(graph, 2, strdup("c")); 71 | graph_set_data(graph, 3, strdup("d")); 72 | graph_set_data(graph, 4, strdup("e")); 73 | graph_set_data(graph, 5, strdup("f")); 74 | 75 | graph_set_edge(graph, 0, 1, true, 1); 76 | graph_set_edge(graph, 0, 2, true, 5); 77 | graph_set_edge(graph, 0, 3, true, 8); 78 | 79 | graph_set_edge(graph, 1, 2, true, 11); 80 | graph_set_edge(graph, 1, 4, true, 6); 81 | graph_set_edge(graph, 1, 5, true, 4); 82 | 83 | graph_set_edge(graph, 2, 3, true, 9); 84 | graph_set_edge(graph, 2, 5, true, 10); 85 | 86 | graph_set_edge(graph, 3, 4, true, 3); 87 | graph_set_edge(graph, 3, 5, true, 0); 88 | 89 | graph_set_edge(graph, 4, 5, true, 7); 90 | 91 | graph_set_edge(graph, 5, 0, true, 3); 92 | 93 | 94 | print_graph(graph); 95 | printf("\n"); 96 | 97 | print_sp(graph); 98 | printf("\n"); 99 | 100 | 101 | 102 | graph_set_edge(graph, 3, 0, true, -1); 103 | 104 | print_graph(graph); 105 | printf("\n"); 106 | 107 | print_sp(graph); 108 | printf("\n"); 109 | 110 | 111 | 112 | graph_set_edge(graph, 2, 0, true, -9); 113 | 114 | print_graph(graph); 115 | printf("\n"); 116 | 117 | print_sp(graph); 118 | printf("\n"); 119 | 120 | 121 | 122 | 123 | free_graph(graph, free); 124 | graph = 0; 125 | 126 | system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); 127 | 128 | return (0); 129 | } 130 | -------------------------------------------------------------------------------- /graph/dijkstra/implementation/dijkstra.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* dijkstra.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jko +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/06/05 18:24:45 by jko #+# #+# */ 9 | /* Updated: 2020/06/09 10:57:54 by jko ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "dijkstra.h" 14 | 15 | static int cmp(void *path1, void *path2) 16 | { 17 | int result; 18 | 19 | if ((result = ((t_path *)path1)->cost - ((t_path *)path2)->cost)) 20 | return (result); 21 | return (((t_path *)path1)->vertex - ((t_path *)path2)->vertex); 22 | } 23 | 24 | static uint *init_result(uint size, uint start) 25 | { 26 | uint *result; 27 | uint i; 28 | 29 | if (!(result = malloc(sizeof(uint) * size))) 30 | return (0); 31 | i = 0; 32 | while (i < size) 33 | { 34 | result[i] = INT_MAX; 35 | i++; 36 | } 37 | result[start] = 0; 38 | return (result); 39 | } 40 | 41 | static t_path *create_path(uint vertex, uint cost) 42 | { 43 | t_path *path; 44 | 45 | if (!(path = malloc(sizeof(t_path)))) 46 | return (0); 47 | path->vertex = vertex; 48 | path->cost = cost; 49 | return path; 50 | } 51 | 52 | static bool push_path(t_heap *heap, uint vertex, uint cost) 53 | { 54 | t_path *path; 55 | 56 | if (!(path = create_path(vertex, cost))) 57 | return (false); 58 | if (heap_push(heap, path)) 59 | return (true); 60 | free(path); 61 | return (false); 62 | } 63 | 64 | static bool dijkstra(const t_graph *src, t_heap *heap, uint *result) 65 | { 66 | t_path *path; 67 | t_node *node; 68 | uint temp; 69 | 70 | while ((path = heap_pop(heap))) 71 | { 72 | if (result[path->vertex] < path->cost) 73 | { 74 | free(path); 75 | continue; 76 | } 77 | result[path->vertex] = path->cost; 78 | node = src->list[path->vertex]; 79 | while (node) 80 | { 81 | temp = node->cost + path->cost; 82 | if (temp < (uint)node->cost || temp < path->cost) 83 | temp = UINT_MAX; 84 | if (result[node->vertex] > temp) 85 | if (!push_path(heap, node->vertex, temp)) 86 | { 87 | free(path); 88 | return (false); 89 | } 90 | node = node->next; 91 | } 92 | free(path); 93 | } 94 | return (true); 95 | } 96 | 97 | static uint *shortest_path(const t_graph *src, uint start, t_heap *heap) 98 | { 99 | uint *result; 100 | uint i; 101 | 102 | if (!(result = init_result(src->size, start))) 103 | return (0); 104 | i = 0; 105 | while (i < src->size) 106 | { 107 | if (!push_path(heap, i, result[i])) 108 | { 109 | free(result); 110 | return (0); 111 | } 112 | i++; 113 | } 114 | if (dijkstra(src, heap, result)) 115 | return (result); 116 | free(result); 117 | return (0); 118 | } 119 | 120 | uint *find_shortest_path(const t_graph *src, uint start) 121 | { 122 | uint *result; 123 | t_heap *heap; 124 | uint max_edge; 125 | 126 | if (!src || start >= src->size) 127 | return (0); 128 | max_edge = src->size * (src->size - 1); 129 | if (!(heap = heap_init(max_edge, cmp))) 130 | return (0); 131 | result = shortest_path(src, start, heap); 132 | free_heap(heap, free); 133 | return (result); 134 | } 135 | --------------------------------------------------------------------------------