├── .gitignore ├── Alg ├── Alg.pro ├── Alg.pro.user ├── main.cpp ├── search.cpp ├── search.h ├── sort.cpp └── sort.h ├── Algorithm终稿18.pdf ├── AutoHotkey.ahk ├── C++ Primer 5th笔记200.pdf ├── C++ 基础知识总结_终稿_124.pdf ├── C++基础知识简单汇总_终稿106.pdf ├── C++对象模型-32.pdf ├── C++题目汇总终稿 70.pdf ├── C_primer_note ├── DataStructure终稿20.pdf ├── EffectiveC++终稿44.pdf ├── Qt ├── Qt 教程 .pdf └── Qt 教程2.docx ├── Qt样式表使用说明.pdf ├── README.md ├── STL使用.pdf ├── STL源码剖析终稿62.pdf ├── STL简单7终稿.pdf ├── STL详细终稿43.pdf ├── UNIX环境高级编程88.pdf ├── UNIX网络编程卷1--59.pdf ├── c-cpp ├── .gitignore ├── .gitkeep ├── 05_array │ ├── .gitkeep │ ├── Array_gp.c │ ├── Array_gp.h │ └── array.c ├── 06_linkedlist │ ├── .gitkeep │ ├── Dlist │ │ ├── Dlist.c │ │ └── Dlist.h │ ├── LRUBasedLinkedList.cpp │ ├── list_isPalindrome │ │ ├── LinkList.cpp │ │ └── LinkList.h │ ├── palindromeList │ │ ├── LinkedList.hpp │ │ ├── ListNode.hpp │ │ └── palindromeList.cpp │ ├── single_list.c │ └── singlelist_gc │ │ ├── singleList.c │ │ └── singleList.h ├── 07_linkedlist │ ├── .gitkeep │ ├── LinkedListAlgo.c │ ├── SingleList.cpp │ ├── linked_list.h │ ├── linked_list_algo.hpp │ └── linklist_jinshaohui.c ├── 08_stack │ ├── StackBasedOnArray │ │ ├── StackBasedOnArray.cpp │ │ └── StackBasedOnArray.h │ ├── StackBasedOnLinkedList │ │ ├── StackBasedOnLinkedList.cpp │ │ └── StackBasedOnLinkedList.h │ ├── arrayStack │ │ ├── arrayStack.c │ │ └── arrayStack.h │ ├── linkList │ │ ├── linklist_stack.c │ │ └── linklist_stack.h │ ├── linked_list.h │ └── stack_based_on_linked_list.hpp ├── 09_queue │ ├── .gitkeep │ ├── array_queue.hpp │ ├── array_queue │ │ ├── array_queue.c │ │ └── array_queue.h │ ├── array_queue_test.cc │ ├── block_queue.hpp │ ├── circular_queue.hpp │ ├── circular_queue_test.cc │ ├── concurrency_queue.hpp │ ├── dynamic_array_queue.hpp │ ├── dynamic_array_queue_test.cc │ ├── linked_queue.hpp │ ├── linked_queue_test.cc │ ├── list_queue │ │ ├── list_queue.c │ │ └── list_queue.h │ ├── lock_free_queue.hpp │ └── ring_queue.c ├── 10_recursive │ ├── .gitkeep │ ├── one_two_step.c │ └── one_two_step.cc ├── 11_sorts │ ├── .gitkeep │ ├── sorts.c │ ├── sorts.cpp │ ├── sorts.hpp │ ├── sorts_jinshaohui.c │ └── sorts_test.cc ├── 12_sorts │ ├── .gitkeep │ ├── merge_sort.c │ ├── merge_sort.hpp │ ├── merge_sort_test.cc │ ├── my12_sorts │ │ ├── merge_sort.c │ │ └── quick_sort.c │ ├── quick_sort.c │ ├── quick_sort.hpp │ └── quick_sort_test.cc ├── 13_sorts │ ├── .gitkeep │ ├── bucket_sort.hpp │ ├── bucket_sort_test.cc │ ├── counting_sort.hpp │ ├── counting_sort_test.cc │ └── sort.c ├── 14_sorts │ ├── .gitkeep │ ├── analytics_of_std_sort.md │ └── counting_sort.c ├── 15_bsearch │ ├── .gitkeep │ ├── binary_search.c │ ├── bsearch.hpp │ ├── bsearch_c │ │ ├── bsearch.c │ │ └── sqrt.c │ └── bsearch_test.cc ├── 16_bsearch │ ├── .gitkeep │ ├── bsearch.c │ ├── bsearch_variant.c │ ├── bsearch_varients.hpp │ └── bsearch_varients_test.cc ├── 17_skiplist │ ├── .gitkeep │ ├── SkipList.cpp │ ├── skiplist.c │ ├── skiplist.hpp │ ├── skiplist_c │ │ ├── skiplist.c │ │ └── skiplist.h │ ├── skiplist_test.cc │ ├── skiplist_tr.hpp │ └── skiplist_tr_test.cc ├── 18_hashtable │ ├── .gitkeep │ ├── hash_map.cc │ ├── hashtable.c │ └── listhash │ │ ├── listhash.c │ │ └── listhash.h ├── 19_Dlisthash │ ├── Dlist.h │ ├── LinkedHashMap.c │ └── LinkedHashMap.h ├── 23_binarytree │ ├── binarytree.c │ └── tree │ │ ├── binarytree.c │ │ ├── list_queue.c │ │ └── list_queue.h ├── 24_binarysearchtree │ ├── binary_search_tree.cpp │ ├── binarysearchtree.c │ ├── binarysearchtree.h │ └── bst.c ├── 28_heap │ └── heap.c ├── 30_Graph │ └── graph.c └── bst.c ├── docx ├── tagged │ ├── Algorithm终稿18.pdf │ ├── C++ 基础知识总结_终稿_124.docx │ ├── C++ 基础知识总结_终稿_124.pdf │ ├── C++基础知识简单汇总_终稿106__tagged.docx │ ├── C++基础知识简单汇总_终稿106__tagged.pdf │ ├── C++对象模型-32.docx │ ├── C++对象模型-32.pdf │ ├── C++题目汇总终稿 70.docx │ ├── C++题目汇总终稿 70.pdf │ ├── C语言,嵌入式_tagged_2019-0623.docx │ ├── C语言,嵌入式_tagged_2019-0623.pdf │ ├── DataStructure终稿20.pdf │ ├── EffectiveC++终稿44__tagged_2019-0624.docx │ ├── EffectiveC++终稿44__tagged_2019-0624.pdf │ ├── 牛客网C++面试真题—已注释.pdf │ ├── 预处理器.docx │ └── 预处理器.pdf └── untagged │ ├── Algorithm终稿18.docx │ ├── C++ Primer 5th笔记200.docx │ ├── DataStructure终稿20.docx │ ├── IBM_链表.docx │ ├── STL使用.docx │ ├── STL源码剖析终稿62.docx │ ├── STL简单7终稿.docx │ ├── STL详细终稿43.docx │ ├── UNIX环境高级编程88.docx │ ├── UNIX网络编程卷1--59.docx │ ├── 嵌入式笔试题.docx │ ├── 指针.docx │ ├── 操作系统笔记32.docx │ ├── 算法题总结 274.docx │ ├── 计算机网络--23.docx │ └── 高级指针话题.docx ├── multiThread_notes ├── c++多线程_12_30.pptx └── 附件 │ ├── C++11多线程相关.md │ ├── myThread.md │ ├── pdf │ └── multithreaded_server.pdf │ ├── ppt.txt │ ├── test_demo │ ├── Data Race.cpp │ ├── _class │ │ ├── class_recursive_mutex.hpp │ │ ├── hierarchical_mutex.cpp │ │ ├── joining_thread.hpp │ │ └── open_source │ │ │ └── ThreadPool-master │ │ │ ├── COPYING │ │ │ ├── README.md │ │ │ ├── ThreadPool.h │ │ │ └── ThreadPool_example.cpp │ ├── apply_level │ │ ├── test_async.cpp │ │ ├── test_chrono.cpp │ │ ├── test_function.cpp │ │ ├── test_packaged_task.cpp │ │ ├── test_promise.cpp │ │ └── test_shared_future.cpp │ ├── deadLock │ │ ├── dead_lock_demo.cpp │ │ └── debug_dead_lock.txt │ ├── for_test_code │ │ ├── double cached_computation(double x).cpp │ │ ├── get_id.cpp │ │ ├── mutex_lock_unlock.cpp │ │ ├── switch.cpp │ │ └── test_condition_variable_2.cpp │ ├── onlinegdb.txt │ ├── scoped_thread.cpp │ ├── shared_ptr_A_B.cpp │ ├── spinlock_mutex.cpp │ ├── test_gdb_step.txt │ ├── test_jthread_cpp_20.cpp │ ├── test_mutex.cpp │ ├── test_recursive_mutex.cpp │ ├── test_shared_mutex.cpp │ ├── test_thread.cpp │ ├── thread_guard.cpp │ └── thread_join_detach_bypass_and_ref.cpp │ ├── 参考资料.txt │ ├── 多线程的一些问题.html │ ├── 多线程的一些问题.md │ ├── 操作系统.md │ ├── 智能指针与内存管理.assets │ ├── 28051715-e28b1c2264504cb1a275b916a641ecbb.png │ ├── 28051719-683eba58c3744417b9f5829aaedf872b.png │ ├── 28051720-e25d8dd42ec440aa9f1675c52ef1f898.png │ ├── 28051721-38f027311ff449709a42aa2d6caeb24d.png │ ├── 28051721-66b4ed146abc44a1b3c3155c0326ded8.png │ ├── 28051722-fd09bc0bd2984d7c84e2068268d4a9e5.png │ ├── pointers1-1640249520497.png │ ├── pointers1.png │ ├── pointers2-1640249520562.png │ ├── pointers2.png │ ├── shared_ptr.png │ └── unique_ptr.png │ ├── 智能指针与内存管理.html │ ├── 智能指针与内存管理.md │ └── 术语.txt ├── printf ├── C++圣经汇总——378.pdf ├── 数据结构计算机网络操作系统--273.pdf ├── 算法合集解析_275.pdf └── 编程基础知识——301.pdf ├── 十大经典排序算法(动图演示) - 一像素 - 博客园.html ├── 操作系统笔记32.pdf ├── 牛客校招面试题(附答案与解析)全集 ├── pdf在线免费解密.url ├── 牛客校招面试题测试(附答案与解析).pdf ├── 牛客校招面试题测试(附答案与解析)java篇.pdf ├── 牛客校招面试题(附答案与解析)c++篇.pdf ├── 牛客校招面试题(附答案与解析)前端.pdf └── 牛客网C++面试真题——部分注释.pdf ├── 算法题总结 274.pdf ├── 计算机网络--23.pdf └── 设计模式.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | 19 | # Compiled Static libraries 20 | *.lai 21 | *.la 22 | *.a 23 | *.lib 24 | 25 | # Executables 26 | *.exe 27 | *.out 28 | *.app 29 | -------------------------------------------------------------------------------- /Alg/Alg.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | CONFIG += console c++11 3 | CONFIG -= app_bundle 4 | CONFIG -= qt 5 | 6 | SOURCES += main.cpp \ 7 | sort.cpp \ 8 | search.cpp 9 | 10 | HEADERS += \ 11 | sort.h \ 12 | search.h 13 | -------------------------------------------------------------------------------- /Alg/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "sort.h" 5 | #include "search.h" 6 | 7 | 8 | using namespace std; 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | int result =1; 13 | for(int i=0;i<1;i++){ 14 | Sort sort(100,0,100); 15 | Search sh; 16 | 17 | sort.BubbleSort(sort.arr); 18 | sort.printf(sort.arr); 19 | // cout<< sh.binarySearch(sort.arr,0,100,49)<%clipboard% 6 | Send ^v 7 | return 8 | -------------------------------------------------------------------------------- /C++ Primer 5th笔记200.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/C++ Primer 5th笔记200.pdf -------------------------------------------------------------------------------- /C++ 基础知识总结_终稿_124.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/C++ 基础知识总结_终稿_124.pdf -------------------------------------------------------------------------------- /C++基础知识简单汇总_终稿106.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/C++基础知识简单汇总_终稿106.pdf -------------------------------------------------------------------------------- /C++对象模型-32.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/C++对象模型-32.pdf -------------------------------------------------------------------------------- /C++题目汇总终稿 70.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/C++题目汇总终稿 70.pdf -------------------------------------------------------------------------------- /DataStructure终稿20.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/DataStructure终稿20.pdf -------------------------------------------------------------------------------- /EffectiveC++终稿44.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/EffectiveC++终稿44.pdf -------------------------------------------------------------------------------- /Qt/Qt 教程 .pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/Qt/Qt 教程 .pdf -------------------------------------------------------------------------------- /Qt/Qt 教程2.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/Qt/Qt 教程2.docx -------------------------------------------------------------------------------- /Qt样式表使用说明.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/Qt样式表使用说明.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Coding_Interviews_CN_Notes 2 | 3 | **大体涵盖以下知识** 4 | 5 | - C++ Primer 5th 6 | - effective C++ 7 | - STL (api和demos) 8 | - C++ 基础知识与理论 9 | - 智能指针、C++11、 10 | - Linux命令 11 | - Unix操作系统(进程、线程、内存管理、信号) 12 | - 计算机网络 13 | - 数据结构(排序、查找)、数据库、C++对象模型、 14 | - 算法(《剑指offer》、leetcode、lintcode、hihocoder、《王道程序员求职宝典》)、面试题等 15 | - Git教程 16 | 17 | *ps 因女友毕业,需要换个工作* 18 | 19 | *所以把相关的知识整理下,在前辈们整理的基础上,对部分自认为较重要的知识进行了标红、加粗处理。* 20 | 21 | 附上部分前辈们链接: 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 离线编程了一年,很多知识不清晰了,希望能和同学们一起提升。 38 | 39 | 感谢部门领导和师傅对我的认可、感谢导师3年来的严厉要求、感谢女友3年的陪伴。 -------------------------------------------------------------------------------- /STL使用.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/STL使用.pdf -------------------------------------------------------------------------------- /STL源码剖析终稿62.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/STL源码剖析终稿62.pdf -------------------------------------------------------------------------------- /STL简单7终稿.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/STL简单7终稿.pdf -------------------------------------------------------------------------------- /STL详细终稿43.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/STL详细终稿43.pdf -------------------------------------------------------------------------------- /UNIX环境高级编程88.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/UNIX环境高级编程88.pdf -------------------------------------------------------------------------------- /UNIX网络编程卷1--59.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/UNIX网络编程卷1--59.pdf -------------------------------------------------------------------------------- /c-cpp/.gitignore: -------------------------------------------------------------------------------- 1 | # main files 2 | main.* 3 | 4 | # executives 5 | a.out 6 | 7 | # objective files 8 | *.o 9 | *.obj 10 | -------------------------------------------------------------------------------- /c-cpp/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/.gitkeep -------------------------------------------------------------------------------- /c-cpp/05_array/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/05_array/.gitkeep -------------------------------------------------------------------------------- /c-cpp/05_array/Array_gp.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/05_array/Array_gp.h -------------------------------------------------------------------------------- /c-cpp/05_array/array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct array { 6 | int size; 7 | int used; 8 | int *arr; 9 | }; 10 | 11 | void dump(struct array *array) 12 | { 13 | int idx; 14 | 15 | for (idx = 0; idx < array->used; idx++) 16 | printf("[%02d]: %08d\n", idx, array->arr[idx]); 17 | } 18 | 19 | void alloc(struct array *array) 20 | { 21 | array->arr = (int *)malloc(array->size * sizeof(int)); 22 | } 23 | 24 | int insert(struct array *array, int elem) 25 | { 26 | int idx; 27 | if (array->used >= array->size) 28 | return -1; 29 | 30 | for (idx = 0; idx < array->used; idx++) { 31 | if (array->arr[idx] > elem) 32 | break; 33 | } 34 | 35 | if (idx < array->used) 36 | memmove(&array->arr[idx+1], &array->arr[idx], 37 | (array->used - idx) * sizeof(int)); 38 | 39 | array->arr[idx] = elem; 40 | array->used++; 41 | return idx; 42 | } 43 | 44 | int delete(struct array *array, int idx) 45 | { 46 | if (idx < 0 || idx >= array->used) 47 | return -1; 48 | 49 | memmove(&array->arr[idx], &array->arr[idx+1], 50 | (array->used - idx - 1) * sizeof(int)); 51 | array->used--; 52 | return 0; 53 | } 54 | 55 | int search(struct array *array, int elem) 56 | { 57 | int idx; 58 | 59 | for (idx = 0; idx < array->used; idx++) { 60 | if (array->arr[idx] == elem) 61 | return idx; 62 | if (array->arr[idx] > elem) 63 | return -1; 64 | } 65 | 66 | return -1; 67 | } 68 | 69 | int main() 70 | { 71 | int idx; 72 | struct array ten_int = {10, 0, NULL}; 73 | 74 | alloc(&ten_int); 75 | if (!ten_int.arr) 76 | return -1; 77 | insert(&ten_int, 1); 78 | insert(&ten_int, 3); 79 | insert(&ten_int, 2); 80 | printf("=== insert 1, 3, 2\n"); 81 | dump(&ten_int); 82 | 83 | idx = search(&ten_int, 2); 84 | printf("2 is at position %d\n", idx); 85 | idx = search(&ten_int, 9); 86 | printf("9 is at position %d\n", idx); 87 | 88 | printf("=== delete [6] element \n"); 89 | delete(&ten_int, 6); 90 | dump(&ten_int); 91 | printf("=== delete [0] element \n"); 92 | delete(&ten_int, 0); 93 | dump(&ten_int); 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /c-cpp/06_linkedlist/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/06_linkedlist/.gitkeep -------------------------------------------------------------------------------- /c-cpp/06_linkedlist/Dlist/Dlist.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: Dlist.c 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-07 6 | > Desc: 7 | ************************************************************************/ 8 | #include 9 | 10 | typedef struct DlistNode //双向链表中每一个节点 11 | { 12 | struct DlistNode *prev; //节点前项指针 13 | struct DlistNode *next; //节点后项指针 14 | int data; //数据 15 | }stDlistNode; 16 | 17 | typedef struct Dlisthead //定义链表总体 18 | { 19 | int size; //链表长度 20 | stDlistNode *head; //头指针 21 | stDlistNode *tail; //尾部指针 22 | }stDlistHead; 23 | 24 | -------------------------------------------------------------------------------- /c-cpp/06_linkedlist/list_isPalindrome/LinkList.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/06_linkedlist/list_isPalindrome/LinkList.cpp -------------------------------------------------------------------------------- /c-cpp/06_linkedlist/list_isPalindrome/LinkList.h: -------------------------------------------------------------------------------- 1 | #ifndef LINKLIST_H 2 | #define LINKLIST_H 3 | 4 | #include 5 | #include 6 | typedef char ElemType; 7 | typedef struct LNode 8 | { 9 | ElemType data; 10 | struct LNode*next; 11 | }LinkList; 12 | 13 | void CreateListHead(LinkList *&L,ElemType a[],int n); 14 | void CreateListTail(LinkList *&L,ElemType a[],int n); 15 | void InitList(LinkList *&L); 16 | void DestroyList(LinkList *&L); 17 | bool ListEmpty(LinkList *L); 18 | int ListLength(LinkList *L); 19 | void ShowList(LinkList *L); 20 | bool GetListElem(LinkList *L,int i,ElemType &e); 21 | int LocateElem(LinkList*L,ElemType e); 22 | bool ListInsert(LinkList *&L,int i,ElemType e); 23 | bool ListDelete(LinkList *&L,int i,ElemType &e); 24 | #endif 25 | -------------------------------------------------------------------------------- /c-cpp/06_linkedlist/palindromeList/LinkedList.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Author: TripleZ 3 | * Date: 2018-10-10 4 | * Brief: Linked list class. 5 | */ 6 | 7 | #ifndef _LINKEDLIST_HPP_ 8 | #define _LINKEDLIST_HPP_ 9 | 10 | #include 11 | #include "ListNode.hpp" 12 | 13 | class LinkedList { 14 | public: 15 | int size; 16 | int length; 17 | ListNode *head; 18 | LinkedList(); 19 | LinkedList(int size); 20 | ~LinkedList(); 21 | ListNode* FindElem(int elemVal); 22 | bool DeleteElem(ListNode *elem); 23 | bool DeleteLastElem(); 24 | bool InsertElemAtFront(int elemVal); 25 | bool InsertElemAtBack(int elemVal); 26 | void PrintList(); 27 | }; 28 | 29 | LinkedList::LinkedList() { 30 | this -> head = new ListNode(); 31 | this -> head->next = nullptr; 32 | this -> head->val = -1; 33 | this -> size = 10; // default 34 | this -> length = 0; 35 | } 36 | 37 | LinkedList::LinkedList(int size) { 38 | this -> head = new ListNode(); 39 | this -> head->next = nullptr; 40 | this -> head->val = -1; 41 | 42 | this -> size = size; 43 | this -> length = 0; 44 | } 45 | 46 | LinkedList::~LinkedList() { 47 | ListNode *p, *q; 48 | p = this -> head; 49 | while(p -> next != nullptr) { 50 | q = p -> next; 51 | p -> next = p -> next -> next; 52 | delete q; 53 | } 54 | delete head; 55 | this -> head = nullptr; 56 | this -> length = 0; 57 | } 58 | 59 | ListNode* LinkedList::FindElem(int elemVal) { 60 | ListNode *p; 61 | for (p = this -> head; p != nullptr; p = p -> next) { 62 | if (p -> val == elemVal) { 63 | return p; 64 | } 65 | } 66 | return nullptr; 67 | } 68 | 69 | bool LinkedList::DeleteElem(ListNode *elem) { 70 | ListNode *prev, *next; 71 | for (prev = this -> head; prev -> next != elem; prev = prev -> next); 72 | next = elem -> next; 73 | prev -> next = next; 74 | delete elem; 75 | this -> length --; 76 | return true; 77 | } 78 | 79 | bool LinkedList::DeleteLastElem() { 80 | ListNode *prev, *elem; 81 | for (prev = this -> head; prev -> next -> next != nullptr; prev = prev -> next) ; 82 | elem = prev -> next; 83 | prev -> next = nullptr; 84 | delete elem; 85 | this -> length --; 86 | return true; 87 | } 88 | 89 | bool LinkedList::InsertElemAtFront(int elemVal) { 90 | ListNode *newNode = new ListNode(); 91 | newNode -> val = elemVal; 92 | newNode -> next = this -> head -> next; 93 | this -> head -> next = newNode; 94 | this -> length ++; 95 | return true; 96 | } 97 | 98 | bool LinkedList::InsertElemAtBack(int elemVal) { 99 | ListNode *newNode = new ListNode(); 100 | newNode -> val = elemVal; 101 | ListNode *end; 102 | for (end = this -> head; end -> next != nullptr; end = end -> next); 103 | end -> next = newNode; 104 | newNode -> next = nullptr; 105 | this -> length ++; 106 | return true; 107 | } 108 | 109 | void LinkedList::PrintList() { 110 | ListNode *elem; 111 | printf("List: "); 112 | for (elem = this -> head -> next; elem -> next != nullptr; elem = elem -> next) { 113 | printf("%d - ", elem -> val); 114 | } 115 | printf("%d\n", elem -> val); 116 | } 117 | 118 | #endif 119 | -------------------------------------------------------------------------------- /c-cpp/06_linkedlist/palindromeList/ListNode.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Author: TripleZ 3 | * Date: 2018-10-10 4 | * Brief: ListNode class. 5 | */ 6 | 7 | #ifndef _LISTNODE_HPP_ 8 | #define _LISTNODE_HPP_ 9 | 10 | class ListNode { 11 | public: 12 | int val; 13 | ListNode *next; 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /c-cpp/06_linkedlist/palindromeList/palindromeList.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Author: TripleZ 3 | * Date: 2018-10-10 4 | * Brief: Check a list whether is palindrome. 5 | */ 6 | 7 | #include 8 | #include "LinkedList.hpp" 9 | 10 | bool CheckPalindromeList(LinkedList *list) { 11 | // 使用快慢指针找链表中点 12 | ListNode *slow, *fast, *mid2; 13 | slow = list -> head; 14 | fast = list -> head; 15 | while (fast -> next != nullptr) { 16 | slow = slow -> next; 17 | fast = fast -> next; 18 | if (fast -> next != nullptr) { 19 | fast = fast -> next; 20 | mid2 = slow -> next; 21 | } else { 22 | mid2 = nullptr; 23 | } 24 | } 25 | 26 | // 从中点向后逆转链表(区分奇偶情况) 27 | ListNode *mid = slow; 28 | ListNode *elem, *prev, *save; 29 | if (mid2 == nullptr) { // odd 30 | elem = mid; 31 | prev = mid -> next; 32 | } else { // even 33 | elem = mid2; 34 | prev = mid2 -> next; 35 | mid2 -> next = nullptr; 36 | } 37 | save = prev -> next; 38 | mid -> next = nullptr; 39 | while (save != nullptr) { 40 | prev -> next = elem; 41 | elem = prev; 42 | prev = save; 43 | save = save -> next; 44 | } 45 | prev -> next = elem; 46 | 47 | ListNode *end = prev; 48 | ListNode *front = list -> head -> next; 49 | 50 | // 从头尾同时遍历比较,检测链表是否为回文 51 | bool palindrome = true; 52 | while (front != end) { 53 | // printf("%d, %d\n", front -> val, end -> val); 54 | if (front -> val != end -> val) { 55 | palindrome = false; 56 | break; 57 | } 58 | front = front -> next; 59 | end = end -> next; 60 | } 61 | 62 | palindrome ? printf("The list is palindrome~\n") : printf("The list is not palindrome!\n"); 63 | 64 | return palindrome; 65 | } 66 | 67 | int main(int argc, char const *argv[]) { 68 | 69 | int init[] = {1, 2, 3, 2, 1}; 70 | LinkedList *list = new LinkedList(5); 71 | for (int i = 0; i < 5; i++) { 72 | list -> InsertElemAtBack(init[i]); 73 | } 74 | list -> PrintList(); 75 | 76 | CheckPalindromeList(list); // true 77 | 78 | list -> InsertElemAtFront(5); 79 | CheckPalindromeList(list); // false 80 | 81 | 82 | int init2[] = {1, 2, 3, 3, 2, 1}; 83 | LinkedList *list2 = new LinkedList(10); 84 | for (int i = 0; i < 6; i++) list2 -> InsertElemAtBack(init2[i]); 85 | list2 -> PrintList(); 86 | CheckPalindromeList(list2); 87 | 88 | list2 -> InsertElemAtBack(4); 89 | CheckPalindromeList(list2); 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /c-cpp/06_linkedlist/singlelist_gc/singleList.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/06_linkedlist/singlelist_gc/singleList.c -------------------------------------------------------------------------------- /c-cpp/06_linkedlist/singlelist_gc/singleList.h: -------------------------------------------------------------------------------- 1 | #ifndef __SINGLELIST_H__ 2 | #define __SINGLELIST_H__ 3 | 4 | #include 5 | #include 6 | 7 | typedef struct listNode 8 | { 9 | struct listNode *next; 10 | void *value; 11 | }listNode; 12 | 13 | typedef struct linkedList 14 | { 15 | listNode *head; 16 | size_t len; 17 | size_t typesize; 18 | 19 | void(*dup)(void*, void*); 20 | int(*match)(void*, void*); 21 | void(*free)(void*); 22 | }linkedList; 23 | 24 | #define listSetDupMethod(l,m) ((l)->dup = (m)) 25 | #define listSetFreeMethod(l,m) ((l)->free = (m)) 26 | #define listSetMatchMethod(l,m) ((l)->match = (m)) 27 | 28 | #define listGetDupMethod(l) ((l)->dup) 29 | #define listGetFree(l) ((l)->free) 30 | #define listGetMatchMethod(l) ((l)->match) 31 | 32 | linkedList *listCreate(); 33 | void listRelease(linkedList *list); 34 | void listEmpty(linkedList *list); 35 | linkedList *listAddNodeHead(linkedList *list, void *value); 36 | linkedList *listAddNodeTail(linkedList *list, void *value); 37 | linkedList *listInsertNode(linkedList *list, listNode *old_node, void *value, bool after); 38 | void listDelNode(linkedList *list, listNode *node); 39 | 40 | listNode *listSearchKey(linkedList *list, void *key); 41 | listNode *listIndex(linkedList *list, long index); 42 | linkedList* listRewind(linkedList *list); 43 | 44 | size_t listLength(linkedList *list); 45 | 46 | #endif // !__SINGLELIST_H__ 47 | -------------------------------------------------------------------------------- /c-cpp/07_linkedlist/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/07_linkedlist/.gitkeep -------------------------------------------------------------------------------- /c-cpp/07_linkedlist/SingleList.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/07_linkedlist/SingleList.cpp -------------------------------------------------------------------------------- /c-cpp/07_linkedlist/linked_list.h: -------------------------------------------------------------------------------- 1 | /** 2 | * 单链表 3 | * 4 | * Author: Liam Huang (Liam0205) 5 | */ 6 | 7 | #ifndef LINKEDLIST_LINKED_LIST_H_ 8 | #define LINKEDLIST_LINKED_LIST_H_ 9 | 10 | #include 11 | 12 | template 13 | struct Node { 14 | using ptr_t = std::shared_ptr>; 15 | T data; 16 | ptr_t next; 17 | 18 | Node(T data_) : data(data_), next(nullptr) {} 19 | Node() : next(nullptr) {} 20 | }; 21 | 22 | #endif // LINKEDLIST_LINKED_LIST_H_ 23 | -------------------------------------------------------------------------------- /c-cpp/07_linkedlist/linklist_jinshaohui.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: lisklist.c 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-07 6 | > Desc: 7 | ************************************************************************/ 8 | #include 9 | 10 | 11 | struct stlistNode 12 | { 13 | int val; 14 | struct listNode *next; 15 | }listNode; 16 | 17 | /*反转链表*/ 18 | listNode reverseList(listNode *head) 19 | { 20 | listNode *prev = NULL; 21 | listNode *next = NULL; 22 | 23 | while(head != NULL) 24 | { 25 | next = head->next; 26 | head->next = prev; 27 | prev = head; 28 | head = next; 29 | } 30 | 31 | return prev; 32 | } 33 | 34 | /*判断链表是否有环*/ 35 | int hasCycle(listNode *head) 36 | { 37 | listNode * fast = head; 38 | listNode * low = head; 39 | 40 | while(fast != NULL && fast->next != NULL) 41 | { 42 | low = low->next; 43 | fast = fast->next->next; 44 | if (low == fast) 45 | { 46 | return 1; 47 | } 48 | } 49 | 50 | return 0; 51 | } 52 | /*合并有序链表*/ 53 | listNode *mergeTwoLists(listNode *l1,listNode *l2) 54 | { 55 | listNode head = {0}; 56 | listNode *pRes = &head; 57 | 58 | while(1) 59 | { 60 | if(l1 == NULL) 61 | { 62 | pRes->next = l2; 63 | } 64 | 65 | if (l2 == NULL) 66 | { 67 | pRes->next = l1; 68 | } 69 | 70 | if(l1->val < l2->val) 71 | { 72 | pRes->next = l1; 73 | l1 = l1->next; 74 | } 75 | else 76 | { 77 | pRes->next = l2; 78 | l2 = l2->next; 79 | } 80 | pRes = pRes->next; 81 | } 82 | 83 | return head; 84 | } 85 | /* 86 | *删除链表倒数第n个节点,并返回链表头节点 */ 87 | 88 | listNode * removeNthFromEnd(listNode*headi,int n) 89 | { 90 | listNode *fast = head; 91 | listNode *prev = NULL; 92 | listNpde *next = head; 93 | int k = n; 94 | 95 | /*快指针往后移动k-1*/ 96 | while((k > 1) && (fast != NULL)) 97 | { 98 | fast = fast->next; 99 | k--; 100 | } 101 | 102 | /*说明链表数目不足n个*/ 103 | if (fast == NULL) 104 | { 105 | return head; 106 | } 107 | 108 | while (fast->next != NULL) 109 | { 110 | fast = fast->next; 111 | prev = next; 112 | next = next->next; 113 | } 114 | 115 | if(prev == NULL) 116 | { 117 | head = head->next; 118 | } 119 | else 120 | { 121 | prev->next = prev->next->next; 122 | } 123 | 124 | return head; 125 | } 126 | /*求链表的中间节点*/ 127 | listNode *middleNode(listNode *head) 128 | { 129 | listNode * fast = head; 130 | listNode * low = head; 131 | 132 | while(fast != NULL && fast->next != NULL) 133 | { 134 | low = low->next; 135 | fast = fast->next->next; 136 | } 137 | 138 | return low; 139 | } 140 | -------------------------------------------------------------------------------- /c-cpp/08_stack/StackBasedOnArray/StackBasedOnArray.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * 1)顺序栈的操作:入栈和出栈; 3 | * 2)采用模板的方法实现存储任意类型的数据 4 | * 3)采用数组的栈,支持动态扩容,每次扩容1.5 倍,初始栈的大小是 10 。 5 | * 6 | * Author:caozx 7 | * time ;2018年10月11日 8 | */ 9 | 10 | #include 11 | #include "StackBasedOnArray.h" 12 | using namespace std; 13 | 14 | //构造函数,创建栈 15 | //类模板成员函数的写法 template 返回值类型 类名::成员函数名(参数列表){} 16 | template ArrayStack::ArrayStack() 17 | { 18 | this -> count = 10; 19 | this -> flag = 0; 20 | this -> array = new T[this -> count]; 21 | if (! this -> array){ 22 | cout << "array malloc memory failure" << endl; 23 | } 24 | } 25 | 26 | 27 | //有参构造函数,创建栈 28 | template ArrayStack::ArrayStack(int count) 29 | { 30 | this -> count = count; 31 | this -> flag = 0; 32 | this -> array = new T[this -> count]; 33 | if (! this -> array){ 34 | cout << "array malloc memory failure" << endl; 35 | } 36 | } 37 | 38 | //析构函数,销毁栈 39 | template ArrayStack::~ArrayStack(){ 40 | this -> count = 0; 41 | this -> flag = 0; 42 | if(this -> array){ 43 | delete [] this -> array; 44 | this -> array = NULL; 45 | } 46 | 47 | } 48 | 49 | // 入栈 50 | template void ArrayStack::push(T data){ 51 | if(this -> flag == this -> count){ 52 | cout << "The stack is full , so need to enlarge 1.5x! "<< endl; 53 | this -> count = int (1.5 * this -> count); 54 | T * temp = new T [this -> count]; 55 | for(int i = 0; i < this -> flag ; i++){ 56 | temp[i] = this -> array[i]; 57 | //cout << temp[i] < array; //释放原来的空间 60 | temp[this -> flag] = data; 61 | this -> flag ++; 62 | this -> array = temp; 63 | } 64 | else{ 65 | this -> array [this -> flag] = data; 66 | this -> flag ++ ; 67 | } 68 | } 69 | 70 | //出栈,并删除栈顶元素 71 | template T ArrayStack::pop(){ 72 | this -> flag --; 73 | T temp = this -> array[this -> flag]; 74 | return temp; 75 | } 76 | 77 | //出栈,不删除栈顶元素 78 | template T ArrayStack::peek(){ 79 | T temp = this -> array[this -> flag - 1]; 80 | return temp; 81 | } 82 | 83 | template int ArrayStack::stackSize(){ 84 | return this -> flag; 85 | } 86 | 87 | template int ArrayStack::stackMaxSize(){ 88 | return this -> count; 89 | } 90 | 91 | int main(int argc, char const *argv[]) 92 | { 93 | cout << " === test begin ===" << endl; 94 | ArrayStack arrstack(12); 95 | arrstack.push(10); 96 | arrstack.push(20); 97 | arrstack.push(30); 98 | arrstack.push(40); 99 | arrstack.push(50); 100 | arrstack.push(60); 101 | arrstack.push(70); 102 | arrstack.push(80); 103 | arrstack.push(90); 104 | arrstack.push(100); 105 | arrstack.push(110); 106 | arrstack.push(120); 107 | arrstack.push(130); 108 | arrstack.push(140); 109 | arrstack.push(150); 110 | 111 | cout << "peek , not delete " << arrstack.peek() << endl; 112 | cout << "pop , delete " << arrstack.pop()< class 类名{} 4 | template class ArrayStack 5 | { 6 | public: 7 | ArrayStack(); 8 | ArrayStack(int count); 9 | ~ArrayStack(); 10 | void push(T data); //入栈 11 | T pop(); //出栈,并删除栈顶元素 12 | T peek(); //返回栈顶元素,不删除栈顶元素,栈顶指针不变 13 | int stackSize(); 14 | int stackMaxSize(); 15 | 16 | private: 17 | int flag; //栈顶标签,指向栈顶 18 | int count ; //栈的容量 19 | T *array; //指针 20 | }; 21 | -------------------------------------------------------------------------------- /c-cpp/08_stack/StackBasedOnLinkedList/StackBasedOnLinkedList.h: -------------------------------------------------------------------------------- 1 | // 类模板的声明,关键字 class 也可以更换成 typename 2 | template class LinkedListStack 3 | { 4 | public: 5 | LinkedListStack(); 6 | ~LinkedListStack(); 7 | 8 | void push(const T & data); //入栈 9 | T peek(); //返回栈顶元素,即出栈,不删除栈顶元素 10 | T pop(); //出栈,删除栈顶元素 11 | int size() const; //返回栈的大小 12 | private: 13 | int count; //存放栈的大小,因为是单链表所以这里不规定栈的最大可承载量 14 | struct LinkedNode 15 | { 16 | T data; 17 | LinkedNode * next; 18 | }; 19 | LinkedNode * head; // 单链表的头指针,不带头节点 20 | }; 21 | -------------------------------------------------------------------------------- /c-cpp/08_stack/arrayStack/arrayStack.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: arrayStack.c 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-12 6 | > Desc: 数组实现顺序栈 7 | ************************************************************************/ 8 | #include 9 | #include 10 | #include 11 | #include"./arrayStack.h" 12 | 13 | /*创建并初始化顺序栈*/ 14 | stArrayStack * arrayStack_create(int size) 15 | { 16 | stArrayStack *parrStack = NULL; 17 | 18 | parrStack = (stArrayStack *)malloc(sizeof(stArrayStack)); 19 | if (parrStack == NULL) 20 | { 21 | return NULL; 22 | } 23 | 24 | parrStack->size = size; 25 | parrStack->pos = -1; 26 | parrStack->array = (int *)malloc(sizeof(int)*size); 27 | if(parrStack->array == NULL) 28 | { 29 | free(parrStack); 30 | return NULL; 31 | } 32 | 33 | return parrStack; 34 | } 35 | /*销毁顺序栈*/ 36 | void arrayStack_destory(stArrayStack * parrStack) 37 | { 38 | if(parrStack == NULL) 39 | { 40 | return; 41 | } 42 | 43 | if (parrStack->array != NULL) 44 | { 45 | free(parrStack->array); 46 | } 47 | 48 | free(parrStack); 49 | return; 50 | } 51 | /*出栈*/ 52 | int arrayStack_pop(stArrayStack *parrStack) 53 | { 54 | int data = 0; 55 | 56 | if(arrayStack_is_empty(parrStack)) 57 | { 58 | return -1; 59 | } 60 | data = parrStack->array[parrStack->pos]; 61 | parrStack->pos--; 62 | 63 | return data; 64 | } 65 | /*入栈*/ 66 | int arrayStack_push(stArrayStack *parrStack,int data) 67 | { 68 | if(arrayStack_is_full(parrStack)) 69 | { 70 | return -1; 71 | } 72 | 73 | parrStack->pos++; 74 | parrStack->array[parrStack->pos] = data; 75 | 76 | return 0; 77 | } 78 | 79 | int arrayStack_push_new(stArrayStack*parrStack,int data) 80 | { 81 | int *ptmp = NULL; 82 | 83 | /*如果栈不满,直接插入*/ 84 | if(!arrayStack_is_full(parrStack)) 85 | { 86 | return arrayStack_push(parrStack,data); 87 | } 88 | 89 | /*如果栈已经满,申请内存*/ 90 | ptmp = (int *)malloc(2*parrStack->size*sizeof(int)); 91 | if (ptmp == NULL) 92 | { 93 | return -1; 94 | } 95 | 96 | memcpy(ptmp,parrStack->array,parrStack->size*sizeof(int)); 97 | 98 | free(parrStack->array); 99 | 100 | parrStack->array = ptmp; 101 | parrStack->size = 2*parrStack->size; 102 | parrStack->pos++; 103 | parrStack->array[parrStack->pos] = data; 104 | 105 | return ; 106 | } 107 | 108 | void arrayStack_dump(stArrayStack *parrStack) 109 | { 110 | int i = 0; 111 | 112 | if (arrayStack_is_empty(parrStack)) 113 | { 114 | printf("\r\n arrayStack is empty."); 115 | return; 116 | } 117 | printf("\r\narrayStack size = %d,pos= %d,", 118 | parrStack->size,parrStack->pos); 119 | for(i = 0; i <= parrStack->pos; i++) 120 | { 121 | printf("\r\narry[%d] = %d",i,parrStack->array[i]); 122 | } 123 | } 124 | 125 | int main() 126 | { 127 | int i = 0; 128 | int ret = 0; 129 | stArrayStack * parrStack = NULL; 130 | 131 | printf("\r\n create size = 4 arrayStack."); 132 | 133 | parrStack = arrayStack_create(4); 134 | if (parrStack == NULL) 135 | { 136 | printf("\r\n create size = 4 arrayStack faided."); 137 | return 0; 138 | } 139 | 140 | for (i = 0; i < 5; i++) 141 | { 142 | ret = arrayStack_push(parrStack,i); 143 | if(ret != 0) 144 | { 145 | printf("\r\n push size = %d arrayStack faided.",i); 146 | 147 | } 148 | } 149 | arrayStack_dump(parrStack); 150 | 151 | ret = arrayStack_push_new(parrStack,4); 152 | if(ret != 0) 153 | { 154 | printf("\r\n push size = %d arrayStack faided.",4); 155 | } 156 | arrayStack_dump(parrStack); 157 | 158 | arrayStack_destory(parrStack); 159 | 160 | return; 161 | } 162 | -------------------------------------------------------------------------------- /c-cpp/08_stack/arrayStack/arrayStack.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: arrayStack.h 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-12 6 | > Desc: 7 | ************************************************************************/ 8 | 9 | #ifndef ARRAY_STACJ_H 10 | #define ARRAY_STACJ_H 11 | 12 | typedef struct _array_stack 13 | { 14 | int size;/*栈的大小*/ 15 | int pos;/*当前存储元素的个数,即栈顶元素下表*/ 16 | int *array;/*数据存储区*/ 17 | }stArrayStack; 18 | 19 | #define arrayStack_size(arrayStack) (arrayStack->size) 20 | #define arrayStack_is_empty(arrayStack) (arrayStack->pos == -1) 21 | #define arrayStack_is_full(arrayStack) (arrayStack->pos == (arrayStack->size-1)) 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /c-cpp/08_stack/linkList/linklist_stack.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: linklist_stack.c 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-12 6 | > Desc: 7 | ************************************************************************/ 8 | #include 9 | #include 10 | #include 11 | #include "./linklist_stack.h" 12 | 13 | linklist_stack * stack_create() 14 | { 15 | linklist_stack * stack = NULL; 16 | 17 | stack = (linklist_stack *)malloc(sizeof(linklist_stack)); 18 | if (stack == NULL) 19 | { 20 | return NULL; 21 | } 22 | 23 | stack->next = NULL; 24 | 25 | return stack; 26 | } 27 | 28 | void stack_destory(linklist_stack* stack) 29 | { 30 | linklist_stack * ptmp = NULL; 31 | 32 | while(!stack_is_empty(stack)) 33 | { 34 | ptmp = stack->next; 35 | stack->next = stack->next->next; 36 | 37 | free(ptmp); 38 | } 39 | 40 | free(stack); 41 | 42 | return; 43 | } 44 | 45 | int stack_push(linklist_stack *stack,int data) 46 | { 47 | linklist_stack * ptmp = NULL; 48 | 49 | ptmp = (linklist_stack *)malloc(sizeof(linklist_stack)); 50 | if (ptmp == NULL) 51 | { 52 | return -1; 53 | } 54 | 55 | ptmp->data = data; 56 | ptmp->next = stack->next; 57 | stack->next = ptmp; 58 | 59 | return 0; 60 | } 61 | 62 | int stack_pop(linklist_stack*stack,int *data) 63 | { 64 | linklist_stack *ptmp = NULL; 65 | if (data == NULL) 66 | { 67 | return -1; 68 | } 69 | if(stack_is_empty(stack)) 70 | { 71 | return -1; 72 | } 73 | *data = stack->next->data; 74 | ptmp = stack->next; 75 | stack->next = ptmp->next; 76 | free(ptmp); 77 | 78 | return 0; 79 | } 80 | 81 | 82 | void stack_dump(linklist_stack *stack) 83 | { 84 | linklist_stack * ptmp = stack->next; 85 | 86 | while(ptmp != NULL) 87 | { 88 | printf("\r\n data = %d",ptmp->data); 89 | ptmp = ptmp->next; 90 | } 91 | return; 92 | } 93 | 94 | int main() 95 | { 96 | int i = 0; 97 | int ret = 0; 98 | int data = 0; 99 | linklist_stack * stack = NULL; 100 | 101 | stack = stack_create(); 102 | if (stack == NULL) 103 | { 104 | printf("\r\n stack create falied."); 105 | return 0; 106 | } 107 | 108 | for (i = 0; i < 4; i++) 109 | { 110 | ret = stack_push(stack,i); 111 | if(ret != 0) 112 | { 113 | printf("\r\n stack push %d falied.",i); 114 | } 115 | } 116 | 117 | stack_dump(stack); 118 | 119 | for (i = 0; i < 5; i++) 120 | { 121 | ret = stack_pop(stack,&data); 122 | if(ret != 0) 123 | { 124 | printf("\r\n stack pop%d falied.", i); 125 | } 126 | else 127 | { 128 | printf("\r\n data = %d,",data); 129 | } 130 | } 131 | 132 | stack_destory(stack); 133 | 134 | return 0; 135 | 136 | } 137 | -------------------------------------------------------------------------------- /c-cpp/08_stack/linkList/linklist_stack.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: linklist_stack.h 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-12 6 | > Desc: 7 | ************************************************************************/ 8 | 9 | #ifndef STACK_LINK_LIST_H 10 | #define STACK_LINK_LIST_H 11 | 12 | typedef struct _linkliststack 13 | { 14 | int data; 15 | struct _linkliststack *next; 16 | }linklist_stack; 17 | 18 | 19 | #define stack_is_empty(liststack) (liststack->next == NULL) 20 | 21 | #endif 22 | 23 | -------------------------------------------------------------------------------- /c-cpp/08_stack/linked_list.h: -------------------------------------------------------------------------------- 1 | /** 2 | * C++ 版本单链表结点 3 | * 4 | * Author: Liam Huang (Liam0205) 5 | */ 6 | 7 | #ifndef STACK_LINKED_LIST_H_ 8 | #define STACK_LINKED_LIST_H_ 9 | 10 | #include 11 | 12 | template 13 | struct Node { 14 | using ptr_t = std::shared_ptr>; 15 | T data; 16 | ptr_t next; 17 | 18 | Node(T data_) : data(data_), next(nullptr) {} 19 | Node() : next(nullptr) {} 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /c-cpp/08_stack/stack_based_on_linked_list.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * 基于链表实现的栈。 3 | * 4 | * Author: Liam Huang (Liam0205) 5 | */ 6 | 7 | #ifndef STACK_STACK_BASED_ON_LINKED_LIST_HPP_ 8 | #define STACK_STACK_BASED_ON_LINKED_LIST_HPP_ 9 | 10 | #include 11 | #include "linked_list.h" 12 | 13 | template 14 | class Stack { 15 | public: 16 | using value_type = T; 17 | using node_type = typename Node::ptr_t; 18 | 19 | private: 20 | node_type top_ = nullptr; 21 | 22 | public: 23 | bool empty(void) const { 24 | return nullptr == top_; 25 | } 26 | void push(const value_type& value) { 27 | auto node = std::make_shared(value); 28 | if (this->empty()) { 29 | top_ = node; 30 | } else { 31 | node->next = top_; 32 | top_ = node; 33 | } 34 | } 35 | value_type top(void) const { 36 | if (not this->empty()) { 37 | return top_->data; 38 | } else { 39 | throw "Fetch data from empty stack!"; 40 | } 41 | } 42 | void pop(void) { 43 | if (not this->empty()) { 44 | top_ = top_->next; 45 | return; 46 | } else { 47 | throw "Pop from empty stack!"; 48 | } 49 | } 50 | }; 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /c-cpp/09_queue/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/09_queue/.gitkeep -------------------------------------------------------------------------------- /c-cpp/09_queue/array_queue.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/10. 3 | */ 4 | 5 | #ifndef QUEUE_ARRAY_QUEUE_HPP_ 6 | #define QUEUE_ARRAY_QUEUE_HPP_ 7 | 8 | template 9 | class ArrayQueue { 10 | private: 11 | T* items_ = nullptr; 12 | size_t capacity_ = 0; 13 | size_t head_ = 0; 14 | size_t tail_ = 0; 15 | 16 | public: 17 | ArrayQueue() = delete; 18 | ArrayQueue(const size_t capacity) : capacity_(capacity) { 19 | items_ = new T[capacity_]; 20 | } 21 | ~ArrayQueue() { 22 | if (nullptr != items_) { 23 | delete[] items_; 24 | items_ = nullptr; 25 | } 26 | } 27 | ArrayQueue(const ArrayQueue& other) : capacity_(other.capacity_) { 28 | items_ = new T[capacity_]; 29 | for (size_t i = other.head_; i != other.tail_; ++i) { 30 | enqueue(other.items_[i]); 31 | } 32 | } 33 | ArrayQueue& operator=(const ArrayQueue& rhs) { 34 | delete[] items_; 35 | head_ = 0; 36 | tail_ = 0; 37 | capacity_ = rhs.capacity_; 38 | items_ = new T[capacity_]; 39 | for (size_t i = rhs.head_; i != rhs.tail_; ++i) { 40 | enqueue(rhs.items_[i]); 41 | } 42 | return *this; 43 | } 44 | ArrayQueue(ArrayQueue&& other) : items_(other.items_), 45 | capacity_(other.capacity_), 46 | head_(other.head_), 47 | tail_(other.tail_) { 48 | other.items_ = nullptr; 49 | other.capacity_ = 0; 50 | other.head_ = 0; 51 | other.tail_ = 0; 52 | } 53 | ArrayQueue& operator=(ArrayQueue&& rhs) { 54 | delete[] items_; 55 | items_ = rhs.items_; 56 | capacity_ = rhs.capacity_; 57 | head_ = rhs.head_; 58 | tail_ = rhs.tail_; 59 | rhs.items_ = nullptr; 60 | rhs.capacity_ = 0; 61 | rhs.head_ = 0; 62 | rhs.tail_ = 0; 63 | return *this; 64 | } 65 | 66 | public: 67 | void enqueue(T item) { 68 | if (capacity_ == tail_) { 69 | throw "Push data into a full queue!"; 70 | } 71 | items_[tail_++] = item; 72 | } 73 | T head() const { 74 | if (head_ != tail_) { 75 | return items_[head_]; 76 | } else { 77 | throw "Fetch data from an empty queue!"; 78 | } 79 | } 80 | void dequeue() { 81 | if (head_ != tail_) { 82 | ++head_; 83 | } else { 84 | throw "Pop data from an empty queue!"; 85 | } 86 | } 87 | 88 | public: 89 | template 90 | void traverse(UnaryFunc do_traverse) { 91 | for (size_t i = head_; i != tail_; ++i) { 92 | do_traverse(items_[i]); 93 | } 94 | } 95 | }; 96 | 97 | #endif // QUEUE_ARRAY_QUEUE_HPP_ 98 | -------------------------------------------------------------------------------- /c-cpp/09_queue/array_queue/array_queue.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: array_queue.c 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-12 6 | > Desc: 7 | ************************************************************************/ 8 | #include 9 | #include 10 | #include 11 | #include"./array_queue.h" 12 | 13 | 14 | array_queue * array_queue_create(int size) 15 | { 16 | array_queue * queue = NULL; 17 | 18 | queue = (array_queue*)malloc(sizeof(array_queue)); 19 | if (queue == NULL) 20 | { 21 | return NULL; 22 | } 23 | queue->array = (int *)malloc(sizeof(int)*size); 24 | if (queue->array == NULL) 25 | { 26 | free(queue); 27 | return NULL; 28 | } 29 | queue->size = size; 30 | queue->num = 0; 31 | queue->head = 0; 32 | queue->tail = 0; 33 | 34 | return queue; 35 | } 36 | 37 | void array_queue_destory(array_queue *queue) 38 | { 39 | if (queue == NULL) 40 | { 41 | return; 42 | } 43 | 44 | if (queue->array != NULL) 45 | { 46 | free(queue->array); 47 | } 48 | 49 | free(queue); 50 | return; 51 | } 52 | 53 | /*入队列 */ 54 | int array_queue_enqueue(array_queue *queue,int data) 55 | { 56 | /*队列为空,或者队列满时,返回-1*/ 57 | if ((queue == NULL) || (array_queue_is_full(queue))) 58 | { 59 | return -1; 60 | } 61 | 62 | queue->num++; 63 | queue->array[queue->tail] = data; 64 | queue->tail = (queue->tail + 1) % queue->size; 65 | 66 | return 0; 67 | } 68 | 69 | /*出队列*/ 70 | int array_queue_dequeue(array_queue * queue,int *data) 71 | { 72 | /*队列为空,数据存储为空,队列为空时返回-1*/ 73 | if ((queue == NULL) || (data == NULL) || (array_queue_is_empty(queue))) 74 | { 75 | return -1; 76 | } 77 | *data = queue->array[queue->head]; 78 | queue->num--; 79 | queue->head = (queue->head + 1) % queue->size; 80 | 81 | return 0; 82 | } 83 | 84 | void array_queue_dump(array_queue *queue) 85 | { 86 | int i = 0; 87 | int pos = 0; 88 | 89 | if ((queue == NULL) || (array_queue_is_empty(queue))) 90 | { 91 | printf("\r\n queue is empty"); 92 | return; 93 | } 94 | 95 | printf("\r\n size:%d,num:%d,head:%d,tali:%d", 96 | queue->size,queue->num,queue->head,queue->tail); 97 | for (i = 0; i < queue->num; i ++) 98 | { 99 | pos = (queue->head + i) %queue->size; 100 | printf("\r\n array[%d] = %d",pos,queue->array[pos]); 101 | } 102 | return; 103 | } 104 | 105 | int main() 106 | { 107 | int i = 0; 108 | int ret = 0; 109 | int data = 0; 110 | array_queue * queue = NULL; 111 | 112 | queue = array_queue_create(4); 113 | if (queue == NULL) 114 | { 115 | printf("\r\n queue is create failed."); 116 | return 0; 117 | } 118 | /*队列时空时,出队返回错误*/ 119 | ret = array_queue_dequeue(queue, &data); 120 | if (ret != 0) 121 | { 122 | printf("\r\n queue %d dequeue failed.",ret); 123 | } 124 | 125 | /*队列大小是4,入队5个,最后一个报错*/ 126 | for (i = 0; i < 5; i++) 127 | { 128 | ret = array_queue_enqueue(queue,i); 129 | if (ret != 0) 130 | { 131 | printf("\r\n queue %d enqueue failed.",i); 132 | 133 | } 134 | } 135 | 136 | array_queue_dump(queue); 137 | 138 | ret = array_queue_dequeue(queue, &data); 139 | if (ret != 0) 140 | { 141 | printf("\r\n queue %d dequeue failed.",i); 142 | } 143 | printf("\r\n queue %d dequue.",data); 144 | array_queue_dump(queue); 145 | data = 5; 146 | printf("\r\n queue %d enqueue.",data); 147 | ret = array_queue_enqueue(queue,data); 148 | if (ret != 0) 149 | { 150 | printf("\r\n queue %d enqueue failed.",data); 151 | } 152 | array_queue_dump(queue); 153 | 154 | array_queue_destory(queue); 155 | return 0; 156 | 157 | } 158 | 159 | -------------------------------------------------------------------------------- /c-cpp/09_queue/array_queue/array_queue.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: array_queue.h 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-12 6 | > Desc: 7 | ************************************************************************/ 8 | #ifndef ARRAY_QUEUE_H 9 | #define ARRAY_QUEUE_H 10 | 11 | typedef struct _array_queue 12 | { 13 | int size;/*队列的大小*/ 14 | int num; /*当前存储数据的大小*/ 15 | int head;/*队列的头*/ 16 | int tail;/*队列的尾*/ 17 | int *array;/*数据存储区*/ 18 | }array_queue; 19 | 20 | #define array_queue_is_empty(array_queue) (array_queue->num == 0) 21 | #define array_queue_is_full(array_queue) ((array_queue->num) == (array_queue->size)) 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /c-cpp/09_queue/array_queue_test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "array_queue.hpp" 3 | 4 | int main() { 5 | auto do_traverse = [&](auto item){ std::cout << item << ' '; }; 6 | 7 | ArrayQueue array_queue_1(3); 8 | array_queue_1.enqueue(1); 9 | array_queue_1.enqueue(2); 10 | array_queue_1.enqueue(3); 11 | // array_queue_1.enqueue(4); // throw 12 | array_queue_1.traverse(do_traverse); 13 | std::cout << std::endl; 14 | 15 | ArrayQueue array_queue_2(array_queue_1); // copy constructor 16 | array_queue_2.traverse(do_traverse); 17 | std::cout << std::endl; 18 | 19 | ArrayQueue array_queue_3(std::move(array_queue_2)); // move constructor 20 | array_queue_3.traverse(do_traverse); 21 | std::cout << std::endl; 22 | array_queue_2.traverse(do_traverse); 23 | std::cout << std::endl; 24 | 25 | std::cout << array_queue_3.head() << std::endl; 26 | array_queue_3.dequeue(); 27 | std::cout << array_queue_3.head() << std::endl; 28 | array_queue_3.dequeue(); 29 | std::cout << array_queue_3.head() << std::endl; 30 | array_queue_3.dequeue(); 31 | // std::cout << array_queue_3.head() << std::endl; // throw 32 | // array_queue_3.dequeue(); // throw 33 | 34 | ArrayQueue array_queue_4(1); 35 | array_queue_4 = array_queue_1; // copy assignment 36 | array_queue_4.traverse(do_traverse); 37 | std::cout << std::endl; 38 | 39 | ArrayQueue array_queue_5(100); 40 | array_queue_5 = std::move(array_queue_4); // move assignment 41 | array_queue_5.traverse(do_traverse); 42 | std::cout << std::endl; 43 | array_queue_4.traverse(do_traverse); 44 | std::cout << std::endl; 45 | 46 | std::cout << array_queue_5.head() << std::endl; 47 | array_queue_5.dequeue(); 48 | std::cout << array_queue_5.head() << std::endl; 49 | array_queue_5.dequeue(); 50 | std::cout << array_queue_5.head() << std::endl; 51 | array_queue_5.dequeue(); 52 | // std::cout << array_queue_5.head() << std::endl; // throw 53 | // array_queue_5.dequeue(); // throw 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /c-cpp/09_queue/block_queue.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/11. 3 | */ 4 | 5 | #ifndef QUEUE_BLOCK_QUEUE_HPP_ 6 | #define QUEUE_BLOCK_QUEUE_HPP_ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | template 13 | class BlockQueue { 14 | public: 15 | using value_type = T; 16 | using container_type = std::queue; 17 | using size_type = typename container_type::size_type; 18 | 19 | private: 20 | size_type capacity_ = 0; 21 | container_type container_; 22 | mutable std::mutex mutex_; 23 | mutable std::condition_variable not_empty_; 24 | mutable std::condition_variable not_full_; 25 | 26 | public: 27 | BlockQueue() = delete; 28 | BlockQueue(const size_type capacity) : capacity_(capacity) {} 29 | BlockQueue(const BlockQueue&) = default; 30 | BlockQueue(BlockQueue&&) = default; 31 | BlockQueue& operator=(const BlockQueue&) = default; 32 | BlockQueue& operator=(BlockQueue&&) = default; 33 | 34 | private: 35 | bool empty() const { return container_.empty(); } 36 | bool full() const { return not(container_.size() < capacity_); } 37 | 38 | public: 39 | void put(const value_type& item) { 40 | std::unqiue_lock lock(mutex_); 41 | while (full()) { 42 | not_full_.wait(lock); 43 | } 44 | container_.push(item); 45 | not_empty_.notify_one(); 46 | } 47 | void take(value_type& out) { 48 | std::unique_lock lock(mutex_); 49 | while (empty()) { 50 | not_empty_.wait(lock); 51 | } 52 | out = container_.front(); 53 | container_.pop(); 54 | not_full_.notify_one(); 55 | } 56 | template 57 | bool put_for(const value_type& item, const Duration& d) { 58 | std::unqiue_lock lock(mutex_); 59 | if (not_full_.wait_for(lock, d, [&](){ return not full(); })) { 60 | container_.push(item); 61 | not_empty_.notify_one(); 62 | return true; 63 | } else { 64 | return false; 65 | } 66 | } 67 | template 68 | bool take_for(const Duration& d, value_type& out) { 69 | std::unique_lock lock(mutex_); 70 | if (not_empty_.wait_for(lock, d, [&](){ return not empty(); })) { 71 | out = container_.front(); 72 | container_.pop(); 73 | not_full_.notify_one(); 74 | return true; 75 | } else { 76 | return false; 77 | } 78 | } 79 | }; 80 | 81 | #endif // QUEUE_BLOCK_QUEUE_HPP_ 82 | 83 | -------------------------------------------------------------------------------- /c-cpp/09_queue/circular_queue.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/10. 3 | */ 4 | 5 | #ifndef QUEUE_CIRCULAR_QUEUE_HPP_ 6 | #define QUEUE_CIRCULAR_QUEUE_HPP_ 7 | 8 | template 9 | class CircularQueue { 10 | private: 11 | T* items_ = nullptr; 12 | size_t capacity_ = 0; 13 | size_t head_ = 0; 14 | size_t tail_ = 0; 15 | 16 | public: 17 | CircularQueue() = delete; 18 | CircularQueue(const size_t capacity) : capacity_(capacity) { 19 | items_ = new T[capacity_]; 20 | } 21 | ~CircularQueue() { 22 | if (nullptr != items_) { 23 | delete[] items_; 24 | items_ = nullptr; 25 | } 26 | } 27 | CircularQueue(const CircularQueue& other) : capacity_(other.capacity_) { 28 | items_ = new T[capacity_]; 29 | for (size_t i = other.head_; i != other.tail_; ++i) { 30 | enqueue(other.items_[i]); 31 | } 32 | } 33 | CircularQueue& operator=(const CircularQueue& rhs) { 34 | delete[] items_; 35 | head_ = 0; 36 | tail_ = 0; 37 | capacity_ = rhs.capacity_; 38 | items_ = new T[capacity_]; 39 | for (size_t i = rhs.head_; i != rhs.tail_; ++i) { 40 | enqueue(rhs.items_[i]); 41 | } 42 | return *this; 43 | } 44 | CircularQueue(CircularQueue&& other) : items_(other.items_), 45 | capacity_(other.capacity_), 46 | head_(other.head_), 47 | tail_(other.tail_) { 48 | other.items_ = nullptr; 49 | other.capacity_ = 0; 50 | other.head_ = 0; 51 | other.tail_ = 0; 52 | } 53 | CircularQueue& operator=(CircularQueue&& rhs) { 54 | delete[] items_; 55 | items_ = rhs.items_; 56 | capacity_ = rhs.capacity_; 57 | head_ = rhs.head_; 58 | tail_ = rhs.tail_; 59 | rhs.items_ = nullptr; 60 | rhs.capacity_ = 0; 61 | rhs.head_ = 0; 62 | rhs.tail_ = 0; 63 | return *this; 64 | } 65 | 66 | public: 67 | void enqueue(T item) { 68 | if ((tail_ + 1) % capacity_ == head_) { 69 | throw "Push data into a full queue!"; 70 | } 71 | items_[tail_] = item; 72 | tail_ = (tail_ + 1) % capacity_; 73 | } 74 | T head() const { 75 | if (head_ != tail_) { 76 | return items_[head_]; 77 | } else { 78 | throw "Fetch data from an empty queue!"; 79 | } 80 | } 81 | void dequeue() { 82 | if (head_ != tail_) { 83 | head_ = (head_ + 1) % capacity_; 84 | } else { 85 | throw "Pop data from an empty queue!"; 86 | } 87 | } 88 | 89 | public: 90 | template 91 | void traverse(UnaryFunc do_traverse) { 92 | if (0 == capacity_) return; 93 | for (size_t i = head_; i % capacity_ != tail_; ++i) { 94 | do_traverse(items_[i % capacity_]); 95 | } 96 | } 97 | }; 98 | 99 | #endif // QUEUE_CIRCULAR_QUEUE_HPP_ 100 | -------------------------------------------------------------------------------- /c-cpp/09_queue/circular_queue_test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "circular_queue.hpp" 3 | 4 | int main() { 5 | auto do_traverse = [&](auto item){ std::cout << item << ' '; }; 6 | 7 | CircularQueue circular_queue_1(4); 8 | circular_queue_1.enqueue(1); 9 | circular_queue_1.enqueue(2); 10 | circular_queue_1.enqueue(3); 11 | // circular_queue_1.enqueue(4); // throw 12 | circular_queue_1.traverse(do_traverse); 13 | std::cout << std::endl; 14 | 15 | CircularQueue circular_queue_2(circular_queue_1); // copy constructor 16 | circular_queue_2.traverse(do_traverse); 17 | std::cout << std::endl; 18 | 19 | CircularQueue circular_queue_3(std::move(circular_queue_2)); // move constructor 20 | circular_queue_3.traverse(do_traverse); 21 | std::cout << std::endl; 22 | circular_queue_2.traverse(do_traverse); 23 | std::cout << std::endl; 24 | 25 | std::cout << circular_queue_3.head() << std::endl; 26 | circular_queue_3.dequeue(); 27 | std::cout << circular_queue_3.head() << std::endl; 28 | circular_queue_3.dequeue(); 29 | std::cout << circular_queue_3.head() << std::endl; 30 | circular_queue_3.dequeue(); 31 | // std::cout << circular_queue_3.head() << std::endl; // throw 32 | // circular_queue_3.dequeue(); // throw 33 | 34 | CircularQueue circular_queue_4(1); 35 | circular_queue_4 = circular_queue_1; // copy assignment 36 | circular_queue_4.traverse(do_traverse); 37 | std::cout << std::endl; 38 | 39 | CircularQueue circular_queue_5(100); 40 | circular_queue_5 = std::move(circular_queue_4); // move assignment 41 | circular_queue_5.traverse(do_traverse); 42 | std::cout << std::endl; 43 | circular_queue_4.traverse(do_traverse); 44 | std::cout << std::endl; 45 | 46 | std::cout << circular_queue_5.head() << std::endl; 47 | circular_queue_5.dequeue(); 48 | std::cout << circular_queue_5.head() << std::endl; 49 | circular_queue_5.dequeue(); 50 | std::cout << circular_queue_5.head() << std::endl; 51 | circular_queue_5.dequeue(); 52 | // std::cout << circular_queue_5.head() << std::endl; // throw 53 | // circular_queue_5.dequeue(); // throw 54 | 55 | for (size_t i = 0; i != 4; ++i) { 56 | circular_queue_1.dequeue(); 57 | circular_queue_1.enqueue(i + 4); 58 | circular_queue_1.traverse(do_traverse); 59 | std::cout << std::endl; 60 | } 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /c-cpp/09_queue/concurrency_queue.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/11. 3 | */ 4 | 5 | #ifndef QUEUE_CONCURRENCY_QUEUE_HPP_ 6 | #define QUEUE_CONCURRENCY_QUEUE_HPP_ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | template 14 | class ConcurrencyQueue { 15 | public: 16 | using value_type = T; 17 | using container_type = std::queue; 18 | using size_type = typename container_type::size_type; 19 | 20 | private: 21 | container_type container_; 22 | mutable std::mutex mutex_; 23 | std::condition_variable container_cond_; 24 | 25 | public: 26 | ConcurrencyQueue() = default; 27 | ConcurrencyQueue(const ConcurrencyQueue&) = default; 28 | ConcurrencyQueue(ConcurrencyQueue&&) = default; 29 | ConcurrencyQueue& operator=(const ConcurrencyQueue&) = default; 30 | ConcurrencyQueue& operator=(ConcurrencyQueue&&) = default; 31 | 32 | private: 33 | bool empty_() const { return container_.empty(); } 34 | 35 | public: 36 | bool empty() const { 37 | std::lock_guard lg(mutex_); 38 | return container_.empty(); 39 | } 40 | void push(value_type item) { 41 | std::lock_guard lg(mutex_); 42 | container_.push(std::move(item)); 43 | container_cond_.notify_one(); 44 | } 45 | void wait_and_pop(value_type& out) { 46 | std::unique_lock lk(mutex_); 47 | while (empty_()) { 48 | container_cond_.wait(lk) 49 | } 50 | out = std::move(container_.front()); 51 | container_.pop(); 52 | } 53 | std::shared_ptr wait_and_pop() { 54 | std::unique_lock lk(mutex_); 55 | while (empty_()) { 56 | container_cond_.wait(lk) 57 | } 58 | auto res = std::make_shared(std::move(container_.front())); 59 | container_.pop(); 60 | return res; 61 | } 62 | bool try_pop(value_type& out) { 63 | std::lock_guard lg(mutex_); 64 | if (empty_()) { 65 | return false; 66 | } else { 67 | out = std::move(container_.front()); 68 | container_.pop(); 69 | return true; 70 | } 71 | } 72 | std::shared_ptr try_pop() { 73 | std::lock_guard lg(mutex_); 74 | if (empty_()) { 75 | return nullptr; 76 | } else { 77 | auto res = std::make_shared(std::move(container_.front())); 78 | container_.pop(); 79 | return res; 80 | } 81 | } 82 | }; 83 | 84 | #endif // QUEUE_CONCURRENCY_QUEUE_HPP_ 85 | 86 | -------------------------------------------------------------------------------- /c-cpp/09_queue/dynamic_array_queue.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/10. 3 | */ 4 | 5 | #ifndef QUEUE_DYNAMIC_ARRAY_QUEUE_HPP_ 6 | #define QUEUE_DYNAMIC_ARRAY_QUEUE_HPP_ 7 | 8 | template 9 | class DynamicArrayQueue { 10 | private: 11 | T* items_ = nullptr; 12 | size_t capacity_ = 0; 13 | size_t head_ = 0; 14 | size_t tail_ = 0; 15 | 16 | public: 17 | DynamicArrayQueue() = delete; 18 | DynamicArrayQueue(const size_t capacity) : capacity_(capacity) { 19 | items_ = new T[capacity_]; 20 | } 21 | ~DynamicArrayQueue() { 22 | if (nullptr != items_) { 23 | delete[] items_; 24 | items_ = nullptr; 25 | } 26 | } 27 | DynamicArrayQueue(const DynamicArrayQueue& other) : capacity_(other.capacity_) { 28 | items_ = new T[capacity_]; 29 | for (size_t i = other.head_; i != other.tail_; ++i) { 30 | enqueue(other.items_[i]); 31 | } 32 | } 33 | DynamicArrayQueue& operator=(const DynamicArrayQueue& rhs) { 34 | delete[] items_; 35 | head_ = 0; 36 | tail_ = 0; 37 | capacity_ = rhs.capacity_; 38 | items_ = new T[capacity_]; 39 | for (size_t i = rhs.head_; i != rhs.tail_; ++i) { 40 | enqueue(rhs.items_[i]); 41 | } 42 | return *this; 43 | } 44 | DynamicArrayQueue(DynamicArrayQueue&& other) : items_(other.items_), 45 | capacity_(other.capacity_), 46 | head_(other.head_), 47 | tail_(other.tail_) { 48 | other.items_ = nullptr; 49 | other.capacity_ = 0; 50 | other.head_ = 0; 51 | other.tail_ = 0; 52 | } 53 | DynamicArrayQueue& operator=(DynamicArrayQueue&& rhs) { 54 | delete[] items_; 55 | items_ = rhs.items_; 56 | capacity_ = rhs.capacity_; 57 | head_ = rhs.head_; 58 | tail_ = rhs.tail_; 59 | rhs.items_ = nullptr; 60 | rhs.capacity_ = 0; 61 | rhs.head_ = 0; 62 | rhs.tail_ = 0; 63 | return *this; 64 | } 65 | 66 | public: 67 | void enqueue(T item) { 68 | if (capacity_ == tail_ - head_) { 69 | throw "Push data into a full queue!"; 70 | } 71 | if (capacity_ == tail_) { 72 | // item transport 73 | for (size_t i = head_; i != tail_; ++i) { 74 | items_[i - head_] = items_[i]; 75 | } 76 | tail_ = tail_ - head_; 77 | head_ = 0; 78 | } 79 | items_[tail_++] = item; 80 | } 81 | T head() const { 82 | if (head_ != tail_) { 83 | return items_[head_]; 84 | } else { 85 | throw "Fetch data from an empty queue!"; 86 | } 87 | } 88 | void dequeue() { 89 | if (head_ != tail_) { 90 | ++head_; 91 | } else { 92 | throw "Pop data from an empty queue!"; 93 | } 94 | } 95 | 96 | public: 97 | template 98 | void traverse(UnaryFunc do_traverse) { 99 | for (size_t i = head_; i != tail_; ++i) { 100 | do_traverse(items_[i]); 101 | } 102 | } 103 | }; 104 | 105 | #endif // QUEUE_DYNAMIC_ARRAY_QUEUE_HPP_ 106 | -------------------------------------------------------------------------------- /c-cpp/09_queue/dynamic_array_queue_test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dynamic_array_queue.hpp" 3 | 4 | int main() { 5 | auto do_traverse = [&](auto item){ std::cout << item << ' '; }; 6 | 7 | DynamicArrayQueue dynamic_array_queue_1(3); 8 | dynamic_array_queue_1.enqueue(1); 9 | dynamic_array_queue_1.enqueue(2); 10 | dynamic_array_queue_1.enqueue(3); 11 | // dynamic_array_queue_1.enqueue(4); // throw 12 | dynamic_array_queue_1.traverse(do_traverse); 13 | std::cout << std::endl; 14 | 15 | DynamicArrayQueue dynamic_array_queue_2(dynamic_array_queue_1); // copy constructor 16 | dynamic_array_queue_2.traverse(do_traverse); 17 | std::cout << std::endl; 18 | 19 | DynamicArrayQueue dynamic_array_queue_3(std::move(dynamic_array_queue_2)); // move constructor 20 | dynamic_array_queue_3.traverse(do_traverse); 21 | std::cout << std::endl; 22 | dynamic_array_queue_2.traverse(do_traverse); 23 | std::cout << std::endl; 24 | 25 | std::cout << dynamic_array_queue_3.head() << std::endl; 26 | dynamic_array_queue_3.dequeue(); 27 | std::cout << dynamic_array_queue_3.head() << std::endl; 28 | dynamic_array_queue_3.dequeue(); 29 | std::cout << dynamic_array_queue_3.head() << std::endl; 30 | dynamic_array_queue_3.dequeue(); 31 | // std::cout << dynamic_array_queue_3.head() << std::endl; // throw 32 | // dynamic_array_queue_3.dequeue(); // throw 33 | 34 | DynamicArrayQueue dynamic_array_queue_4(1); 35 | dynamic_array_queue_4 = dynamic_array_queue_1; // copy assignment 36 | dynamic_array_queue_4.traverse(do_traverse); 37 | std::cout << std::endl; 38 | 39 | DynamicArrayQueue dynamic_array_queue_5(100); 40 | dynamic_array_queue_5 = std::move(dynamic_array_queue_4); // move assignment 41 | dynamic_array_queue_5.traverse(do_traverse); 42 | std::cout << std::endl; 43 | dynamic_array_queue_4.traverse(do_traverse); 44 | std::cout << std::endl; 45 | 46 | std::cout << dynamic_array_queue_5.head() << std::endl; 47 | dynamic_array_queue_5.dequeue(); 48 | std::cout << dynamic_array_queue_5.head() << std::endl; 49 | dynamic_array_queue_5.dequeue(); 50 | std::cout << dynamic_array_queue_5.head() << std::endl; 51 | dynamic_array_queue_5.dequeue(); 52 | // std::cout << dynamic_array_queue_5.head() << std::endl; // throw 53 | // dynamic_array_queue_5.dequeue(); // throw 54 | 55 | for (size_t i = 0; i != 3; ++i) { 56 | dynamic_array_queue_1.dequeue(); 57 | dynamic_array_queue_1.enqueue(i + 4); 58 | dynamic_array_queue_1.traverse(do_traverse); 59 | std::cout << std::endl; 60 | } 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /c-cpp/09_queue/linked_queue.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/10. 3 | */ 4 | 5 | #ifndef QUEUE_LINKED_QUEUE_HPP_ 6 | #define QUEUE_LINKED_QUEUE_HPP_ 7 | 8 | #include 9 | 10 | template 11 | struct Node { 12 | using ptr_t = std::shared_ptr>; 13 | T data; 14 | ptr_t next; 15 | 16 | Node(T data_) : data(data_), next(nullptr) {} 17 | Node() : next(nullptr) {} 18 | }; 19 | 20 | template 21 | class LinkedQueue { 22 | public: 23 | using node_type = Node; 24 | using node_ptr_t = typename node_type::ptr_t; 25 | 26 | private: 27 | node_ptr_t head_ = nullptr; 28 | node_ptr_t before_tail_ = nullptr; 29 | 30 | public: 31 | LinkedQueue() = default; 32 | ~LinkedQueue() = default; 33 | LinkedQueue(const LinkedQueue& other) = default; 34 | LinkedQueue& operator=(const LinkedQueue& rhs) = default; 35 | LinkedQueue(LinkedQueue&& other) = default; 36 | LinkedQueue& operator=(LinkedQueue&& rhs) = default; 37 | 38 | public: 39 | void enqueue(T item) { 40 | if (nullptr == head_) { 41 | head_ = std::make_shared(item); 42 | before_tail_ = head_; 43 | } else { 44 | before_tail_->next = std::make_shared(item); 45 | before_tail_ = before_tail_->next; 46 | } 47 | } 48 | T head() const { 49 | if (nullptr != head_) { 50 | return head_->data; 51 | } else { 52 | throw "Fetch data from an empty queue!"; 53 | } 54 | } 55 | void dequeue() { 56 | if (nullptr != head_) { 57 | head_ = head_->next; 58 | if (nullptr == head_) { 59 | before_tail_ = nullptr; 60 | } 61 | } else { 62 | throw "Pop data from an empty queue!"; 63 | } 64 | } 65 | 66 | public: 67 | template 68 | void traverse(UnaryFunc do_traverse) { 69 | for (node_ptr_t work = head_; nullptr != work; work = work->next) { 70 | do_traverse(work->data); 71 | } 72 | } 73 | }; 74 | 75 | #endif // QUEUE_LINKED_QUEUE_HPP_ 76 | -------------------------------------------------------------------------------- /c-cpp/09_queue/linked_queue_test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "linked_queue.hpp" 3 | 4 | int main() { 5 | auto do_traverse = [&](auto item){ std::cout << item << ' '; }; 6 | 7 | LinkedQueue linked_queue_1; 8 | linked_queue_1.enqueue(1); 9 | linked_queue_1.enqueue(2); 10 | linked_queue_1.enqueue(3); 11 | linked_queue_1.traverse(do_traverse); 12 | std::cout << std::endl; 13 | 14 | LinkedQueue linked_queue_2(linked_queue_1); // copy constructor 15 | linked_queue_2.traverse(do_traverse); 16 | std::cout << std::endl; 17 | 18 | LinkedQueue linked_queue_3(std::move(linked_queue_2)); // move constructor 19 | linked_queue_3.traverse(do_traverse); 20 | std::cout << std::endl; 21 | linked_queue_2.traverse(do_traverse); 22 | std::cout << std::endl; 23 | 24 | std::cout << linked_queue_3.head() << std::endl; 25 | linked_queue_3.dequeue(); 26 | std::cout << linked_queue_3.head() << std::endl; 27 | linked_queue_3.dequeue(); 28 | std::cout << linked_queue_3.head() << std::endl; 29 | linked_queue_3.dequeue(); 30 | // std::cout << linked_queue_3.head() << std::endl; // throw 31 | // linked_queue_3.dequeue(); // throw 32 | 33 | LinkedQueue linked_queue_4; 34 | linked_queue_4 = linked_queue_1; // copy assignment 35 | linked_queue_4.traverse(do_traverse); 36 | std::cout << std::endl; 37 | 38 | LinkedQueue linked_queue_5; 39 | linked_queue_5 = std::move(linked_queue_4); // move assignment 40 | linked_queue_5.traverse(do_traverse); 41 | std::cout << std::endl; 42 | linked_queue_4.traverse(do_traverse); 43 | std::cout << std::endl; 44 | 45 | std::cout << linked_queue_5.head() << std::endl; 46 | linked_queue_5.dequeue(); 47 | std::cout << linked_queue_5.head() << std::endl; 48 | linked_queue_5.dequeue(); 49 | std::cout << linked_queue_5.head() << std::endl; 50 | linked_queue_5.dequeue(); 51 | // std::cout << linked_queue_5.head() << std::endl; // throw 52 | // linked_queue_5.dequeue(); // throw 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /c-cpp/09_queue/list_queue/list_queue.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: list_queue.c 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-13 6 | > Desc: 7 | ************************************************************************/ 8 | #include 9 | #include 10 | #include 11 | #include"./list_queue.h" 12 | 13 | /*创建队列头*/ 14 | list_queue *list_queue_create() 15 | { 16 | list_queue * queue = NULL; 17 | 18 | queue = (list_queue *)malloc(sizeof(list_queue)); 19 | if(queue == NULL) 20 | { 21 | return NULL; 22 | } 23 | 24 | queue->num = 0; 25 | queue->head = NULL; 26 | queue->tail = NULL; 27 | 28 | return queue; 29 | } 30 | void list_queue_destroy(list_queue*queue) 31 | { 32 | int i = 0; 33 | int data = 0; 34 | 35 | if ((queue == NULL) || (list_queue_is_empty(queue))) 36 | { 37 | return ; 38 | } 39 | 40 | while(!list_queue_is_empty(queue)) 41 | { 42 | (void)list_queue_dequeue(queue,&data); 43 | } 44 | 45 | free(queue); 46 | return; 47 | } 48 | int list_queue_enqueue(list_queue *queue,int data) 49 | { 50 | queue_node *ptmp = NULL; 51 | 52 | if(queue == NULL) 53 | { 54 | return -1; 55 | } 56 | 57 | ptmp = (queue_node *)malloc(sizeof(queue_node)); 58 | if (ptmp == NULL) 59 | { 60 | return -1; 61 | } 62 | 63 | ptmp->data = data; 64 | ptmp->next = NULL; 65 | if (queue->head == NULL) 66 | { 67 | queue->head = ptmp; 68 | } 69 | else 70 | { 71 | queue->tail->next = ptmp; 72 | 73 | } 74 | queue->tail = ptmp; 75 | queue->num++; 76 | 77 | return 0; 78 | } 79 | 80 | /*出队*/ 81 | int list_queue_dequeue(list_queue *queue,int *data) 82 | { 83 | queue_node * ptmp = NULL; 84 | 85 | if ((queue == NULL) || (data == NULL) || list_queue_is_empty(queue)) 86 | { 87 | return -1; 88 | } 89 | 90 | *data = queue->head->data; 91 | ptmp = queue->head; 92 | queue->head = queue->head->next; 93 | queue->num--; 94 | 95 | if (queue->head == NULL) 96 | { 97 | queue->tail = NULL; 98 | } 99 | 100 | 101 | free(ptmp); 102 | return 0; 103 | } 104 | void list_queue_dump(list_queue*queue) 105 | { 106 | int i = 0; 107 | queue_node *ptmp = NULL; 108 | 109 | if ((queue == NULL) || (list_queue_is_empty(queue))) 110 | { 111 | return; 112 | } 113 | 114 | ptmp = queue->head; 115 | 116 | printf("\r\n----dump queue num = %d--------",queue->num); 117 | while(ptmp != NULL) 118 | { 119 | printf("\r\nnode[%d] = %d",i,ptmp->data); 120 | i++; 121 | ptmp = ptmp->next; 122 | } 123 | printf("\r\n---------------------------------\r\n"); 124 | 125 | return; 126 | } 127 | 128 | int main() 129 | { 130 | int i = 0; 131 | int data = 0; 132 | int ret = 0; 133 | list_queue * queue; 134 | 135 | queue = list_queue_create(); 136 | if (queue == NULL) 137 | { 138 | printf("\r\nlist queue create falied.."); 139 | return 0; 140 | } 141 | 142 | for (i = 0; i < 5; i++) 143 | { 144 | (void)list_queue_enqueue(queue,i); 145 | } 146 | list_queue_dump(queue); 147 | 148 | ret = list_queue_dequeue(queue,&data); 149 | if(ret != 0) 150 | { 151 | printf("\r\nlist queue dequeue %d falied.",data); 152 | } 153 | printf("\r\nlist queue dequeue %d",data); 154 | list_queue_dump(queue); 155 | 156 | 157 | ret = list_queue_dequeue(queue,&data); 158 | if(ret != 0) 159 | { 160 | printf("\r\nlist queue dequeue %d failed.",data); 161 | } 162 | printf("\r\nlist queue dequeue %d",data); 163 | list_queue_dump(queue); 164 | 165 | printf("\r\nlist queue enqueue %d",data); 166 | (void)list_queue_enqueue(queue,data); 167 | list_queue_dump(queue); 168 | 169 | list_queue_destroy(queue); 170 | return 0; 171 | } 172 | -------------------------------------------------------------------------------- /c-cpp/09_queue/list_queue/list_queue.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: list_queue.h 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-13 6 | > Desc: 7 | ************************************************************************/ 8 | 9 | #ifndef LINK_LIST_QUEUE_H 10 | #define LINK_LIST_QUEUE_H 11 | 12 | typedef struct _list_queue_node 13 | { 14 | int data; 15 | struct _list_queue_node *next; 16 | }queue_node; 17 | 18 | typedef struct _list_queue 19 | { 20 | int num; 21 | queue_node *head; 22 | queue_node *tail; 23 | }list_queue; 24 | 25 | #define list_queue_is_empty(queue) ((queue->num) == 0) 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /c-cpp/09_queue/lock_free_queue.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/11. 3 | */ 4 | 5 | #ifndef QUEUE_LOCK_FREE_QUEUE_HPP_ 6 | #define QUEUE_LOCK_FREE_QUEUE_HPP_ 7 | 8 | #include 9 | #include 10 | 11 | template 12 | class LockFreeQueue { 13 | public: 14 | using value_type = T; 15 | 16 | private: 17 | struct node { 18 | std::shared data = nullptr; 19 | node* next = nullptr; 20 | }; 21 | std::atomic head = nullptr; 22 | std::atomic tail = nullptr; 23 | 24 | public: 25 | LockFreeQueue() head(new node), tail(head.load()) {} 26 | LockFreeQueue(const LockFreeQueue&) = delete; 27 | LockFreeQueue(LockFreeQueue&& other) : head(other.head.load()), tail(other.tail.load()) { 28 | other.head.store(nullptr); 29 | other.tail.store(nullptr); 30 | } 31 | LockFreeQueue& operator=(const LockFreeQueue&) = delete; 32 | LockFreeQueue& operator=(LockFreeQueue&& rhs) { 33 | while (node* const old_head = head.load()) { 34 | head.store(old_head->next); 35 | delete old_head; 36 | } 37 | head.store(rhs.head.load()); 38 | tail.store(rhs.tail.load()); 39 | rhs.head.store(nullptr); 40 | rhs.tail.store(nullptr); 41 | } 42 | ~LockFreeQueue() { 43 | while (node* const old_head = head.load()) { 44 | head.store(old_head->next); 45 | delete old_head; 46 | } 47 | } 48 | 49 | private: 50 | node* pop_head() { 51 | node* const res = head.load(); 52 | if (res == tail.load()) { 53 | return nullptr; 54 | } 55 | head.store(res->next); 56 | return res; 57 | } 58 | 59 | public: 60 | bool empty() const { 61 | return head.load() == tail.load(); 62 | } 63 | std::shared_ptr pop() { 64 | node* old_head = pop_head(); 65 | if (nullptr == old_head) { 66 | return nullptr; 67 | } else { 68 | auto res = old_head->data; 69 | delete old_head; 70 | return res; 71 | } 72 | } 73 | void push(value_type new_value) { 74 | auto new_data = std::make_shared(new_value); 75 | node* p = new node; 76 | node* old_tail = tail.load(); 77 | old_tail->data.swap(new_data); 78 | old_tail->next = p; 79 | tail_.store(p); 80 | } 81 | }; 82 | 83 | #endif // QUEUE_LOCK_FREE_QUEUE_HPP_ 84 | 85 | -------------------------------------------------------------------------------- /c-cpp/09_queue/ring_queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | struct ring_queue { 7 | int cap; 8 | int head, tail; 9 | int *_q; 10 | }; 11 | 12 | int alloc_queue(struct ring_queue* queue, int cap) 13 | { 14 | if (!queue || cap < 0) 15 | return -1; 16 | if (queue->_q) 17 | return -1; 18 | 19 | queue->_q = (int *)malloc(cap * sizeof(int)); 20 | if (!queue->_q) 21 | return -1; 22 | 23 | queue->head = queue->tail = 0; 24 | queue->cap = cap; 25 | return 0; 26 | } 27 | 28 | void free_queue(struct ring_queue *queue) 29 | { 30 | queue->cap = 0; 31 | queue->head = queue->tail = 0; 32 | free(queue->_q); 33 | } 34 | 35 | int _valid_index(int curr, int step, int cap) 36 | { 37 | return (curr + step) % cap; 38 | } 39 | 40 | int _next(int curr, int cap) 41 | { 42 | return _valid_index(curr, 1, cap); 43 | } 44 | 45 | bool is_empty(struct ring_queue *queue) 46 | { 47 | return (queue->head == queue->tail); 48 | } 49 | 50 | bool is_full(struct ring_queue *queue) 51 | { 52 | int next_tail = _next(queue->tail, queue->cap); 53 | return (next_tail == queue->head); 54 | } 55 | 56 | int enqueue(struct ring_queue* queue, int elem) 57 | { 58 | if (is_full(queue)) 59 | return -1; 60 | 61 | queue->_q[queue->tail] = elem; 62 | queue->tail = _next(queue->tail, queue->cap); 63 | return 0; 64 | } 65 | 66 | int dequeue(struct ring_queue* queue, int *elem) 67 | { 68 | if (is_empty(queue)) 69 | return -1; 70 | 71 | if (elem) 72 | *elem = queue->_q[queue->head]; 73 | queue->head = _next(queue->head, queue->cap); 74 | return 0; 75 | } 76 | 77 | int size(struct ring_queue* queue) 78 | { 79 | int size = queue->tail - queue->head; 80 | 81 | if (size < 0) 82 | size += queue->cap; 83 | return size; 84 | } 85 | 86 | void dump(struct ring_queue* queue) 87 | { 88 | int i, idx; 89 | 90 | printf("Queue has %d elements with %d capacity\n", 91 | size(queue), queue->cap); 92 | for (i = 0; i < size(queue); i++) { 93 | idx = _valid_index(queue->head, i, queue->cap); 94 | printf("[%02d]: %08d\n", idx, queue->_q[idx]); 95 | } 96 | } 97 | 98 | int main() 99 | { 100 | struct ring_queue queue = {0, 0, 0, NULL}; 101 | int i; 102 | 103 | if (alloc_queue(&queue, 8)) { 104 | printf("Failed to allocate a queue\n"); 105 | return -1; 106 | } 107 | 108 | printf("A new queue is %s\n", is_empty(&queue)?"empty":"not empty"); 109 | 110 | enqueue(&queue, 1); 111 | printf("After enqueue 1 element, queue is %s\n", is_empty(&queue)?"empty":"not empty"); 112 | dequeue(&queue, NULL); 113 | printf("After dequeue 1 element, queue is %s\n", is_empty(&queue)?"empty":"not empty"); 114 | 115 | for (i = 0; i < 7; i++) 116 | enqueue(&queue, i); 117 | printf("After enqueue 7 element, queue is %s\n", is_full(&queue)?"full":"not full"); 118 | 119 | for (i = 0; i < 4; i++) { 120 | dequeue(&queue, NULL); 121 | enqueue(&queue, i); 122 | } 123 | printf("After enqueue/dequeue 4 element, queue is %s\n", 124 | is_full(&queue)?"full":"not full"); 125 | printf("Head is %d, Tail is %d\n", queue.head, queue.tail); 126 | 127 | dump(&queue); 128 | free_queue(&queue); 129 | return 0; 130 | } 131 | -------------------------------------------------------------------------------- /c-cpp/10_recursive/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/10_recursive/.gitkeep -------------------------------------------------------------------------------- /c-cpp/10_recursive/one_two_step.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: one_two_step.c 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-19 6 | > Desc: 7 | ************************************************************************/ 8 | #include 9 | #include 10 | #include 11 | 12 | /*爬楼梯的问题,解决重复计算,采用数据保存方法*/ 13 | 14 | int helper(int n ,int *vlaue) 15 | { 16 | 17 | if(vlaue[n] != 0) 18 | { 19 | return vlaue[n]; 20 | } 21 | 22 | vlaue[n] = helper(n - 1,vlaue) + helper(n - 2,vlaue); 23 | 24 | return vlaue[n]; 25 | } 26 | 27 | int climbStaris(int n) 28 | { 29 | int *vlaue = NULL; 30 | int res = 0; 31 | 32 | vlaue = (int *)malloc(sizeof(int)*(n+1)); 33 | if(vlaue == NULL) 34 | { 35 | return -1; 36 | } 37 | 38 | memset(vlaue,0,sizeof(int)*(n + 1)); 39 | vlaue[0] = 0; 40 | vlaue[1] = 1; 41 | vlaue[2] = 2; 42 | res = helper(n,vlaue); 43 | free(vlaue); 44 | 45 | return res; 46 | } 47 | 48 | int main() 49 | { 50 | 51 | printf("\r\nnum%d ,%d",5,climbStaris(5)); 52 | printf("\r\nnum%d ,%d",6,climbStaris(6)); 53 | printf("\r\nnum%d ,%d",7,climbStaris(7)); 54 | return 0; 55 | } 56 | 57 | 58 | -------------------------------------------------------------------------------- /c-cpp/10_recursive/one_two_step.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class SolutionOFOneTwoStep { 5 | private: 6 | static std::unordered_map result_; 7 | 8 | public: 9 | enum class POLICY { 10 | RECURSIVE, 11 | NONRECURSIVE 12 | }; 13 | 14 | private: 15 | size_t recursive(size_t steps) { 16 | auto iter = result_.find(steps); 17 | if (result_.end() != iter) { // found. 18 | return iter->second; 19 | } else { 20 | size_t res = operator()(steps - 1) + operator()(steps - 2); 21 | result_.insert({steps, res}); 22 | return res; 23 | } 24 | } 25 | size_t nonrecursive(size_t steps) { 26 | auto iter = result_.find(steps); 27 | if (result_.end() != iter) { // found. 28 | return iter->second; 29 | } else { 30 | size_t start; 31 | for (start = steps; start != 2 and result_.end() == result_.find(start); --start) {} 32 | for (size_t i = start; i != steps; ++i) { 33 | result_.insert({i + 1, result_[i - 1] + result_[i]}); 34 | } 35 | return result_[steps]; 36 | } 37 | } 38 | 39 | public: 40 | size_t operator()(size_t steps, const POLICY policy = POLICY::RECURSIVE) { 41 | if (policy == POLICY::RECURSIVE) { 42 | return recursive(steps); 43 | } else if (policy == POLICY::NONRECURSIVE) { 44 | return nonrecursive(steps); 45 | } 46 | } 47 | static void debug() { 48 | for (auto kv : result_) { 49 | std::cout << kv.first << ' ' << kv.second << std::endl; 50 | } 51 | std::cout << std::endl; 52 | } 53 | }; 54 | 55 | std::unordered_map SolutionOFOneTwoStep::result_ = {{1, 1}, {2, 2}}; 56 | 57 | int main() { 58 | SolutionOFOneTwoStep::debug(); 59 | 60 | std::cout << SolutionOFOneTwoStep()(5, SolutionOFOneTwoStep::POLICY::RECURSIVE) << std::endl; 61 | SolutionOFOneTwoStep::debug(); 62 | 63 | std::cout << SolutionOFOneTwoStep()(10, SolutionOFOneTwoStep::POLICY::NONRECURSIVE) << std::endl; 64 | SolutionOFOneTwoStep::debug(); 65 | 66 | std::cout << SolutionOFOneTwoStep()(20, SolutionOFOneTwoStep::POLICY::RECURSIVE) << std::endl; 67 | SolutionOFOneTwoStep::debug(); 68 | 69 | return 0; 70 | } 71 | 72 | -------------------------------------------------------------------------------- /c-cpp/11_sorts/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/11_sorts/.gitkeep -------------------------------------------------------------------------------- /c-cpp/11_sorts/sorts.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct array { 6 | int size; 7 | int used; 8 | int *arr; 9 | }; 10 | 11 | void dump(struct array *array) 12 | { 13 | int idx; 14 | 15 | for (idx = 0; idx < array->used; idx++) 16 | printf("[%02d]: %08d\n", idx, array->arr[idx]); 17 | } 18 | 19 | void alloc(struct array *array) 20 | { 21 | array->arr = (int *)malloc(array->size * sizeof(int)); 22 | } 23 | 24 | void bubble_sort(struct array *array) 25 | { 26 | int i, j; 27 | 28 | if (array->used <= 1) 29 | return; 30 | 31 | for (i = 0; i < array->used; i++) { 32 | bool has_swap = false; 33 | for (j = 0; j < array->used - i - 1; j++) { 34 | if (array->arr[j] > array->arr[j+1]) { 35 | int tmp; 36 | tmp = array->arr[j]; 37 | array->arr[j] = array->arr[j+1]; 38 | array->arr[j+1] = tmp; 39 | has_swap = true; 40 | } 41 | 42 | } 43 | if (!has_swap) 44 | break; 45 | } 46 | } 47 | 48 | void bubble_sort_test() 49 | { 50 | int idx; 51 | struct array ten_int = {10, 0, NULL}; 52 | 53 | alloc(&ten_int); 54 | for (idx = 0; idx < 10; idx++) 55 | ten_int.arr[idx] = 30 - idx; 56 | ten_int.used = 10; 57 | dump(&ten_int); 58 | bubble_sort(&ten_int); 59 | dump(&ten_int); 60 | } 61 | 62 | void insertion_sort(struct array *array) 63 | { 64 | int i, j; 65 | 66 | if (array->used <= 1) 67 | return; 68 | 69 | for (i = 1; i < array->used; i++) { 70 | int val = array->arr[i]; 71 | 72 | for (j = i - 1; j >= 0; j--) { 73 | if (val < array->arr[j]) 74 | array->arr[j+1] = array->arr[j]; 75 | else 76 | break; 77 | } 78 | array->arr[j+1] = val; 79 | } 80 | } 81 | 82 | void insertion_sort_test() 83 | { 84 | int idx; 85 | struct array ten_int = {10, 0, NULL}; 86 | 87 | alloc(&ten_int); 88 | for (idx = 0; idx < 10; idx++) 89 | ten_int.arr[idx] = 30 - idx; 90 | ten_int.used = 10; 91 | dump(&ten_int); 92 | insertion_sort(&ten_int); 93 | dump(&ten_int); 94 | } 95 | 96 | void selection_sort(struct array *array) 97 | { 98 | int i, j; 99 | 100 | if (array->used <= 1) 101 | return; 102 | 103 | for (i = 0; i < array->used - 1; i++) { 104 | int tmp, idx = i; 105 | 106 | for (j = i + 1; j < array->used; j++) 107 | if (array->arr[j] < array->arr[idx]) 108 | idx = j; 109 | 110 | if (idx == i) 111 | continue; 112 | 113 | tmp = array->arr[i]; 114 | array->arr[i] = array->arr[idx]; 115 | array->arr[idx] = tmp; 116 | } 117 | } 118 | 119 | void selection_sort_test() 120 | { 121 | int idx; 122 | struct array ten_int = {10, 0, NULL}; 123 | 124 | alloc(&ten_int); 125 | for (idx = 0; idx < 10; idx++) 126 | ten_int.arr[idx] = 30 - idx; 127 | ten_int.used = 10; 128 | dump(&ten_int); 129 | selection_sort(&ten_int); 130 | dump(&ten_int); 131 | } 132 | 133 | int main() 134 | { 135 | //bubble_sort_test(); 136 | //selection_sort_test(); 137 | insertion_sort_test(); 138 | return 0; 139 | } 140 | -------------------------------------------------------------------------------- /c-cpp/11_sorts/sorts.cpp: -------------------------------------------------------------------------------- 1 | // C program for implementation of selection sort 2 | #include 3 | 4 | void swap(int *xp, int *yp) 5 | { 6 | int temp = *xp; 7 | *xp = *yp; 8 | *yp = temp; 9 | } 10 | 11 | void selectionSort(int arr[], int n) 12 | { 13 | int i, j, min_idx; 14 | 15 | // One by one move boundary of unsorted subarray 16 | for (i = 0; i < n-1; i++) 17 | { 18 | // Find the minimum element in unsorted array 19 | min_idx = i; 20 | for (j = i+1; j < n; j++) 21 | if (arr[j] < arr[min_idx]) 22 | min_idx = j; 23 | 24 | // Swap the found minimum element with the first element 25 | swap(&arr[min_idx], &arr[i]); 26 | } 27 | } 28 | 29 | /* Function to print an array */ 30 | void printArray(int arr[], int size) 31 | { 32 | int i; 33 | for (i=0; i < size; i++) 34 | printf("%d ", arr[i]); 35 | printf("\n"); 36 | } 37 | 38 | // Driver program to test above functions 39 | int main() 40 | { 41 | int arr[] = {64, 25, 12, 22, 11}; 42 | int n = sizeof(arr)/sizeof(arr[0]); 43 | selectionSort(arr, n); 44 | printf("Sorted array: \n"); 45 | printArray(arr, n); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /c-cpp/11_sorts/sorts.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/16. 3 | */ 4 | 5 | #ifndef SORTS_SORTS_HPP_ 6 | #define SORTS_SORTS_HPP_ 7 | 8 | #include 9 | #include 10 | 11 | template ::value_type>> 13 | void bubble_sort(BidirIt first, BidirIt last, BinaryPred comp = BinaryPred()) { 14 | if (std::distance(first, last) <= 1) { return; } 15 | bool flag = true; 16 | for (auto it = first; flag and it != last; ++it) { 17 | flag = false; 18 | for (auto itt = first; itt != last - std::distance(first, it) - 1; ++itt) { 19 | if (comp(*(itt + 1), *itt)) { 20 | std::swap(*itt, *(itt + 1)); 21 | flag = true; 22 | } 23 | } 24 | } 25 | } 26 | 27 | template ::value_type>> 29 | void insertion_sort(BidirIt first, BidirIt last, BinaryPred comp = BinaryPred()) { 30 | if (std::distance(first, last) <= 1) { return; } 31 | for (auto it = first + 1; it != last; ++it) { 32 | const auto target = *it; 33 | auto itt = it; 34 | for (; std::distance(first, itt) > 0 and comp(target, *(itt - 1)); --itt) { 35 | *itt = *(itt - 1); 36 | } 37 | *itt = target; 38 | } 39 | } 40 | 41 | template ::value_type>> 43 | void selection_sort(BidirIt first, BidirIt last, BinaryPred comp = BinaryPred()) { 44 | if (std::distance(first, last) <= 1) { return; } 45 | for (auto it = first; it != last - 1; ++it) { 46 | auto tag = it; 47 | for (auto itt = it + 1; itt != last; ++itt) { 48 | if (comp(*itt, *tag)) { 49 | tag = itt; 50 | } 51 | } 52 | if (tag != it) { 53 | std::swap(*it, *tag); 54 | } 55 | } 56 | } 57 | 58 | template ::value_type>> 60 | void bubble_down_sort(FrwdIt first, FrwdIt last, BinaryPred comp = BinaryPred()) { 61 | if (std::distance(first, last) <= 1) { return; } 62 | for (auto it = first; it != last; ++it) { 63 | for (auto itt = it + 1; itt != last; ++itt) { 64 | if (comp(*itt, *it)) { 65 | std::swap(*it, *itt); 66 | } 67 | } 68 | } 69 | } 70 | 71 | template ::value_type>> 73 | void shell_sort(BidirIt first, BidirIt last, BinaryPred comp = BinaryPred()) { 74 | const size_t len = std::distance(first, last); 75 | if (len <= 1) { return; } 76 | for (size_t step = len / 2; step >= 1; step /= 2) { 77 | for (auto it = first + step; it != last; ++it) { 78 | auto target = *it; 79 | auto itt = it - step; 80 | for (; std::distance(first, itt) >= 0 and comp(target, *itt); itt -= step) { 81 | *(itt + step) = *itt; 82 | } 83 | *(itt + step) = target; 84 | } 85 | } 86 | } 87 | 88 | #endif // SORTS_SORTS_HPP_ 89 | 90 | -------------------------------------------------------------------------------- /c-cpp/11_sorts/sorts_jinshaohui.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: sorts_jinshaohui.c 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-19 6 | > Desc: 7 | ************************************************************************/ 8 | #include 9 | #include 10 | #include 11 | 12 | #define SWAP(a,b) \ 13 | do{\ 14 | (a) ^= (b);\ 15 | (b) ^= (a);\ 16 | (a) ^= (b);\ 17 | }while(0) 18 | 19 | /*冒泡排序*/ 20 | void bubble_sort(int a[],int size) 21 | { 22 | int i = 0; 23 | int j = 0; 24 | int swap_flg = 0; 25 | 26 | if (size < 1) 27 | { 28 | return; 29 | } 30 | 31 | for (i = size - 1; i > 0; i--)/*排序的趟数*/ 32 | { 33 | swap_flg = 0;/*每次设置交换标识为0*/ 34 | for (j = 0; j < i; j++)/*本趟排序的遍历元素个数*/ 35 | { 36 | if (a[j] > a[j + 1]) 37 | { 38 | SWAP(a[j],a[j+1]); 39 | swap_flg = 1; 40 | } 41 | } 42 | /*本趟数,无数据交换的话,说明已经有序,直接退出*/ 43 | if (swap_flg == 0) 44 | { 45 | break; 46 | } 47 | } 48 | return; 49 | } 50 | 51 | /*插入排序*/ 52 | void insert_sort(int a[],int size) 53 | { 54 | int i = 0; 55 | int j = 0; 56 | int key = 0; 57 | 58 | for (i = 1; i < size; i ++)/*需要插入的元素个数*/ 59 | { 60 | key = a[i];/*保存插入的元素数据*/ 61 | j = i - 1; 62 | /* i 之前的元素都是有序的,找到比key小的插入到他后面, 63 | * 比key大的,需要往后挪一个位置*/ 64 | while((j >= 0) && (a[j] > key)) 65 | { 66 | a[j + 1] = a[j]; 67 | j--; 68 | } 69 | a[j + 1] = key; 70 | } 71 | 72 | return; 73 | } 74 | /*选择排序*/ 75 | void select_sort(int a[],int size) 76 | { 77 | int i = 0; 78 | int j = 0; 79 | int min = 0; 80 | 81 | for (i = 0; i < size - 1; i++) 82 | { 83 | min = i; 84 | for (j = i + 1; j < size; j++) 85 | { 86 | if (a[j] < a[min]) 87 | { 88 | min = j; 89 | } 90 | } 91 | 92 | if (min != i) 93 | { 94 | SWAP(a[i],a[min]); 95 | } 96 | } 97 | return; 98 | } 99 | 100 | void dump(int a[],int size) 101 | { 102 | int i = 0; 103 | 104 | printf("\r\n"); 105 | for (i = 0; i < size; i++ ) 106 | { 107 | printf("%d ",a[i]); 108 | } 109 | printf("\r\n"); 110 | return; 111 | } 112 | 113 | int main() 114 | { 115 | int a[10] = {9,11,4,15,16,3,20,44,5,10}; 116 | 117 | //bubble_sort(a,sizeof(a)/sizeof(int)); 118 | //insert_sort(a,sizeof(a)/sizeof(int)); 119 | select_sort(a,sizeof(a)/sizeof(int)); 120 | 121 | dump(a,sizeof(a)/sizeof(int)); 122 | 123 | return 0; 124 | } 125 | -------------------------------------------------------------------------------- /c-cpp/11_sorts/sorts_test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "sorts.hpp" 5 | 6 | int main() { 7 | const std::vector test_data{1, 2, 3, 0}; 8 | 9 | std::vector a(test_data.begin(), test_data.end()); 10 | bubble_sort(a.begin(), a.end()); 11 | for (auto i : a) { 12 | std::cout << i << ' '; 13 | } 14 | std::cout << '\n'; 15 | 16 | std::vector b(test_data.begin(), test_data.end()); 17 | insertion_sort(b.begin(), b.end()); 18 | for (auto i : b) { 19 | std::cout << i << ' '; 20 | } 21 | std::cout << '\n'; 22 | 23 | std::vector c(test_data.begin(), test_data.end()); 24 | selection_sort(c.begin(), c.end()); 25 | for (auto i : c) { 26 | std::cout << i << ' '; 27 | } 28 | std::cout << '\n'; 29 | 30 | std::vector d(test_data.begin(), test_data.end()); 31 | bubble_down_sort(d.begin(), d.end()); 32 | for (auto i : d) { 33 | std::cout << i << ' '; 34 | } 35 | std::cout << '\n'; 36 | 37 | std::vector e(test_data.begin(), test_data.end()); 38 | shell_sort(e.begin(), e.end()); 39 | for (auto i : e) { 40 | std::cout << i << ' '; 41 | } 42 | std::cout << '\n'; 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /c-cpp/12_sorts/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/12_sorts/.gitkeep -------------------------------------------------------------------------------- /c-cpp/12_sorts/merge_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void dump(int *arr, int size) 7 | { 8 | int idx; 9 | 10 | for (idx = 0; idx < size; idx++) 11 | printf("%08d\n", arr[idx]); 12 | } 13 | 14 | void __merge(int *arr, int p, int q, int r) 15 | { 16 | int *tmp; 17 | int i, j, k; 18 | 19 | tmp = (int*)malloc((r - p + 1) * sizeof(int)); 20 | 21 | if (!tmp) 22 | abort(); 23 | 24 | for (i = p, j = q + 1, k = 0; i <= q && j <= r;) { 25 | if (arr[i] <= arr[j]) 26 | tmp[k++] = arr[i++]; 27 | else 28 | tmp[k++] = arr[j++]; 29 | } 30 | 31 | if (i == q + 1) { 32 | for (; j <= r;) 33 | tmp[k++] = arr[j++]; 34 | } else { 35 | for (; i <= q;) 36 | tmp[k++] = arr[i++]; 37 | } 38 | 39 | memcpy(arr + p, tmp, (r - p + 1) * sizeof(int)); 40 | free(tmp); 41 | } 42 | 43 | void __merge_sort(int *arr, int p, int r) 44 | { 45 | int q; 46 | 47 | if (p >= r) 48 | return; 49 | 50 | q = (p + r) / 2; 51 | __merge_sort(arr, p, q); 52 | __merge_sort(arr, q + 1, r); 53 | __merge(arr, p, q, r); 54 | } 55 | 56 | void merge_sort(int *arr, int size) 57 | { 58 | __merge_sort(arr, 0, size - 1); 59 | } 60 | 61 | void merge_verify() 62 | { 63 | int test[10] = {5, 8, 9, 23, 67, 1, 3, 7, 31, 56}; 64 | 65 | __merge(test, 0, 4, 9); 66 | 67 | dump(test, 10); 68 | } 69 | 70 | void merge_sort_test() 71 | { 72 | int test[10] = {5, 8, 9, 23, 67, 1, 3, 7, 31, 56}; 73 | 74 | merge_sort(test, 10); 75 | 76 | dump(test, 10); 77 | } 78 | 79 | int main() 80 | { 81 | //merge_verify(); 82 | merge_sort_test(); 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /c-cpp/12_sorts/merge_sort.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/17. 3 | */ 4 | 5 | #ifndef SORTS_MERGE_SORT_HPP_ 6 | #define SORTS_MERGE_SORT_HPP_ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | namespace detail { 14 | template ::value_type>> 16 | OutputIt merge(InputIt1 first1, InputIt1 last1, 17 | InputIt2 first2, InputIt2 last2, 18 | OutputIt d_first, 19 | BinaryPred comp = BinaryPred()) { 20 | for (; first1 != last1; ++d_first) { 21 | if (first2 == last2) { 22 | return std::copy(first1, last1, d_first); 23 | } 24 | if (comp(*first2, *first1)) { 25 | *d_first = *first2; 26 | ++first2; 27 | } else { 28 | *d_first = *first1; 29 | ++first1; 30 | } 31 | } 32 | return std::copy(first2, last2, d_first); 33 | } 34 | } // namespace detail 35 | 36 | template ::value_type>> 38 | void merge_sort(FrwdIt first, FrwdIt last, BinaryPred comp = BinaryPred()) { 39 | const auto len = std::distance(first, last); 40 | if (len <= 1) { return; } 41 | auto cut = first + len / 2; 42 | merge_sort(first, cut, comp); 43 | merge_sort(cut, last, comp); 44 | std::vector::value_type> tmp; 45 | tmp.reserve(len); 46 | detail::merge(first, cut, cut, last, std::back_inserter(tmp), comp); 47 | std::copy(tmp.begin(), tmp.end(), first); 48 | } 49 | 50 | template ::value_type>> 52 | void inplace_merge_sort(BidirIt first, BidirIt last, BinaryPred comp = BinaryPred()) { 53 | const auto len = std::distance(first, last); 54 | if (len <= 1) { return; } 55 | auto cut = first + len / 2; 56 | inplace_merge_sort(first, cut, comp); 57 | inplace_merge_sort(cut, last, comp); 58 | std::inplace_merge(first, cut, last, comp); 59 | } 60 | 61 | #endif // SORTS_MERGE_SORT_HPP_ 62 | 63 | -------------------------------------------------------------------------------- /c-cpp/12_sorts/merge_sort_test.cc: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/17. 3 | */ 4 | 5 | #include 6 | #include 7 | #include "merge_sort.hpp" 8 | 9 | int main() { 10 | const std::vector test_data{0, -1, 3, 190, -500}; 11 | 12 | std::vector a{test_data}; 13 | merge_sort(a.begin(), a.end()); 14 | for (auto i : a) { 15 | std::cout << i << ' '; 16 | } 17 | std::cout << std::endl; 18 | 19 | std::vector b{test_data}; 20 | inplace_merge_sort(b.begin(), b.end()); 21 | for (auto i : b) { 22 | std::cout << i << ' '; 23 | } 24 | std::cout << std::endl; 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /c-cpp/12_sorts/my12_sorts/merge_sort.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: merge_sort.c 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-19 6 | > Desc: 7 | ************************************************************************/ 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #define SORT_MAX (1000000) 14 | 15 | void dump(int a[],int size); 16 | void merge_sentry(int a[],int middle,int left,int right) 17 | { 18 | int *pleft = NULL; 19 | int *pright = NULL; 20 | int i = 0; 21 | int j = 0; 22 | int k = 0; 23 | int left_size = middle - left + 1; 24 | int right_size = right - middle; 25 | 26 | 27 | pleft = (int *)malloc(sizeof(int)*(left_size + 1)); 28 | assert(pleft != NULL); 29 | pright = (int *)malloc(sizeof(int)*(right_size + 1)); 30 | assert(pright != NULL); 31 | 32 | for(i = 0; i < left_size; i ++) 33 | { 34 | pleft[i] = a[left + i]; 35 | } 36 | pleft[left_size] = SORT_MAX; 37 | for(i = 0; i < right_size; i ++) 38 | { 39 | pright[i] = a[middle + 1 + i]; 40 | } 41 | pright[right_size] = SORT_MAX; 42 | 43 | for (k = left,i = 0,j = 0; k <= right; k++) 44 | { 45 | if (pleft[i] <= pright[j]) 46 | { 47 | a[k] = pleft[i++]; 48 | } 49 | else 50 | { 51 | a[k] = pright[j++]; 52 | } 53 | } 54 | 55 | free(pleft); 56 | free(pright); 57 | 58 | return; 59 | } 60 | 61 | /*两个有序数组合并*/ 62 | void merge(int a[],int middle,int left,int right) 63 | { 64 | int *tmp = NULL; 65 | int i = 0; 66 | int j = 0; 67 | int k = 0; 68 | 69 | tmp = (int*)malloc((right - left + 1)*sizeof(int)); 70 | assert(tmp != NULL); 71 | 72 | i = left; 73 | j = middle + 1; 74 | 75 | while(1) 76 | { 77 | if((i > middle) || (j > right)) 78 | { 79 | break; 80 | } 81 | 82 | if (a[i] > a[j]) 83 | { 84 | tmp[k++] = a[j++]; 85 | } 86 | else 87 | { 88 | tmp[k++] = a[i++]; 89 | } 90 | } 91 | 92 | if (i > middle) 93 | { 94 | while(j <= right) 95 | { 96 | tmp[k++] = a[j++]; 97 | } 98 | } 99 | else 100 | { 101 | while(i <= middle) 102 | { 103 | tmp[k++] = a[i++]; 104 | } 105 | } 106 | 107 | memcpy((a + left),tmp,(right - left + 1)*sizeof(int)); 108 | 109 | free(tmp); 110 | 111 | return ; 112 | } 113 | 114 | void merge_sort(int a[],int left,int right) 115 | { 116 | int middle = 0; 117 | 118 | if(left >= right) 119 | { 120 | return; 121 | } 122 | 123 | middle = (left + right)/2; 124 | 125 | merge_sort(a,left,middle); 126 | merge_sort(a,middle + 1,right); 127 | 128 | merge_sentry(a,middle,left,right); 129 | 130 | return; 131 | } 132 | 133 | void dump(int a[],int size) 134 | { 135 | int i = 0; 136 | 137 | if(size == 0) 138 | { 139 | return; 140 | } 141 | 142 | printf("\r\n"); 143 | for (i = 0; i < size; i++ ) 144 | { 145 | printf("%d ",a[i]); 146 | } 147 | printf("\r\n"); 148 | return; 149 | } 150 | int main() 151 | { 152 | int a[10] = {30,20,10,15,4,8,40,80,20,9}; 153 | 154 | merge_sort(a,0,9); 155 | 156 | dump(a,10); 157 | 158 | return 0; 159 | } 160 | 161 | -------------------------------------------------------------------------------- /c-cpp/12_sorts/my12_sorts/quick_sort.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: quick_sort.c 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-19 6 | > Desc: 7 | ************************************************************************/ 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | /* SWAP 使用必须主要,不能是同一个数据进行交换*/ 14 | #define SWAP(a,b) \ 15 | do{\ 16 | (a) ^= (b);\ 17 | (b) ^= (a);\ 18 | (a) ^= (b);\ 19 | }while(0) 20 | 21 | int partition2(int a[],int left,int right) 22 | { 23 | int i = left; 24 | int j = left; 25 | 26 | for(; j < right;j++) 27 | { 28 | if (a[j] < a[right]) 29 | { 30 | if(i != j) 31 | { 32 | SWAP(a[i],a[j]); 33 | } 34 | i++; 35 | } 36 | } 37 | 38 | if(i != right) 39 | { 40 | SWAP(a[i],a[right]); 41 | } 42 | 43 | return i; 44 | } 45 | 46 | int partition(int a[],int left,int right) 47 | { 48 | int i = left; 49 | int j = right; 50 | int key = a[left]; 51 | 52 | while(i < j) 53 | { 54 | while((i < j)&& (a[j] >= key)) 55 | { 56 | j--; 57 | } 58 | if (i < j) 59 | { 60 | a[i] = a[j]; 61 | } 62 | while((i < j) && a[i] <= key) 63 | { 64 | i++; 65 | } 66 | 67 | if (i= right) 82 | { 83 | return; 84 | } 85 | 86 | q = partition2(a,left,right); 87 | quick_sort(a,left,(q - 1)); 88 | quick_sort(a,(q + 1),right); 89 | return; 90 | } 91 | 92 | void dump(int a[],int size) 93 | { 94 | int i = 0; 95 | 96 | printf("\r\n"); 97 | for (i = 0; i < size; i++ ) 98 | { 99 | printf("%d ",a[i]); 100 | } 101 | printf("\r\n"); 102 | return; 103 | } 104 | 105 | int helper(int a[],int left,int right,int k) 106 | { 107 | int q = 0; 108 | 109 | q = partition(a,left,right); 110 | 111 | if (q > (k - 1)) 112 | { 113 | return helper(a,left,q-1,k); 114 | } 115 | else if (q < (k - 1)) 116 | { 117 | return helper(a,q+1,right,k); 118 | } 119 | 120 | return a[q]; 121 | } 122 | 123 | /*求无序数组中从小到大第K个元素的数值*/ 124 | int findKthlagest(int a[],int size,int k) 125 | { 126 | return helper(a,0,size-1,k); 127 | } 128 | 129 | 130 | int main() 131 | { 132 | int a[10] = {30,20,10,15,4,8,40,80,20,9}; 133 | int k; 134 | 135 | scanf("%d",&k); 136 | 137 | printf("\r\n从小到大排序,第%d个元素数值是%d",k,findKthlagest(a,10,k)); 138 | 139 | quick_sort(a,0,9); 140 | 141 | dump(a,10); 142 | 143 | return 0; 144 | } 145 | 146 | -------------------------------------------------------------------------------- /c-cpp/12_sorts/quick_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void dump(int *arr, int size) 7 | { 8 | int idx; 9 | 10 | for (idx = 0; idx < size; idx++) 11 | printf("%08d\n", arr[idx]); 12 | } 13 | 14 | void swap(int *a, int *b) 15 | { 16 | int tmp = *a; 17 | *a = *b; 18 | *b = tmp; 19 | } 20 | 21 | int partition(int *arr, int p, int r) 22 | { 23 | //int pivot = arr[r]; 24 | int i, j; 25 | 26 | i = j = p; 27 | 28 | for (; j < r; j++) { 29 | if (arr[j] < arr[r]) { 30 | if(i != j) 31 | { 32 | swap(arr + i, arr + j); 33 | 34 | } 35 | i++; 36 | } 37 | } 38 | 39 | swap(arr + i, arr + r); 40 | return i; 41 | } 42 | 43 | void __quick_sort(int *arr, int p, int r) 44 | { 45 | int q; 46 | 47 | if (p >= r) 48 | return; 49 | 50 | q = partition(arr, p, r); 51 | __quick_sort(arr, p, q-1); 52 | __quick_sort(arr, q+1, r); 53 | } 54 | 55 | void quick_sort(int *arr, int size) 56 | { 57 | __quick_sort(arr, 0, size - 1); 58 | } 59 | 60 | void quick_sort_test() 61 | { 62 | int test[10] = {5, 8, 9, 23, 67, 1, 3, 7, 31, 56}; 63 | 64 | quick_sort(test, 10); 65 | 66 | dump(test, 10); 67 | } 68 | 69 | int main() 70 | { 71 | quick_sort_test(); 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /c-cpp/12_sorts/quick_sort.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/17. 3 | */ 4 | 5 | #ifndef SORTS_QUICK_SORT_HPP_ 6 | #define SORTS_QUICK_SORT_HPP_ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | namespace detail { 14 | template > 15 | const T& median(const T& a, const T& b, const T& c, Compare comp = Compare()) { 16 | if (comp(a, b) and comp(b, c) or comp(c, b) and comp(b, a)) { 17 | return b; 18 | } else if (comp(b, c) and comp(c, a) or comp(a, c) and comp(c, b)) { 19 | return c; 20 | } else { 21 | return a; 22 | } 23 | } 24 | 25 | template ::value_type, 27 | typename Compare = std::less> 28 | const T& iter_median(Iter a, Iter b, Iter c, Compare comp = Compare()) { 29 | return median(*a, *b, *c, comp); 30 | } 31 | 32 | template ::value_type, 34 | typename Compare = std::less> 35 | std::pair inplace_partition(BidirIt first, 36 | BidirIt last, 37 | const T& pivot, 38 | Compare comp = Compare()) { 39 | BidirIt last_less, last_greater, first_equal, last_equal; 40 | for (last_less = first, last_greater = first, first_equal = last; 41 | last_greater != first_equal; ) { 42 | if (comp(*last_greater, pivot)) { 43 | std::iter_swap(last_greater++, last_less++); 44 | } else if (comp(pivot, *last_greater)) { 45 | ++last_greater; 46 | } else { // pivot == *last_greater 47 | std::iter_swap(last_greater, --first_equal); 48 | } 49 | } 50 | const auto cnt = std::distance(first_equal, last); 51 | std::swap_ranges(first_equal, last, last_less); 52 | first_equal = last_less; 53 | last_equal = first_equal + cnt; 54 | return {first_equal, last_equal}; 55 | } 56 | } // namespace detail 57 | 58 | template ::value_type, 60 | typename Compare = std::less> 61 | void quick_sort(BidirIt first, BidirIt last, Compare comp = Compare()) { 62 | for (auto size = std::distance(first, last); size > 1; size = std::distance(first, last)) { 63 | const T pivot = detail::iter_median(first, last - 1, first + size / 2, comp); 64 | const auto eq = detail::inplace_partition(first, last, pivot, comp); 65 | quick_sort(first, eq.first, comp); 66 | first = eq.second; // Liam Huang: economize half of recursive calling. 67 | } 68 | } 69 | 70 | #endif // SORTS_QUICK_SORT_HPP_ 71 | 72 | -------------------------------------------------------------------------------- /c-cpp/12_sorts/quick_sort_test.cc: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/17. 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "quick_sort.hpp" 10 | 11 | void test_quick_sort(std::vector test_data) { 12 | quick_sort(test_data.begin(), test_data.end()); 13 | std::transform(test_data.begin(), test_data.end(), 14 | std::ostream_iterator(std::cout, " "), [](int i){ return i; }); 15 | std::cout << '\n'; 16 | } 17 | 18 | int main() { 19 | test_quick_sort({-3, -1, 1, -2, -3, 0, -3, 100, 1, 1, -100}); 20 | test_quick_sort({1, 1, 1}); 21 | test_quick_sort({1, 0, -1}); 22 | test_quick_sort({1}); 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /c-cpp/13_sorts/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/13_sorts/.gitkeep -------------------------------------------------------------------------------- /c-cpp/13_sorts/bucket_sort.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/26. 3 | */ 4 | 5 | #ifndef SORTS_BUCKET_SORT_HPP_ 6 | #define SORTS_BUCKET_SORT_HPP_ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | template ::value_type, 16 | typename Compare = std::less> 17 | void bucket_sort(IterT first, IterT last, Compare comp = Compare()) { 18 | const T min = *std::min_element(first, last), max = *std::max_element(first, last); 19 | const T range = max + 1 - min; 20 | const size_t bucket_num = (range - 1) / BucketSize + 1; 21 | 22 | std::vector> buckets(bucket_num); 23 | for (auto b : buckets) { 24 | b.reserve(2 * BucketSize); 25 | } 26 | 27 | for (IterT i = first; i != last; ++i) { 28 | size_t idx = (*i - min) / BucketSize; 29 | buckets[idx].emplace_back(*i); 30 | } 31 | 32 | IterT dest = first; 33 | for (auto b : buckets) { 34 | std::sort(b.begin(), b.end(), comp); 35 | std::copy(b.begin(), b.end(), dest); 36 | dest += b.size(); 37 | } 38 | 39 | return; 40 | } 41 | 42 | #endif // SORTS_BUCKET_SORT_HPP_ 43 | 44 | -------------------------------------------------------------------------------- /c-cpp/13_sorts/bucket_sort_test.cc: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/26. 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "bucket_sort.hpp" 10 | 11 | template > 15 | void test_bucket_sort(Container cont, Compare comp = Compare()) { 16 | bucket_sort(cont.begin(), cont.end(), comp); 17 | std::transform(cont.begin(), cont.end(), std::ostream_iterator(std::cout, " "), 18 | [](T i){ return i; }); 19 | std::cout << std::endl; 20 | } 21 | 22 | int main() { 23 | std::vector test{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9}; 24 | 25 | test_bucket_sort<2>(test); // 1 1 2 3 3 4 5 5 5 6 7 8 9 9 9 26 | test_bucket_sort<3>(test); // 1 1 2 3 3 4 5 5 5 6 7 8 9 9 9 27 | test_bucket_sort<4>(test); // 1 1 2 3 3 4 5 5 5 6 7 8 9 9 9 28 | test_bucket_sort<5>(test); // 1 1 2 3 3 4 5 5 5 6 7 8 9 9 9 29 | test_bucket_sort<6>(test); // 1 1 2 3 3 4 5 5 5 6 7 8 9 9 9 30 | 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /c-cpp/13_sorts/counting_sort.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/26. 3 | */ 4 | 5 | #ifndef SORTS_COUNTING_SORT_HPP_ 6 | #define SORTS_COUNTING_SORT_HPP_ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | template ::value_type> 15 | void counting_sort(IterT first, IterT last) { 16 | const auto len = std::distance(first, last); 17 | if (len < 2) { return; } 18 | 19 | const T max = *std::max_element(first, last); 20 | if (max == 0) { return; } 21 | 22 | std::vector counter(max + 1); 23 | for (IterT i = first; i != last; ++i) { 24 | ++counter[*i]; 25 | } 26 | for (size_t i = 1; i != max + 1; ++i) { 27 | const size_t j = max - i; 28 | counter[j] += counter[j + 1]; // Liam Huang: count of numbers that is not less than j. 29 | } 30 | 31 | std::vector temp(len); 32 | for (IterT i = first; i != last; ++i) { 33 | temp[len - counter[*i]] = *i; 34 | --counter[*i]; // Liam Huang: stable for relative position. 35 | } 36 | std::copy(temp.begin(), temp.end(), first); 37 | } 38 | 39 | #endif // SORTS_COUNTING_SORT_HPP_ 40 | 41 | -------------------------------------------------------------------------------- /c-cpp/13_sorts/counting_sort_test.cc: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/26. 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "counting_sort.hpp" 10 | 11 | template 13 | void test_counting_sort(Container cont) { 14 | counting_sort(cont.begin(), cont.end()); 15 | std::transform(cont.begin(), cont.end(), std::ostream_iterator(std::cout, " "), 16 | [](T i){ return i; }); 17 | std::cout << std::endl; 18 | } 19 | 20 | int main() { 21 | // Liam Huang: pi for test 22 | const std::vector test1{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3}; 23 | const std::vector test2{2, 3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9}; 24 | const std::vector test3{5, 0, 2, 8, 8, 4, 1, 9, 7, 1, 6, 9, 3, 9, 9}; 25 | const std::vector test4{3, 7, 5, 1, 0, 5, 8, 2, 0, 9, 7, 4, 9, 4, 4}; 26 | const std::vector test5{5, 9, 2, 3, 0, 7, 8, 1, 6, 4, 0, 6, 2, 8, 6}; 27 | 28 | test_counting_sort(test1); // 1 1 2 3 3 3 4 5 5 5 6 7 8 9 9 9 29 | test_counting_sort(test2); // 2 2 2 3 3 3 3 4 4 6 6 7 8 8 9 30 | test_counting_sort(test3); // 0 1 1 2 3 4 5 6 7 8 8 9 9 9 9 31 | test_counting_sort(test4); // 0 0 1 2 3 4 4 4 5 5 7 7 8 9 9 32 | test_counting_sort(test5); // 0 0 1 2 2 3 4 5 6 6 6 7 8 8 9 33 | 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /c-cpp/14_sorts/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/14_sorts/.gitkeep -------------------------------------------------------------------------------- /c-cpp/14_sorts/analytics_of_std_sort.md: -------------------------------------------------------------------------------- 1 | # C++ STL 中的 std::sort 分析 2 | 3 | 参见 [Liam Huang 的博客](https://liam.page/)中的 3 篇文章: 4 | 5 | * [谈谈基于比较的排序算法的复杂度下界](https://liam.page/2018/08/28/lower-bound-of-comparation-based-sort-algorithm/) 6 | * [谈谈内省式排序算法](https://liam.page/2018/08/29/introspective-sort/) 7 | * [谈谈 STL 中的 std::sort](https://liam.page/2018/09/18/std-sort-in-STL/) 8 | 9 | -------------------------------------------------------------------------------- /c-cpp/14_sorts/counting_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void dump(int *arr, int size) 7 | { 8 | int i; 9 | 10 | for (i = 0; i < size; i++) 11 | printf("%08d\n", arr[i]); 12 | } 13 | 14 | // content in arr must be positive integer 15 | void counting_sort(int *arr, int size) 16 | { 17 | int max, i; 18 | int *count, *tmp; 19 | 20 | if (size <= 1) 21 | return; 22 | 23 | max = 0; 24 | // find the biggest integer 25 | for (i = 0; i < size; i++) { 26 | if (max < arr[i]) 27 | max = arr[i]; 28 | } 29 | 30 | // init count to 0 31 | count = (int*)malloc((max+1) * sizeof(int)); 32 | tmp = (int*)malloc(size * sizeof(int)); 33 | if (!count || !tmp) 34 | return; 35 | memset(count, 0, (max + 1) * sizeof(int)); 36 | 37 | // counting 38 | for (i = 0; i < size; i++) 39 | count[arr[i]]++; 40 | for (i = 1; i < max + 1; i++) 41 | count[i] = count[i-1] + count[i]; 42 | 43 | // iterate arr and put it to the correct index in tmp 44 | for (i = 0; i < size; i++){ 45 | int index = count[arr[i]] - 1; 46 | tmp[index] = arr[i]; 47 | count[arr[i]]--; 48 | } 49 | 50 | // move back to arr 51 | memcpy(arr, tmp, size * sizeof(int)); 52 | } 53 | 54 | void counting_sort_test() 55 | { 56 | int test_data[10] = {3, 23, 98, 1, 27, 36, 52, 89, 76, 44}; 57 | 58 | counting_sort(test_data, 10); 59 | dump(test_data, 10); 60 | } 61 | 62 | int main() 63 | { 64 | counting_sort_test(); 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /c-cpp/15_bsearch/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/15_bsearch/.gitkeep -------------------------------------------------------------------------------- /c-cpp/15_bsearch/binary_search.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | typedef int(*bs)(int *arr, int size, int val); 7 | 8 | int binary_search_r(int *arr, int size, int val) 9 | { 10 | int mid = size / 2; 11 | int idx; 12 | 13 | if (arr[mid] == val) 14 | return mid; 15 | 16 | // mid == 0 means size == 1 17 | // so the only element in array doesn't equal to val 18 | if (!mid) 19 | return -1; 20 | 21 | if (arr[mid] < val) { 22 | idx = binary_search_r(arr + mid + 1, size - mid - 1, val); 23 | if (idx != -1) 24 | idx += mid + 1; 25 | } else { 26 | idx = binary_search_r(arr, mid, val); 27 | } 28 | 29 | return idx; 30 | } 31 | 32 | int binary_search_i(int *arr, int size, int val) 33 | { 34 | int low = 0, high = size - 1, mid; 35 | 36 | while (low <= high) { 37 | mid = (low + high) / 2; 38 | if (arr[mid] == val) 39 | return mid; 40 | 41 | if (arr[mid] < val) 42 | low = mid + 1; 43 | else 44 | high = mid - 1; 45 | } 46 | 47 | return -1; 48 | } 49 | 50 | void iteratioin_test(bs binary_search) 51 | { 52 | int arr[10] = {1, 4, 5, 9, 12, 19, 21, 28, 31, 36}; 53 | int idx; 54 | 55 | idx = binary_search(arr, 10, 12); 56 | if (idx != -1) 57 | printf("find 12 at %d\n", idx); 58 | else 59 | printf("12 not in arr \n"); 60 | 61 | idx = binary_search(arr, 10, 13); 62 | if (idx != -1) 63 | printf("find 13 at %d\n", idx); 64 | else 65 | printf("13 not in arr \n"); 66 | 67 | idx = binary_search(arr, 10, 1); 68 | if (idx != -1) 69 | printf("find 1 at %d\n", idx); 70 | else 71 | printf("1 not in arr \n"); 72 | 73 | idx = binary_search(arr, 10, 36); 74 | if (idx != -1) 75 | printf("find 36 at %d\n", idx); 76 | else 77 | printf("36 not in arr \n"); 78 | 79 | idx = binary_search(arr, 10, 31); 80 | if (idx != -1) 81 | printf("find 31 at %d\n", idx); 82 | else 83 | printf("31 not in arr \n"); 84 | 85 | } 86 | 87 | int main() 88 | { 89 | printf("=== Test iteration version:\n"); 90 | iteratioin_test(binary_search_i); 91 | 92 | printf("=== Test recursive version:\n"); 93 | iteratioin_test(binary_search_r); 94 | 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /c-cpp/15_bsearch/bsearch.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/24. 3 | */ 4 | 5 | #ifndef BSEARCH_BSEARCH_HPP_ 6 | #define BSEARCH_BSEARCH_HPP_ 7 | 8 | #include 9 | #include 10 | 11 | // Liam Huang: The algorithm works right with iterators that meet the ForwardIterator requirement, 12 | // but with a bad time complexity. For better performance, iterators should meet 13 | // the RandomAccessIterator requirement. 14 | template ::value_type, 16 | typename Compare = std::less> 17 | IterT bsearch(IterT first, 18 | IterT last, 19 | ValueT target, 20 | Compare comp = Compare()) { 21 | IterT result = last; 22 | while (std::distance(first, last) > 0) { 23 | IterT mid = first + std::distance(first, last) / 2; 24 | if (comp(*mid, target)) { 25 | first = mid + 1; 26 | } else if (comp(target, *mid)) { 27 | last = mid; 28 | } else { // equal 29 | result = mid; 30 | break; 31 | } 32 | } 33 | return result; 34 | } 35 | 36 | #endif // BSEARCH_BSEARCH_HPP_ 37 | 38 | -------------------------------------------------------------------------------- /c-cpp/15_bsearch/bsearch_c/bsearch.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: bsearch.c 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-21 6 | > Desc: 7 | ************************************************************************/ 8 | #include 9 | #include 10 | #include 11 | 12 | 13 | int mybsearch(int a[],int size,int value) 14 | { 15 | int mid = 0; 16 | int left = 0; 17 | int right = size - 1; 18 | 19 | while(left <= right) 20 | { 21 | /*防止size数量太大是,(left + right)数据翻转,导致问题*/ 22 | mid = left + ((right - left)>>1); 23 | 24 | if (a[mid] == value) 25 | { 26 | return mid; 27 | } 28 | else if (a[mid] < value) 29 | { 30 | left = mid + 1; 31 | } 32 | else 33 | { 34 | right = mid - 1; 35 | } 36 | } 37 | 38 | return -1; 39 | } 40 | 41 | int helper(int a[], int left,int right,int value) 42 | { 43 | int mid = 0; 44 | 45 | if (left > right) 46 | { 47 | return -1; 48 | } 49 | /*防止size数量太大是,(left + right)数据翻转,导致问题*/ 50 | mid = left + ((right - left)>>1); 51 | if (a[mid] == value) 52 | { 53 | return mid; 54 | } 55 | else if (a[mid] < value) 56 | { 57 | return helper(a,mid + 1,right,value); 58 | } 59 | else 60 | { 61 | return helper(a,left,mid - 1,value); 62 | } 63 | return -1; 64 | } 65 | /*递归实现*/ 66 | int mybsearch_2(int a[],int size,int value) 67 | { 68 | 69 | return helper(a,0,size-1,value); 70 | } 71 | 72 | int main() 73 | { 74 | int a[10] = {5,6,8,9,10,11,23,42,53,123}; 75 | int data = 0; 76 | int res = 0; 77 | 78 | printf("\r\n输入一个整数"); 79 | scanf("%d",&data); 80 | res = mybsearch(a,10,data); 81 | printf("data[%d] %s 在数据中,下标是%d",data,(res != -1)?"":"不",res); 82 | 83 | printf("\r\n输入一个整数"); 84 | scanf("%d",&data); 85 | res = mybsearch_2(a,10,data); 86 | printf("data[%d] %s 在数据中,下标是%d",data,(res != -1)?"":"不",res); 87 | return; 88 | } 89 | -------------------------------------------------------------------------------- /c-cpp/15_bsearch/bsearch_c/sqrt.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: sqrt.c 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-31 6 | > Desc: 7 | ************************************************************************/ 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | /*求解精度设置*/ 15 | #define E 0.000001 16 | double mybsearch(double num) 17 | { 18 | double start = 1.0; 19 | double end = num; 20 | double mid = 0.0; 21 | while(1) 22 | { 23 | mid = (start + end)/2; 24 | if(((mid*mid - num) <= E) && ((mid*mid - num) >= -E)) 25 | { 26 | return mid; 27 | } 28 | 29 | if ((mid*mid - num) > E) 30 | { 31 | end = mid; 32 | } 33 | else 34 | { 35 | start = mid; 36 | } 37 | } 38 | 39 | return 0; 40 | } 41 | 42 | 43 | int main() 44 | { 45 | double num = 0.0; 46 | 47 | /*这里需要注意:double的输入方式*/ 48 | scanf("%lf",&num); 49 | printf("\r\n num %lf的平方根是%lf",num,mybsearch(num)); 50 | 51 | return 0; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /c-cpp/15_bsearch/bsearch_test.cc: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/24. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | #include "bsearch.hpp" 9 | 10 | template 11 | void test_bsearch(const VecT& test, T target) { 12 | auto it = bsearch(test.begin(), test.end(), target); 13 | std::cout << std::distance(test.begin(), it) << std::endl; 14 | } 15 | 16 | int main() { 17 | std::vector test{0, 0, 1, 2, 3, 4, 4, 5, 5, 5, 5, 5, 6, 7}; // std::less() 18 | 19 | test_bsearch(test, 8); // 14 20 | test_bsearch(test, -1); // 14 21 | test_bsearch(test, 0); // 0, 1 22 | test_bsearch(test, 4); // 5, 6 23 | test_bsearch(test, 5); // 7, 8, 9, 10, 11 24 | test_bsearch(test, 7); // 13 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /c-cpp/16_bsearch/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/16_bsearch/.gitkeep -------------------------------------------------------------------------------- /c-cpp/16_bsearch/bsearch_varients.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/26. 3 | */ 4 | 5 | #ifndef BSEARCH_BSEARCH_VARIENTS_HPP_ 6 | #define BSEARCH_BSEARCH_VARIENTS_HPP_ 7 | 8 | #include 9 | #include 10 | 11 | enum class BsearchPolicy { UNSPECIFIED, FIRST, LAST, FIRST_NOT_LESS, LAST_NOT_GREATER }; 12 | 13 | // Liam Huang: The algorithm works right with iterators that meet the ForwardIterator requirement, 14 | // but with a bad time complexity. For better performance, iterators should meet 15 | // the RandomAccessIterator requirement. 16 | template ::value_type, 18 | typename Compare> 19 | IterT bsearch(IterT first, 20 | IterT last, 21 | ValueT target, 22 | Compare comp, 23 | BsearchPolicy policy = BsearchPolicy::UNSPECIFIED) { 24 | IterT result = last; 25 | while (std::distance(first, last) > 0) { 26 | IterT mid = first + std::distance(first, last) / 2; 27 | if (policy == BsearchPolicy::FIRST_NOT_LESS) { 28 | if (!comp(*mid, target)) { 29 | if (mid == first or comp(*(mid - 1), target)) { 30 | result = mid; 31 | break; 32 | } else { 33 | last = mid; 34 | } 35 | } else { 36 | first = mid + 1; 37 | } 38 | } else if (policy == BsearchPolicy::LAST_NOT_GREATER) { 39 | if (comp(target, *mid)) { 40 | last = mid; 41 | } else { 42 | if (std::distance(mid, last) == 1 or comp(target, *(mid + 1))) { 43 | result = mid; 44 | break; 45 | } else { 46 | first = mid + 1; 47 | } 48 | } 49 | } else { // policy == UNSPECIFIED or FIRST or LAST 50 | if (comp(*mid, target)) { 51 | first = mid + 1; 52 | } else if (comp(target, *mid)) { 53 | last = mid; 54 | } else { // equal 55 | if (policy == BsearchPolicy::FIRST) { 56 | if (mid == first or comp(*(mid - 1), *mid)) { 57 | result = mid; 58 | break; 59 | } else { 60 | last = mid; 61 | } 62 | } else if (policy == BsearchPolicy::LAST) { 63 | if (std::distance(mid, last) == 1 or comp(*mid, *(mid + 1))) { 64 | result = mid; 65 | break; 66 | } else { 67 | first = mid + 1; 68 | } 69 | } else { 70 | result = mid; 71 | break; 72 | } 73 | } 74 | } 75 | } 76 | return result; 77 | } 78 | 79 | template ::value_type, 81 | typename Compare = std::less> 82 | IterT bsearch(IterT first, 83 | IterT last, 84 | ValueT target, 85 | BsearchPolicy policy = BsearchPolicy::UNSPECIFIED) { 86 | return bsearch(first, last, target, Compare(), policy); 87 | } 88 | 89 | #endif // BSEARCH_BSEARCH_VARIENTS_HPP_ 90 | 91 | -------------------------------------------------------------------------------- /c-cpp/16_bsearch/bsearch_varients_test.cc: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/26. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | #include "bsearch_varients.hpp" 9 | 10 | template 11 | void test_bsearch(const VecT& test, 12 | T target, 13 | BsearchPolicy policy = BsearchPolicy::UNSPECIFIED) { 14 | auto it = bsearch(test.begin(), test.end(), target, policy); 15 | std::cout << std::distance(test.begin(), it) << std::endl; 16 | } 17 | 18 | int main() { 19 | std::vector test{0, 0, 1, 2, 3, 4, 4, 5, 5, 5, 5, 5, 6, 8}; // std::less() 20 | 21 | test_bsearch(test, 8); // 14 22 | test_bsearch(test, -1); // 14 23 | test_bsearch(test, 0); // 0, 1 24 | test_bsearch(test, 0, BsearchPolicy::FIRST); // 0 25 | test_bsearch(test, 0, BsearchPolicy::LAST); // 1 26 | test_bsearch(test, 4); // 5, 6 27 | test_bsearch(test, 4, BsearchPolicy::FIRST); // 5 28 | test_bsearch(test, 4, BsearchPolicy::LAST); // 6 29 | test_bsearch(test, 5); // 7, 8, 9, 10, 11 30 | test_bsearch(test, 5, BsearchPolicy::FIRST); // 7 31 | test_bsearch(test, 5, BsearchPolicy::LAST); // 11 32 | test_bsearch(test, 7, BsearchPolicy::FIRST_NOT_LESS); // 13 33 | test_bsearch(test, 7, BsearchPolicy::LAST_NOT_GREATER); // 12 34 | test_bsearch(test, 7, BsearchPolicy::FIRST); // 14 35 | test_bsearch(test, 8); // 13 36 | test_bsearch(test, 8, BsearchPolicy::FIRST); // 13 37 | test_bsearch(test, 8, BsearchPolicy::LAST); // 13 38 | 39 | return 0; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /c-cpp/17_skiplist/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/17_skiplist/.gitkeep -------------------------------------------------------------------------------- /c-cpp/17_skiplist/SkipList.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/17_skiplist/SkipList.cpp -------------------------------------------------------------------------------- /c-cpp/17_skiplist/skiplist_c/skiplist.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: skiplist.h 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-31 6 | > Desc: 7 | ************************************************************************/ 8 | #ifndef __SKIP_LIST_H__ 9 | #define __SKIP_LIST_H__ 10 | 11 | 12 | typedef struct _node 13 | { 14 | int key; /*key是唯一的*/ 15 | int value; /*存储的内容*/ 16 | int max_level; /*当前节点最大层数*/ 17 | struct _node *next[0];/*level层链表结构*/ 18 | }node; 19 | 20 | typedef struct _skiplist 21 | { 22 | int level; 23 | int count; 24 | node *head; 25 | }skiplist; 26 | 27 | /*根据当前结构体元素的地址,获取到结构体首地址*/ 28 | #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 29 | #define container(ptr,type,member) ({\ 30 | const typeof( ((type *)0)->member) *__mptr = (ptr);\ 31 | (type *) ( (char *)__mptr - offsetof(type,member));}) 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /c-cpp/17_skiplist/skiplist_test.cc: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/30. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | #include "skiplist.hpp" 9 | 10 | int main() { 11 | // 1. Initialize a skip list for test 12 | // * default constructor 13 | // * constructor with initializer list 14 | // * insert 15 | skiplist ss{"1", "2", "3", "4", "5"}; 16 | 17 | // 1a. show 18 | // * print 19 | ss.print(std::cout); 20 | std::cout << std::endl; 21 | 22 | // 2. move construction 23 | // * move constructor 24 | skiplist s(std::move(ss)); 25 | 26 | // 2a. show 27 | // * print 28 | s.print(std::cout); 29 | std::cout << std::endl; 30 | 31 | // 3.a find something doesn't exist. 32 | // * find 33 | auto f = s.find("0"); 34 | if (!f.empty()) { 35 | std::cout << "Node found!\nvalue: " << f << '\n'; 36 | } else { 37 | std::cout << "Node NOT found!\n"; 38 | } 39 | 40 | // 3.b find something does exist. 41 | // * find 42 | auto ff = s.find("1"); 43 | if (!ff.empty()) { 44 | std::cout << "Node found!\tvalue: " << ff << '\n'; 45 | } else { 46 | std::cout << "Node NOT found!\n"; 47 | } 48 | 49 | // 4. insert() - reassign 50 | s.insert("TEST"); 51 | 52 | // 4a. print() 53 | s.print(std::cout); 54 | std::cout << std::endl; 55 | 56 | // 5. erase() 57 | s.erase("TEST"); 58 | 59 | // 5a. print(); 60 | s.print(std::cout); 61 | std::cout << std::endl; 62 | 63 | std::cout << "\nDone!\n"; 64 | 65 | return 0; 66 | // 6. destructor 67 | } 68 | -------------------------------------------------------------------------------- /c-cpp/17_skiplist/skiplist_tr_test.cc: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/10/30. 3 | */ 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #include "skiplist_tr.hpp" 11 | 12 | int main() { 13 | // 1. UT for skiplist_detail::random_level 14 | skiplist_detail::random_level rl(5, 0.5); 15 | std::map hist; 16 | for (size_t i = 0; i != 10000; ++i) { 17 | ++hist[rl()]; 18 | } 19 | for (auto p : hist) { 20 | std::cout << p.first << ' ' << std::string(p.second / 100, '*') << '\n'; 21 | } 22 | 23 | // 2. UT for skiplist(), init_internally(), size(), empty() 24 | skiplist sl_default; 25 | assert(sl_default.empty()); 26 | // 2.1. UT for grow with abandon 27 | sl_default.grow(1); 28 | assert(sl_default.capability() == 4); 29 | // 2.2. UT for grow 30 | sl_default.grow(10); 31 | assert(sl_default.capability() == 1024); 32 | 33 | // 3. UT for constructor of initializer_list and InputIt, init_internally(), insert(), 34 | // find_predecessors() 35 | skiplist sl{"hello", "world", "!"}; 36 | assert(not sl.empty()); 37 | assert(3 == sl.size()); 38 | 39 | // 4. UT for find() find_helper() 40 | auto search = sl_default.find("nonexist"); 41 | assert(search == sl_default.cend()); 42 | assert(search == sl_default.end()); 43 | search = sl.find("hello"); 44 | assert(search != sl.cend()); 45 | assert(search != sl.end()); 46 | search = sl.find("nonexist"); 47 | assert(search == sl.cend()); 48 | assert(search == sl.end()); 49 | 50 | // 5. UT for insert(), find_predecessors() 51 | // 5.1. UT for insert a already-exist item 52 | sl.insert("hello"); 53 | search = sl.find("hello"); 54 | assert(search != sl.cend()); 55 | assert(search != sl.end()); 56 | // 5.2. UT for insert a new incoming item 57 | search = sl.find("now exist"); 58 | assert(search == sl.cend()); 59 | assert(search == sl.end()); 60 | sl.insert("now exist"); 61 | search = sl.find("now exist"); 62 | assert(search != sl.cend()); 63 | assert(search != sl.end()); 64 | 65 | // 6. UT for erase(), find_predecessors() 66 | sl.insert("hello"); 67 | sl.insert("hello"); 68 | sl.insert("hello"); 69 | sl.insert("hello"); 70 | // 6.1. UT for erase single item 71 | sl.erase("hello", erase_policy::SINGLE); 72 | search = sl.find("hello"); 73 | assert(search != sl.cend()); 74 | assert(search != sl.end()); 75 | // 6.2. UT for erase all items 76 | sl.erase("hello", erase_policy::ALL); 77 | search = sl.find("hello"); 78 | assert(search == sl.cend()); 79 | assert(search == sl.end()); 80 | // 6.3 UT for erase non-exist item 81 | sl.erase("nonexist"); 82 | 83 | // 7. UT for insert() behind erase() 84 | // 7.1. different word 85 | sl.insert("world"); 86 | search = sl.find("world"); 87 | assert(search != sl.cend()); 88 | assert(search != sl.end()); 89 | assert(search->values.count("world") == 2); 90 | // 7.1. same word, also UT for grow() 91 | search = sl.find("hello"); 92 | assert(search == sl.cend()); 93 | assert(search == sl.end()); 94 | sl.insert("hello"); 95 | sl.insert("hello"); 96 | sl.insert("hello"); 97 | sl.insert("hello"); 98 | sl.insert("hello"); 99 | search = sl.find("hello"); 100 | assert(search != sl.cend()); 101 | assert(search != sl.end()); 102 | assert(search->values.count("hello") == 5); 103 | 104 | return 0; 105 | // 8. UT for ~skiplist() 106 | } 107 | 108 | -------------------------------------------------------------------------------- /c-cpp/18_hashtable/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/c-cpp/18_hashtable/.gitkeep -------------------------------------------------------------------------------- /c-cpp/18_hashtable/hash_map.cc: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Liam Huang (Liam0205) on 2018/08/14. 3 | * This is an old test file for hash_map, created by Liam. 4 | * Just for showing the inner logic for hash_map class template. 5 | * Original posted on: 6 | * https://github.com/Liam0205/leetcode/tree/master/met/hash_map.cc 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | template > 16 | class hash_map { 17 | public: 18 | using key_type = Key; 19 | using mapped_type = T; 20 | using value_type = std::pair; 21 | using size_type = size_t; 22 | using hasher = std::hash; 23 | 24 | private: // helper 25 | using wrapper = std::shared_ptr; 26 | 27 | public: // constructor 28 | hash_map() { 29 | container_.resize(primes_[size_level_]); 30 | } 31 | 32 | public: // capacity 33 | bool empty() const { return empty_; } 34 | size_type size() const { return size_; } 35 | size_type max_size() const { return primes_[size_level_]; } 36 | 37 | public: // find and modify 38 | mapped_type& operator[](const key_type& key) { 39 | auto hashed = find_hash(key); 40 | if (not(container_[hashed]) and construct_new_on_position(hashed, key) and 41 | load_factor() > max_load_factor()) { 42 | expand(); 43 | } 44 | return container_[hashed]->second; 45 | } 46 | 47 | public: // hash policy 48 | double load_factor() const { return static_cast(size()) / max_size(); } 49 | double max_load_factor() const { return max_load_factor_; } 50 | void expand() const { 51 | ++size_level_; 52 | std::vector temp; 53 | temp.resize(primes_[size_level_]); 54 | for (auto w : container_) { 55 | if (nullptr != w) { 56 | auto hashed = find_hash(w->first); 57 | temp[hashed] = w; 58 | } 59 | } 60 | container_ = std::move(temp); 61 | } 62 | 63 | private: // helper functions 64 | size_type find_hash(const key_type& key) const { 65 | const size_t csz = container_.size(); 66 | size_t count = 0; 67 | size_t hashed = hasher_(key) % csz; 68 | while (nullptr != container_[hashed] and container_[hashed]->first != key) { 69 | hashed = (hashed + ++count) % csz; 70 | } 71 | return hashed; 72 | } 73 | bool construct_new_on_position(const size_type pos, const key_type& key) { 74 | empty_ = false; 75 | ++size_; 76 | container_[pos] = std::make_shared(std::make_pair(key, mapped_type())); 77 | return true; 78 | } 79 | 80 | private: 81 | const hasher hasher_ = hasher(); 82 | mutable size_t size_level_ = 0; 83 | mutable std::vector container_; 84 | static const size_t primes_[]; 85 | bool empty_ = true; 86 | size_type size_ = 0; 87 | double max_load_factor_ = 0.75; 88 | }; 89 | template 90 | const size_t hash_map::primes_[] = {7, 17, 29, 53, 101, 211, 401, 809, 1601, 3203}; // ... 91 | 92 | int main() { 93 | hash_map test; 94 | test[1]; 95 | test[2] = 2; 96 | std::cout << test[1] << ' ' << test[2] << std::endl; 97 | return 0; 98 | } 99 | -------------------------------------------------------------------------------- /c-cpp/18_hashtable/listhash/listhash.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: listhash.h 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-11-06 6 | > Desc: 根据linux内核模块hashtab编写用户层hashtab接口 7 | linux-4.19.1\security\selinux\ss\hashtab.c 8 | linux-4.19.1\security\selinux\ss\hashtab.h 9 | ************************************************************************/ 10 | 11 | 12 | #ifndef __HASHTAB_H__ 13 | #define __HASHTAB_H__ 14 | 15 | 16 | typedef struct _hashtab_node 17 | { 18 | void * key; 19 | void * data; 20 | struct _hashtab_node *next; 21 | }hashtab_node; 22 | 23 | typedef struct _hashtab 24 | { 25 | hashtab_node **htables; /*哈希桶*/ 26 | int size; /*哈希桶的最大数量*/ 27 | int nel; /*哈希桶中元素的个数*/ 28 | int (*hash_value)(struct _hashtab *h,const void *key); /*哈希函数*/ 29 | int (*keycmp)(struct _hashtab *h,const void *key1,const void *key2);/*哈希key比较函数,当哈希数值一致时使用*/ 30 | void (*hash_node_free)(hashtab_node *node); 31 | }hashtab; 32 | 33 | 34 | #define HASHTAB_MAX_NODES (0xffffffff) 35 | 36 | typedef int (*hash_key_func)(struct _hashtab *h,const void *key); /*哈希函数*/ 37 | typedef int (*keycmp_func)(struct _hashtab *h,const void *key1,const void *key2);/*哈希key比较函数,当哈希数值一致时使用*/ 38 | typedef void (*hash_node_free_func)(hashtab_node *node); 39 | /*根据当前结构体元素的地址,获取到结构体首地址*/ 40 | #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 41 | #define container(ptr,type,member) ({\ 42 | const typeof( ((type *)0)->member) *__mptr = (ptr);\ 43 | (type *) ( (char *)__mptr - offsetof(type,member));}) 44 | 45 | 46 | hashtab * hashtab_create(int size,hash_key_func hash_value, 47 | keycmp_func keycmp,hash_node_free_func hash_node_free); 48 | void hashtab_destory(hashtab *h); 49 | int hashtab_insert(hashtab * h,void *key,void *data); 50 | hashtab_node *hashtab_delete(hashtab *h, void *key); 51 | void *hashtab_search(hashtab*h,void *key); 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /c-cpp/19_Dlisthash/Dlist.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: Dlist.h 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-11-08 6 | > Desc: linux内核源码双向链表实现include/linux/list.h 7 | ************************************************************************/ 8 | #ifndef _LIST_HEAD_H 9 | #define _LIST_HEAD_H 10 | 11 | // 双向链表节点 12 | struct list_head { 13 | struct list_head *next, *prev; 14 | }; 15 | 16 | // 初始化节点:设置name节点的前继节点和后继节点都是指向name本身。 17 | #define LIST_HEAD_INIT(name) { &(name), &(name) } 18 | 19 | // 定义表头(节点):新建双向链表表头name,并设置name的前继节点和后继节点都是指向name本身。 20 | #define LIST_HEAD(name) \ 21 | struct list_head name = LIST_HEAD_INIT(name) 22 | 23 | // 初始化节点:将list节点的前继节点和后继节点都是指向list本身。 24 | static inline void INIT_LIST_HEAD(struct list_head *list) 25 | { 26 | list->next = list; 27 | list->prev = list; 28 | } 29 | 30 | // 添加节点:将new插入到prev和next之间。 31 | static inline void __list_add(struct list_head *new, 32 | struct list_head *prev, 33 | struct list_head *next) 34 | { 35 | next->prev = new; 36 | new->next = next; 37 | new->prev = prev; 38 | prev->next = new; 39 | } 40 | 41 | // 添加new节点:将new添加到head之后,是new称为head的后继节点。 42 | static inline void list_add(struct list_head *new, struct list_head *head) 43 | { 44 | __list_add(new, head, head->next); 45 | } 46 | 47 | // 添加new节点:将new添加到head之前,即将new添加到双链表的末尾。 48 | static inline void list_add_tail(struct list_head *new, struct list_head *head) 49 | { 50 | __list_add(new, head->prev, head); 51 | } 52 | 53 | // 从双链表中删除entry节点。 54 | static inline void __list_del(struct list_head * prev, struct list_head * next) 55 | { 56 | next->prev = prev; 57 | prev->next = next; 58 | } 59 | 60 | // 从双链表中删除entry节点。 61 | static inline void list_del(struct list_head *entry) 62 | { 63 | __list_del(entry->prev, entry->next); 64 | } 65 | 66 | // 从双链表中删除entry节点。 67 | static inline void __list_del_entry(struct list_head *entry) 68 | { 69 | __list_del(entry->prev, entry->next); 70 | } 71 | 72 | // 从双链表中删除entry节点,并将entry节点的前继节点和后继节点都指向entry本身。 73 | static inline void list_del_init(struct list_head *entry) 74 | { 75 | __list_del_entry(entry); 76 | INIT_LIST_HEAD(entry); 77 | } 78 | 79 | // 用new节点取代old节点 80 | static inline void list_replace(struct list_head *old, 81 | struct list_head *new) 82 | { 83 | new->next = old->next; 84 | new->next->prev = new; 85 | new->prev = old->prev; 86 | new->prev->next = new; 87 | } 88 | 89 | // 双链表是否为空 90 | static inline int list_empty(const struct list_head *head) 91 | { 92 | return head->next == head; 93 | } 94 | 95 | // 获取"MEMBER成员"在"结构体TYPE"中的位置偏移 96 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 97 | 98 | // 根据"结构体(type)变量"中的"域成员变量(member)的指针(ptr)"来获取指向整个结构体变量的指针 99 | #define container_of(ptr, type, member) ({ \ 100 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 101 | (type *)( (char *)__mptr - offsetof(type,member) );}) 102 | 103 | // 遍历双向链表 104 | #define list_for_each(pos, head) \ 105 | for (pos = (head)->next; pos != (head); pos = pos->next) 106 | 107 | #define list_for_each_safe(pos, n, head) \ 108 | for (pos = (head)->next, n = pos->next; pos != (head); \ 109 | pos = n, n = pos->next) 110 | 111 | #define list_entry(ptr, type, member) \ 112 | container_of(ptr, type, member) 113 | 114 | #endif 115 | -------------------------------------------------------------------------------- /c-cpp/19_Dlisthash/LinkedHashMap.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: LinkedHashMap.h 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-11-08 6 | > Desc: 7 | ************************************************************************/ 8 | 9 | 10 | #ifndef __LINKED_HASH_MAP__ 11 | #define __LINKED_HASH_MAP__ 12 | 13 | /*数据存放节点*/ 14 | typedef struct _lisked_hash_map_node 15 | { 16 | void *key; /*键*/ 17 | void *data; /*数据*/ZZ 18 | struct _lisked_hash_map_node *next; /*哈希冲突时,用来挂接后续节点*/ 19 | struct list_head Dlist_node;/*用来挂接双向链表*/ 20 | }LiskedHashMapNode; 21 | 22 | typedef struct _lisked_hash_map 23 | { 24 | LiskedHashMapNode **hTabs;/*哈希桶*/ 25 | struct list_head header;/*双向循环链表头*/ 26 | int size; /**/ 27 | int nel_max; /*支持最大节点数*/ 28 | int nel; /*当前节点数*/ 29 | int (*hash_value)(struct _lisked_hash_map *h,const void *key); /*哈希函数*/ 30 | int (*keycmp)(struct _lisked_hash_map *h,const void *key1,const void *key2);/*哈希key比较函数,当哈希数值一致时使用*/ 31 | void (*hash_node_free)(LiskedHashMapNode *node,int flg);/*用来释放节点内存*/ 32 | 33 | }LinkedHashMap; 34 | 35 | typedef int (*hash_value_func)(struct _lisked_hash_map *h,const void *key); /*哈希函数*/ 36 | typedef int (*keycmp_func)(struct _lisked_hash_map *h,const void *key1,const void *key2);/*哈希key比较函数,当哈希数值一致时使用*/ 37 | typedef void (*hash_node_free_func)(LiskedHashMapNode *node,int flg); 38 | 39 | LiskedHashMapNode * LinkedHashMap_delete(LinkedHashMap *h,void *key); 40 | #endif 41 | -------------------------------------------------------------------------------- /c-cpp/23_binarytree/binarytree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | /* Implement binary tree in array */ 8 | 9 | #define MAX_TREE_NODES (1 << 8) 10 | 11 | struct node { 12 | int data; 13 | }; 14 | 15 | struct binary_tree { 16 | union { 17 | unsigned long nodes; 18 | struct node *n[MAX_TREE_NODES]; 19 | }; 20 | }; 21 | 22 | void init_binary_tree(struct binary_tree *tree) 23 | { 24 | int i; 25 | 26 | for(i = 0; i < MAX_TREE_NODES; i++) { 27 | tree->n[i] = NULL; 28 | } 29 | } 30 | 31 | struct node* create_node(int data) 32 | { 33 | struct node* n; 34 | 35 | n = malloc(sizeof(struct node)); 36 | 37 | if (n) 38 | n->data = data; 39 | 40 | return n; 41 | } 42 | 43 | void fake_a_tree(struct binary_tree* tree) 44 | { 45 | /* data is in ordered */ 46 | int i, data[10] = {7, 4, 9, 2, 6, 8, 10, 1, 3, 5}; 47 | 48 | init_binary_tree(tree); 49 | 50 | /* root start at 1 */ 51 | for (i = 0; i < 10; i++) 52 | tree->n[i+1] = create_node(data[i]); 53 | 54 | tree->nodes = 10; 55 | } 56 | 57 | void _in_order(struct binary_tree* tree, int index) 58 | { 59 | if (!tree->n[index]) 60 | return; 61 | 62 | /* left child at (index << 1) */ 63 | _in_order(tree, index << 1); 64 | 65 | printf("[%2d]: %4d\n", index, tree->n[index]->data); 66 | 67 | /* right child at (index << 1) + 1 */ 68 | _in_order(tree, (index << 1) + 1); 69 | } 70 | 71 | void in_order(struct binary_tree* tree) 72 | { 73 | _in_order(tree, 1); 74 | } 75 | 76 | int main() 77 | { 78 | struct binary_tree tree; 79 | 80 | fake_a_tree(&tree); 81 | in_order(&tree); 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /c-cpp/23_binarytree/tree/list_queue.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: list_queue.c 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-13 6 | > Desc: 7 | ************************************************************************/ 8 | #include 9 | #include 10 | #include 11 | #include"./list_queue.h" 12 | 13 | /*创建队列头*/ 14 | list_queue *list_queue_create() 15 | { 16 | list_queue * queue = NULL; 17 | 18 | queue = (list_queue *)malloc(sizeof(list_queue)); 19 | if(queue == NULL) 20 | { 21 | return NULL; 22 | } 23 | 24 | queue->num = 0; 25 | queue->head = NULL; 26 | queue->tail = NULL; 27 | 28 | return queue; 29 | } 30 | int list_queue_enqueue(list_queue *queue,void *data) 31 | { 32 | queue_node *ptmp = NULL; 33 | 34 | if(queue == NULL) 35 | { 36 | return -1; 37 | } 38 | 39 | ptmp = (queue_node *)malloc(sizeof(queue_node)); 40 | if (ptmp == NULL) 41 | { 42 | return -1; 43 | } 44 | 45 | ptmp->data = data; 46 | ptmp->next = NULL; 47 | if (queue->head == NULL) 48 | { 49 | queue->head = ptmp; 50 | } 51 | else 52 | { 53 | queue->tail->next = ptmp; 54 | 55 | } 56 | queue->tail = ptmp; 57 | queue->num++; 58 | 59 | return 0; 60 | } 61 | 62 | /*出队*/ 63 | int list_queue_dequeue(list_queue *queue,void **data) 64 | { 65 | queue_node * ptmp = NULL; 66 | 67 | if ((queue == NULL) || (data == NULL) || list_queue_is_empty(queue)) 68 | { 69 | return -1; 70 | } 71 | 72 | *data = queue->head->data; 73 | ptmp = queue->head; 74 | queue->head = queue->head->next; 75 | queue->num--; 76 | 77 | if (queue->head == NULL) 78 | { 79 | queue->tail = NULL; 80 | } 81 | 82 | 83 | free(ptmp); 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /c-cpp/23_binarytree/tree/list_queue.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: list_queue.h 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-10-13 6 | > Desc: 7 | ************************************************************************/ 8 | 9 | #ifndef LINK_LIST_QUEUE_H 10 | #define LINK_LIST_QUEUE_H 11 | 12 | typedef struct _list_queue_node 13 | { 14 | void *data; 15 | struct _list_queue_node *next; 16 | }queue_node; 17 | 18 | typedef struct _list_queue 19 | { 20 | int num; 21 | queue_node *head; 22 | queue_node *tail; 23 | }list_queue; 24 | 25 | #define list_queue_is_empty(queue) ((queue->num) == 0) 26 | list_queue *list_queue_create(); 27 | int list_queue_enqueue(list_queue *queue,void *data); 28 | int list_queue_dequeue(list_queue *queue,void **data); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /c-cpp/24_binarysearchtree/binarysearchtree.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: binarysearchtree.h 3 | > Author: jinshaohui 4 | > Mail: jinshaohui789@163.com 5 | > Time: 18-11-12 6 | > Desc: 7 | ************************************************************************/ 8 | #ifndef __BINARY_SEARCH_TREE__ 9 | #define __BINARY_SEARCH_TREE__ 10 | typedef int mytype; 11 | 12 | typedef struct _bstree_node 13 | { 14 | mytype data; 15 | struct _bstree_node *lchild; 16 | struct _bstree_node *rchild; 17 | }bstree_node; 18 | 19 | typedef struct _bstree 20 | { 21 | int size; 22 | int (*compare)(mytype key1,mytype key2); 23 | int (*destory)(mytype data); 24 | bstree_node *root; 25 | }bstree; 26 | 27 | typedef int (*compare_fuc)(mytype key1,mytype key2); 28 | typedef int (*destory_fuc)(mytype data); 29 | 30 | #define bstree_is_empty(tree) (tree->size == 0) 31 | 32 | bstree *bstree_create(compare_fuc compare,destory_fuc destory); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /c-cpp/28_heap/heap.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | /* Implement heap */ 8 | 9 | #define MAX_HEAP_SIZE (1 << 8) 10 | 11 | struct element { 12 | int data; 13 | }; 14 | 15 | struct heap { 16 | union { 17 | unsigned long elements; 18 | struct element *elem[MAX_HEAP_SIZE]; 19 | }; 20 | }; 21 | 22 | void init_heap(struct heap *heap) 23 | { 24 | int i; 25 | 26 | for(i = 0; i < MAX_HEAP_SIZE; i++) { 27 | heap->elem[i] = NULL; 28 | } 29 | } 30 | 31 | void dump_heap(struct heap *heap, int index) 32 | { 33 | struct element *elem; 34 | int level; 35 | 36 | if (index > heap->elements) 37 | return; 38 | 39 | elem = heap->elem[index]; 40 | level = fls(index); 41 | 42 | dump_heap(heap, index * 2 + 1); 43 | 44 | if (!(index % 2) && index != 1) 45 | printf("%*s\n", level*3, "|"); 46 | 47 | printf("%*s - %05d\n", level*3, " ", elem->data); 48 | 49 | if (index % 2 && index != 1) 50 | printf("%*s\n", level*3, "|"); 51 | 52 | dump_heap(heap, index * 2); 53 | } 54 | 55 | void dump(struct heap *heap, int elements) 56 | { 57 | int i; 58 | 59 | for (i = 1; i <= elements; i++) 60 | printf("[%02d]: %4d\n", i, heap->elem[i]->data); 61 | 62 | } 63 | 64 | struct element* create_element(int data) 65 | { 66 | struct element *elem; 67 | 68 | elem = malloc(sizeof(struct element)); 69 | 70 | if (elem) 71 | elem->data = data; 72 | 73 | return elem; 74 | } 75 | 76 | void fake_a_heap(struct heap *heap) 77 | { 78 | /* data is in ordered */ 79 | int i, data[10] = {7, 4, 9, 2, 6, 8, 10, 1, 3, 5}; 80 | 81 | init_heap(heap); 82 | 83 | /* root start at 1 */ 84 | for (i = 0; i < 10; i++) 85 | heap->elem[i+1] = create_element(data[i]); 86 | 87 | heap->elements = 10; 88 | } 89 | 90 | void swap(struct heap *heap, int i, int j) 91 | { 92 | struct element *tmp; 93 | 94 | tmp = heap->elem[j]; 95 | heap->elem[j] = heap->elem[i]; 96 | heap->elem[i] = tmp; 97 | } 98 | 99 | void heapify(struct heap *heap, int parent) 100 | { 101 | struct element **elem = heap->elem; 102 | int elements = heap->elements; 103 | int left, right, max; 104 | 105 | while (true) { 106 | left = parent * 2; 107 | right = left + 1; 108 | 109 | max = parent; 110 | if (left <= elements && elem[max]->data < elem[left]->data) 111 | max = left; 112 | if (right <= elements && elem[max]->data < elem[right]->data) 113 | max = right; 114 | 115 | if (max == parent) 116 | break; 117 | 118 | swap(heap, max, parent); 119 | parent = max; 120 | } 121 | } 122 | 123 | void build_heap(struct heap *heap) 124 | { 125 | int i; 126 | 127 | for (i = heap->elements / 2; i >= 1; i--) 128 | heapify(heap, i); 129 | } 130 | 131 | int heap_sort(struct heap *heap) 132 | { 133 | int elements = heap->elements; 134 | 135 | while (heap->elements) { 136 | swap(heap, 1, heap->elements); 137 | heap->elements--; 138 | heapify(heap, 1); 139 | } 140 | 141 | return elements; 142 | } 143 | 144 | int main() 145 | { 146 | struct heap heap; 147 | int elements; 148 | 149 | fake_a_heap(&heap); 150 | dump_heap(&heap, 1); 151 | 152 | printf("After Heapify:\n"); 153 | build_heap(&heap); 154 | dump_heap(&heap, 1); 155 | 156 | printf("After Heap sort:\n"); 157 | elements = heap_sort(&heap); 158 | dump(&heap, elements); 159 | return 0; 160 | } 161 | -------------------------------------------------------------------------------- /c-cpp/30_Graph/graph.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | struct vertex; 8 | struct vertex_adjs { 9 | struct vertex *v; 10 | struct vertex_adjs *next; 11 | }; 12 | 13 | struct vertex { 14 | int data; 15 | struct vertex_adjs *adj; 16 | }; 17 | 18 | #define MAX_GRAPH_VERTEX (1 << 8) 19 | struct graph { 20 | struct vertex *vxs[MAX_GRAPH_VERTEX]; 21 | }; 22 | 23 | void init_graph(struct graph *graph) 24 | { 25 | int i; 26 | 27 | for (i = 0; i < MAX_GRAPH_VERTEX; i++) 28 | graph->vxs[i] = NULL; 29 | } 30 | 31 | struct vertex *create_vertex(int data) 32 | { 33 | struct vertex *v; 34 | 35 | v = malloc(sizeof(struct vertex)); 36 | 37 | if (v) { 38 | v->data = data; 39 | v->adj = NULL; 40 | } 41 | 42 | return v; 43 | } 44 | 45 | struct vertex_adjs *create_vertex_adj(struct vertex *v) 46 | { 47 | struct vertex_adjs *v_adj; 48 | 49 | v_adj = malloc(sizeof(struct vertex_adjs)); 50 | 51 | if (!v_adj) 52 | return NULL; 53 | 54 | v_adj->v = v; 55 | v_adj->next = NULL; 56 | return v_adj; 57 | } 58 | 59 | void insert_adj(struct vertex *v, struct vertex *adj) 60 | { 61 | struct vertex_adjs **v_adj; 62 | 63 | v_adj = &v->adj; 64 | 65 | while (*v_adj) 66 | v_adj = &(*v_adj)->next; 67 | 68 | *v_adj = create_vertex_adj(adj); 69 | } 70 | 71 | void dump_raw(struct graph *graph) 72 | { 73 | int i; 74 | 75 | for (i = 0; i < MAX_GRAPH_VERTEX; i++) { 76 | struct vertex *v = graph->vxs[i]; 77 | struct vertex_adjs *adj; 78 | if (v == NULL) 79 | continue; 80 | 81 | printf("Vertex[%02d]: %8d ->", i, v->data); 82 | 83 | adj = v->adj; 84 | while (adj) { 85 | printf(" %8d,", adj->v->data); 86 | adj = adj->next; 87 | } 88 | printf("\n"); 89 | } 90 | } 91 | 92 | /* 93 | 1 ----- 2 ----- 3 94 | | / | / 95 | | / | / 96 | | / | / 97 | | / | / 98 | | / | / 99 | 4 ----- 5 100 | */ 101 | void fake_a_graph(struct graph *graph) 102 | { 103 | int i; 104 | 105 | init_graph(graph); 106 | 107 | for (i = 0; i < 5; i++) 108 | graph->vxs[i] = create_vertex(i+1); 109 | 110 | /* connect 1 -> 2, 1 -> 4 */ 111 | insert_adj(graph->vxs[0], graph->vxs[1]); 112 | insert_adj(graph->vxs[0], graph->vxs[3]); 113 | /* connect 2 -> 1, 2 -> 3, 2 -> 5, 2 -> 4 */ 114 | insert_adj(graph->vxs[1], graph->vxs[0]); 115 | insert_adj(graph->vxs[1], graph->vxs[2]); 116 | insert_adj(graph->vxs[1], graph->vxs[4]); 117 | insert_adj(graph->vxs[1], graph->vxs[3]); 118 | /* connect 3 -> 2, 3 -> 5 */ 119 | insert_adj(graph->vxs[2], graph->vxs[1]); 120 | insert_adj(graph->vxs[2], graph->vxs[4]); 121 | /* connect 4 -> 1, 4 -> 2, 4 -> 5 */ 122 | insert_adj(graph->vxs[3], graph->vxs[0]); 123 | insert_adj(graph->vxs[3], graph->vxs[1]); 124 | insert_adj(graph->vxs[3], graph->vxs[4]); 125 | /* connect 5 -> 4, 5 -> 2, 5 -> 3 */ 126 | insert_adj(graph->vxs[4], graph->vxs[3]); 127 | insert_adj(graph->vxs[4], graph->vxs[1]); 128 | insert_adj(graph->vxs[4], graph->vxs[3]); 129 | } 130 | 131 | int main() 132 | { 133 | struct graph g; 134 | 135 | fake_a_graph(&g); 136 | dump_raw(&g); 137 | return 0; 138 | } 139 | -------------------------------------------------------------------------------- /c-cpp/bst.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | enum child_dir { 8 | left_child, 9 | right_child, 10 | root, 11 | }; 12 | 13 | struct node { 14 | unsigned long data; 15 | struct node *left; 16 | struct node *right; 17 | }; 18 | 19 | struct root { 20 | struct node *r; 21 | }; 22 | 23 | void dump(struct node *node, int level, enum child_dir dir) 24 | { 25 | if (!node) 26 | return; 27 | 28 | dump(node->right, level + 1, right_child); 29 | 30 | if (dir == left_child) 31 | printf("%*s\n", level*3, "|"); 32 | 33 | printf("%*s - %05lu\n", level*3, " ", node->data); 34 | 35 | if (dir == right_child) 36 | printf("%*s\n", level*3, "|"); 37 | 38 | dump(node->left, level + 1, left_child); 39 | } 40 | 41 | struct node* find(struct root *root, unsigned long data) 42 | { 43 | struct node* n = root->r; 44 | 45 | while (n) { 46 | if (n->data == data) 47 | return n; 48 | if (data < n->data) 49 | n = n->left; 50 | else 51 | n = n->right; 52 | } 53 | 54 | return NULL; 55 | } 56 | 57 | struct node* new_node(unsigned long data) 58 | { 59 | struct node *n; 60 | 61 | n = malloc(sizeof(struct node)); 62 | 63 | n->data = data; 64 | n->left = n->right = NULL; 65 | return n; 66 | } 67 | 68 | void insert(struct root *root, struct node *new) 69 | { 70 | struct node *parent; 71 | 72 | if (!root->r) { 73 | root->r = new; 74 | return; 75 | } 76 | 77 | parent = root->r; 78 | 79 | while (true) { 80 | /* Don't support duplicate data */ 81 | if (new->data == parent->data) 82 | break; 83 | 84 | if (new->data < parent->data) { 85 | if (!parent->left) { 86 | parent->left = new; 87 | break; 88 | } 89 | parent = parent->left; 90 | } else { 91 | if (!parent->right) { 92 | parent->right = new; 93 | break; 94 | } 95 | parent = parent->right; 96 | } 97 | } 98 | } 99 | 100 | struct node* delete(struct root *root, unsigned long data) 101 | { 102 | struct node *n = root->r, **p = &root->r; 103 | struct node *child; 104 | 105 | while (n && n->data != data) { 106 | if (data < n->data) { 107 | p = &n->left; 108 | n = n->left; 109 | } else { 110 | p = &n->right; 111 | n = n->right; 112 | } 113 | } 114 | 115 | if (!n) 116 | return NULL; 117 | 118 | if (n->left && n->right) { 119 | struct node *rn = n->right, **rp = &n->right; 120 | 121 | while (rn->left) { 122 | rp = &rn->left; 123 | rn = rn->left; 124 | } 125 | 126 | n->data = rn->data; 127 | n = rn; 128 | p = rp; 129 | } 130 | 131 | child = n->left ? n->left : n->right; 132 | *p = child; 133 | 134 | return NULL; 135 | } 136 | 137 | void insert_test() 138 | { 139 | struct root tree; 140 | struct node* n; 141 | 142 | tree.r = NULL; 143 | 144 | insert(&tree, new_node(9)); 145 | 146 | insert(&tree, new_node(5)); 147 | insert(&tree, new_node(2)); 148 | insert(&tree, new_node(8)); 149 | 150 | insert(&tree, new_node(18)); 151 | insert(&tree, new_node(13)); 152 | insert(&tree, new_node(21)); 153 | insert(&tree, new_node(20)); 154 | 155 | dump(tree.r, 0, root); 156 | 157 | n = find(&tree, 18); 158 | if (n && n->data == 18) 159 | printf("Get 18\n"); 160 | 161 | } 162 | 163 | void delete_test() 164 | { 165 | struct root tree; 166 | struct node* n; 167 | 168 | tree.r = NULL; 169 | 170 | insert(&tree, new_node(9)); 171 | 172 | insert(&tree, new_node(5)); 173 | insert(&tree, new_node(2)); 174 | insert(&tree, new_node(8)); 175 | 176 | insert(&tree, new_node(18)); 177 | insert(&tree, new_node(13)); 178 | insert(&tree, new_node(21)); 179 | insert(&tree, new_node(20)); 180 | 181 | dump(tree.r, 0, root); 182 | 183 | delete(&tree, 20); 184 | printf("Delete 20\n"); 185 | dump(tree.r, 0, root); 186 | 187 | delete(&tree, 9); 188 | printf("Delete 9\n"); 189 | dump(tree.r, 0, root); 190 | } 191 | 192 | int main() 193 | { 194 | //insert_test(); 195 | delete_test(); 196 | return 0; 197 | } 198 | -------------------------------------------------------------------------------- /docx/tagged/Algorithm终稿18.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/Algorithm终稿18.pdf -------------------------------------------------------------------------------- /docx/tagged/C++ 基础知识总结_终稿_124.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/C++ 基础知识总结_终稿_124.docx -------------------------------------------------------------------------------- /docx/tagged/C++ 基础知识总结_终稿_124.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/C++ 基础知识总结_终稿_124.pdf -------------------------------------------------------------------------------- /docx/tagged/C++基础知识简单汇总_终稿106__tagged.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/C++基础知识简单汇总_终稿106__tagged.docx -------------------------------------------------------------------------------- /docx/tagged/C++基础知识简单汇总_终稿106__tagged.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/C++基础知识简单汇总_终稿106__tagged.pdf -------------------------------------------------------------------------------- /docx/tagged/C++对象模型-32.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/C++对象模型-32.docx -------------------------------------------------------------------------------- /docx/tagged/C++对象模型-32.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/C++对象模型-32.pdf -------------------------------------------------------------------------------- /docx/tagged/C++题目汇总终稿 70.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/C++题目汇总终稿 70.docx -------------------------------------------------------------------------------- /docx/tagged/C++题目汇总终稿 70.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/C++题目汇总终稿 70.pdf -------------------------------------------------------------------------------- /docx/tagged/C语言,嵌入式_tagged_2019-0623.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/C语言,嵌入式_tagged_2019-0623.docx -------------------------------------------------------------------------------- /docx/tagged/C语言,嵌入式_tagged_2019-0623.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/C语言,嵌入式_tagged_2019-0623.pdf -------------------------------------------------------------------------------- /docx/tagged/DataStructure终稿20.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/DataStructure终稿20.pdf -------------------------------------------------------------------------------- /docx/tagged/EffectiveC++终稿44__tagged_2019-0624.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/EffectiveC++终稿44__tagged_2019-0624.docx -------------------------------------------------------------------------------- /docx/tagged/EffectiveC++终稿44__tagged_2019-0624.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/EffectiveC++终稿44__tagged_2019-0624.pdf -------------------------------------------------------------------------------- /docx/tagged/牛客网C++面试真题—已注释.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/牛客网C++面试真题—已注释.pdf -------------------------------------------------------------------------------- /docx/tagged/预处理器.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/预处理器.docx -------------------------------------------------------------------------------- /docx/tagged/预处理器.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/tagged/预处理器.pdf -------------------------------------------------------------------------------- /docx/untagged/Algorithm终稿18.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/untagged/Algorithm终稿18.docx -------------------------------------------------------------------------------- /docx/untagged/C++ Primer 5th笔记200.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/untagged/C++ Primer 5th笔记200.docx -------------------------------------------------------------------------------- /docx/untagged/DataStructure终稿20.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/untagged/DataStructure终稿20.docx -------------------------------------------------------------------------------- /docx/untagged/IBM_链表.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/untagged/IBM_链表.docx -------------------------------------------------------------------------------- /docx/untagged/STL使用.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/untagged/STL使用.docx -------------------------------------------------------------------------------- /docx/untagged/STL源码剖析终稿62.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/untagged/STL源码剖析终稿62.docx -------------------------------------------------------------------------------- /docx/untagged/STL简单7终稿.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/untagged/STL简单7终稿.docx -------------------------------------------------------------------------------- /docx/untagged/STL详细终稿43.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/untagged/STL详细终稿43.docx -------------------------------------------------------------------------------- /docx/untagged/UNIX环境高级编程88.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/untagged/UNIX环境高级编程88.docx -------------------------------------------------------------------------------- /docx/untagged/UNIX网络编程卷1--59.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/untagged/UNIX网络编程卷1--59.docx -------------------------------------------------------------------------------- /docx/untagged/嵌入式笔试题.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/untagged/嵌入式笔试题.docx -------------------------------------------------------------------------------- /docx/untagged/指针.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/untagged/指针.docx -------------------------------------------------------------------------------- /docx/untagged/操作系统笔记32.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/untagged/操作系统笔记32.docx -------------------------------------------------------------------------------- /docx/untagged/算法题总结 274.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/untagged/算法题总结 274.docx -------------------------------------------------------------------------------- /docx/untagged/计算机网络--23.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/untagged/计算机网络--23.docx -------------------------------------------------------------------------------- /docx/untagged/高级指针话题.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/docx/untagged/高级指针话题.docx -------------------------------------------------------------------------------- /multiThread_notes/c++多线程_12_30.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/multiThread_notes/c++多线程_12_30.pptx -------------------------------------------------------------------------------- /multiThread_notes/附件/pdf/multithreaded_server.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/multiThread_notes/附件/pdf/multithreaded_server.pdf -------------------------------------------------------------------------------- /multiThread_notes/附件/ppt.txt: -------------------------------------------------------------------------------- 1 | 进程与线程 2 | 线程更快,轻量级,但是存在同步问题. 3 | Thread的使用 4 | 0.start 5 | 1.join和detach.以及不设置 6 | 2.异常 7 | 3.RAII 8 | 4.gei_id 9 | 5.detach 引用问题 10 | 6.std::move 11 | 7. hardware_concurrrency() 12 | 13 | Data Race and Mutex 14 | 1.RAII 15 | 2.Data Race (多线程取栈元素) 16 | 3.使用Mutex保护 17 | 18 | 死锁 19 | 0.多个Mutex顺序不当 20 | 避免死锁{ 21 | 0.使用一个mutex. 22 | 1.持有lock的函数不再调用其他的用户函数. 23 | 2.std::lock多个mutex 24 | 3.lock顺序一致 25 | } 26 | 27 | unique_lock和延时初始化 28 | demo: open file并写数据.open file 只做一次 29 | 写数据做多次. 30 | open_and_write(); 31 | 0.需要线程安全,单mutex,存在打开2次的可能,使用uniq 32 | 1.lock 性能降低 33 | 2.std::once_flag 34 | 35 | condition Variable 36 | 2个线程实现PV操作,难以控制sleep/while 37 | wait虚假唤醒 38 | notify_one 39 | notify_all 40 | 41 | std::future和async 42 | fu.get() 43 | std::launch方式 44 | promise p; 45 | p.set(); 46 | p.set_ex();//异常也可以设置 47 | 48 | 各种可以调用的函数 49 | std::bind 50 | std::async 51 | std::call_once() 52 | 53 | packaged_tast 54 | 回顾: 55 | thread 56 | mutex 57 | lock_guard 58 | Time 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/Data Race.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | std::mutex m; 8 | void func_1(int count){ 9 | static int mMoney =0 ; 10 | for(int i =0 ;i lock(m); 12 | ++mMoney; 13 | } 14 | std::cout << mMoney <<" "; 15 | } 16 | 17 | int main() { 18 | 19 | 20 | std::thread t1(func_1,100000); 21 | std::thread t2(func_1,200000); 22 | std::thread t3(func_1,300000); 23 | std::thread t4(func_1,400000); 24 | t1.join(); 25 | t2.join(); 26 | t3.join(); 27 | t4.join(); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/_class/class_recursive_mutex.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class mutex { 6 | public: 7 | void lock(); 8 | void unlock(); 9 | protected: 10 | uint32_t key{}; //bits 0-30: thread_handle, bit 31: hasWaiters_flag 11 | }; 12 | 13 | class recursive_mutex : public mutex { 14 | public: 15 | void lock() { 16 | uint32_t handle = current_thread_native_handle(); //obtained from TLS memory in most OS 17 | if ((key & 0x7FFFFFFF) == handle) { // Impossible to return true unless you own the mutex. 18 | uses++; // we own the mutex, just increase uses. 19 | } else { 20 | mutex::lock(); // we don't own the mutex, try to obtain it. 21 | uses = 1; 22 | } 23 | } 24 | 25 | void unlock() { 26 | // asserts for debug, we should own the mutex and uses > 0 27 | --uses; 28 | if (uses == 0) { 29 | mutex::unlock(); 30 | } 31 | } 32 | private: 33 | uint32_t uses{}; // no need to be atomic, can only be modified in exclusion and only interesting read is on exclusion. 34 | }; 35 | 36 | -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/_class/hierarchical_mutex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class hierarchical_mutex { 6 | std::mutex internal_mutex; 7 | const unsigned long hierarchy_value; // 当前层级值 8 | unsigned long previous_hierarchy_value; // 前一线程的层级值 9 | // 所在线程的层级值,thread_local表示值存在于线程存储期 10 | static thread_local unsigned long this_thread_hierarchy_value; 11 | void check_for_hierarchy_violation() { // 检查是否违反层级结构 12 | if (this_thread_hierarchy_value <= hierarchy_value) { 13 | throw std::logic_error("mutex hierarchy violated"); 14 | } 15 | } 16 | void update_hierarchy_value() { 17 | // 先存储当前线程的层级值(用于解锁时恢复) 18 | previous_hierarchy_value = this_thread_hierarchy_value; 19 | // 再把其设为锁的层级值 20 | this_thread_hierarchy_value = hierarchy_value; 21 | } 22 | 23 | public: 24 | explicit hierarchical_mutex(unsigned long value) 25 | : hierarchy_value(value), previous_hierarchy_value(0) {} 26 | void lock() { 27 | check_for_hierarchy_violation(); // 要求线程层级值大于锁的层级值 28 | internal_mutex.lock(); // 内部锁被锁住 29 | update_hierarchy_value(); // 更新层级值 30 | } 31 | void unlock() { 32 | if (this_thread_hierarchy_value != hierarchy_value) { 33 | throw std::logic_error("mutex hierarchy violated"); 34 | } 35 | // 恢复前一线程的层级值 36 | this_thread_hierarchy_value = previous_hierarchy_value; 37 | internal_mutex.unlock(); 38 | } 39 | bool try_lock() { 40 | check_for_hierarchy_violation(); 41 | if (!internal_mutex.try_lock()) return false; 42 | update_hierarchy_value(); 43 | return true; 44 | } 45 | }; 46 | 47 | thread_local unsigned long // 初始化为ULONG_MAX以使构造锁时能通过检查 48 | hierarchical_mutex::this_thread_hierarchy_value(ULONG_MAX); 49 | 50 | // 设定值来表示层级 51 | hierarchical_mutex high(10000); 52 | hierarchical_mutex mid(6000); 53 | hierarchical_mutex low(5000); 54 | 55 | void lf() { // 最低层函数 56 | std::scoped_lock l(low); 57 | } 58 | 59 | void hf() { 60 | std::scoped_lock l(high); 61 | lf(); // 可以调用低层函数 62 | } 63 | 64 | void mf() { 65 | std::scoped_lock l(mid); 66 | hf(); // 中层调用了高层函数,违反了层次结构 67 | } 68 | 69 | using namespace std; 70 | int main() { 71 | hf(); // OK 72 | mf(); // 错误 73 | } -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/_class/joining_thread.hpp: -------------------------------------------------------------------------------- 1 | // 在std::thread基础上添加了析构函数 2 | #include 3 | #include 4 | 5 | class joining_thread { 6 | std::thread t; 7 | 8 | public: 9 | joining_thread() noexcept = default; 10 | 11 | template 12 | explicit joining_thread(T&& f, Ts&&... args) 13 | : t(std::forward(f), std::forward(args)...) {} 14 | 15 | explicit joining_thread(std::thread x) noexcept : t(std::move(x)) {} 16 | joining_thread(joining_thread&& rhs) noexcept : t(std::move(rhs.t)) {} 17 | 18 | joining_thread& operator=(joining_thread&& rhs) noexcept { 19 | if (joinable()) join(); 20 | t = std::move(rhs.t); 21 | return *this; 22 | } 23 | 24 | joining_thread& operator=(std::thread rhs) noexcept { 25 | if (joinable()) join(); 26 | t = std::move(rhs); 27 | return *this; 28 | } 29 | 30 | ~joining_thread() noexcept { 31 | if (joinable()) join(); 32 | } 33 | 34 | void swap(joining_thread&& rhs) noexcept { t.swap(rhs.t); } 35 | std::thread::id get_id() const noexcept { return t.get_id(); } 36 | bool joinable() const noexcept { return t.joinable(); } 37 | void join() { t.join(); } 38 | void detach() { t.detach(); } 39 | std::thread& as_thread() noexcept { return t; } 40 | const std::thread& as_thread() const noexcept { return t; } 41 | }; -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/_class/open_source/ThreadPool-master/COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Jakob Progsch, Václav Zeman 2 | 3 | This software is provided 'as-is', without any express or implied 4 | warranty. In no event will the authors be held liable for any damages 5 | arising from the use of this software. 6 | 7 | Permission is granted to anyone to use this software for any purpose, 8 | including commercial applications, and to alter it and redistribute it 9 | freely, subject to the following restrictions: 10 | 11 | 1. The origin of this software must not be misrepresented; you must not 12 | claim that you wrote the original software. If you use this software 13 | in a product, an acknowledgment in the product documentation would be 14 | appreciated but is not required. 15 | 16 | 2. Altered source versions must be plainly marked as such, and must not be 17 | misrepresented as being the original software. 18 | 19 | 3. This notice may not be removed or altered from any source 20 | distribution. 21 | -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/_class/open_source/ThreadPool-master/README.md: -------------------------------------------------------------------------------- 1 | ThreadPool 2 | ========== 3 | 4 | A simple C++11 Thread Pool implementation. 5 | 6 | Basic usage: 7 | ```c++ 8 | // create thread pool with 4 worker threads 9 | ThreadPool pool(4); 10 | 11 | // enqueue and store future 12 | auto result = pool.enqueue([](int answer) { return answer; }, 42); 13 | 14 | // get result from future 15 | std::cout << result.get() << std::endl; 16 | 17 | ``` 18 | -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/_class/open_source/ThreadPool-master/ThreadPool.h: -------------------------------------------------------------------------------- 1 | #ifndef THREAD_POOL_H 2 | #define THREAD_POOL_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | class ThreadPool { 15 | public: 16 | ThreadPool(size_t); 17 | template 18 | auto enqueue(F&& f, Args&&... args) 19 | -> std::future::type>; 20 | ~ThreadPool(); 21 | private: 22 | // need to keep track of threads so we can join them 23 | std::vector< std::thread > workers; 24 | // the task queue 25 | std::queue< std::function > tasks; 26 | 27 | // synchronization 28 | std::mutex queue_mutex; 29 | std::condition_variable condition; 30 | bool stop; 31 | }; 32 | 33 | // the constructor just launches some amount of workers 34 | inline ThreadPool::ThreadPool(size_t threads) 35 | : stop(false) 36 | { 37 | for(size_t i = 0;i task; 44 | 45 | { 46 | std::unique_lock lock(this->queue_mutex); 47 | this->condition.wait(lock, 48 | [this]{ return this->stop || !this->tasks.empty(); }); 49 | if(this->stop && this->tasks.empty()) 50 | return; 51 | task = std::move(this->tasks.front()); 52 | this->tasks.pop(); 53 | } 54 | 55 | task(); 56 | } 57 | } 58 | ); 59 | } 60 | 61 | // add new work item to the pool 62 | template 63 | auto ThreadPool::enqueue(F&& f, Args&&... args) 64 | -> std::future::type> 65 | { 66 | using return_type = typename std::result_of::type; 67 | 68 | auto task = std::make_shared< std::packaged_task >( 69 | std::bind(std::forward(f), std::forward(args)...) 70 | ); 71 | 72 | std::future res = task->get_future(); 73 | { 74 | std::unique_lock lock(queue_mutex); 75 | 76 | // don't allow enqueueing after stopping the pool 77 | if(stop) 78 | throw std::runtime_error("enqueue on stopped ThreadPool"); 79 | 80 | tasks.emplace([task](){ (*task)(); }); 81 | } 82 | condition.notify_one(); 83 | return res; 84 | } 85 | 86 | // the destructor joins all threads 87 | inline ThreadPool::~ThreadPool() 88 | { 89 | { 90 | std::unique_lock lock(queue_mutex); 91 | stop = true; 92 | } 93 | condition.notify_all(); 94 | for(std::thread &worker: workers) 95 | worker.join(); 96 | } 97 | 98 | #endif 99 | -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/_class/open_source/ThreadPool-master/ThreadPool_example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "ThreadPool.h" 6 | 7 | int main() 8 | { 9 | 10 | ThreadPool pool(4); 11 | std::vector< std::future > results; 12 | 13 | for(int i = 0; i < 8; ++i) { 14 | results.emplace_back( 15 | pool.enqueue([i] { 16 | std::cout << "hello " << i << std::endl; 17 | std::this_thread::sleep_for(std::chrono::seconds(1)); 18 | std::cout << "world " << i << std::endl; 19 | return i*i; 20 | }) 21 | ); 22 | } 23 | 24 | for(auto && result: results) 25 | std::cout << result.get() << ' '; 26 | std::cout << std::endl; 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/apply_level/test_async.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | int f(int x){ 7 | if (x < 0){ 8 | throw std::out_of_range("x < 0"); 9 | } 10 | return x; 11 | } 12 | 13 | struct A {// 成员函数 14 | int f(int value){return value;} 15 | int operator()(int value){return value;} 16 | }; 17 | 18 | int main(){ 19 | std::thread t(f,1); // How to got the return value of f? 20 | t.join(); 21 | std::future ft = std::async(f,1);//异步任务 22 | std::cout << ft.get()<<" "; //可阻塞线程,并获取值 1 23 | // std::cout << ft.get(); //再次调用出错 24 | 25 | std::future ft2 = std::async(f,-1);//异步任务 26 | int x = ft2.get(); // 抛出已存储的异常 27 | std::cout << x; 28 | A a; 29 | auto ft1 = std::async(&A::f, &a, 42); // 调用p->f(42),p是指向x的指针 30 | std::cout < 2 | #include 3 | #include 4 | 5 | int main(){ 6 | // using namespace std::chrono; 7 | auto startTimer = std::chrono::high_resolution_clock::now(); 8 | std::this_thread::sleep_for(std::chrono::seconds(1)); 9 | std::chrono::system_clock::time_point 10 | EndTimer = std::chrono::high_resolution_clock::now(); 11 | std::chrono::duration fp_ms = EndTimer - startTimer; 12 | std::cout << "duration:"<< fp_ms.count() << " ms"< 2 | #include 3 | #include 4 | void f(int i) { std::cout << i< f1 = f; //存储函数 18 | std::function f2 = X(); //存储函数对象 19 | std::function f3 = [] (int i) { std::cout << i< f4 = std::bind(f, std::placeholders::_1); //存储 bind 对象 21 | std::function f5 = &A::f; //存储成员函数 22 | std::function f6 = &A::i; //存储数据成员 23 | 24 | f1(1); 25 | f2(2); 26 | f3(3); 27 | f4(4); 28 | struct A a; 29 | 30 | f5(a,5); 31 | f6(a); 32 | std::cout << "f6 =" < 2 | #include 3 | #include 4 | int f(int x){ 5 | if (x < 0){ 6 | throw std::out_of_range("x < 0"); 7 | } 8 | return x; 9 | } 10 | void task_lambda(int value){ 11 | std::packaged_task task(f); 12 | std::future result = task.get_future(); 13 | task(value); //调用std::packaged_task对象,将std::future设为就绪 14 | std::cout << "task_lambda:\t" << result.get() << '\n'; 15 | } 16 | void task_future_error(){ 17 | std::packaged_task task; 18 | //future::get抛出future_error异常 19 | std::future result = task.get_future(); 20 | } 21 | int main(){ 22 | // task_future_error(); 23 | int value = 1; 24 | task_lambda(value); 25 | value = -1; 26 | task_lambda(value); 27 | } 28 | -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/apply_level/test_promise.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | void test_set_promise(std::promise& promiseObj) { 8 | std::cout << "In a thread, making data...\n"; 9 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 10 | promiseObj.set_value(35); //set_value or set_value_at_thread_exit 11 | // promiseObj.set_value_at_thread_exit(9); 12 | } 13 | 14 | void test_get_promise(std::promise& promiseObj) { 15 | std::future futureObj = promiseObj.get_future(); 16 | futureObj.wait(); 17 | std::cout <<" get_promise value ="<< futureObj.get() << std::endl; 18 | } 19 | void test_future_error() { 20 | std::future fu; 21 | std::cout << fu.get() << std::endl; 22 | } 23 | 24 | 25 | int main() { 26 | std::promise promiseObj; 27 | std::thread t1(test_get_promise, std::ref(promiseObj)); 28 | std::thread t2(test_set_promise, std::ref(promiseObj)); 29 | t1.join(); 30 | t2.join(); 31 | // test_future_error(); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/apply_level/test_shared_future.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::promise promise; 9 | std::shared_future fut(promise.get_future()); 10 | auto fun1 = [fut](){ 11 | fut.wait(); // waits for the signal from main() 12 | return fut; 13 | }; 14 | auto fun2 = std::async(std::launch::async, fun1); 15 | auto fun3 = std::async(std::launch::async, fun1); 16 | auto fun4 = std::async(std::launch::async, fun1); 17 | promise.set_value(1); 18 | std::cout<< "fun2 wait: " << fun2.get().get() <<" "< 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::mutex m1; 8 | std::mutex m2; 9 | 10 | auto f1 = [&]() { 11 | std::lock_guard lg1(m1); 12 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); 13 | std::lock_guard lg2(m2); 14 | }; 15 | 16 | auto f2 = [&]() { 17 | std::lock_guard lg1(m2); 18 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); 19 | std::lock_guard lg2(m1); 20 | }; 21 | 22 | std::thread thread1(f1); 23 | std::thread thread2(f2); 24 | thread1.join(); 25 | thread2.join(); 26 | std::cout<< "~main()" < 2 | #include 3 | #include 4 | 5 | std::mutex mtx; 6 | void do_stuff() 7 | { 8 | mtx.lock(); 9 | // ... 做一些事 ...也可能是(抛出异常,return) 10 | mtx.unlock(); //忘记 mtx.unlock() 11 | } 12 | 13 | void do_stuff() 14 | { 15 | std::unique_lock lck {mtx}; 16 | // ... 做一些事 ...也可能是(抛出异常,return) 17 | } 18 | 19 | int main(){ 20 | std::thread st1(do_stuff); 21 | st1.join(); 22 | } 23 | -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/for_test_code/switch.cpp: -------------------------------------------------------------------------------- 1 | // 未用锁进行控制的代码 2 | 3 | unsigned val; 4 | ​ 5 | if (val < 5) { 6 | // ... 其他线程可能在这里改动 val ... 7 | switch (val) { 8 | case 0: // ... 9 | case 1: // ... 10 | case 2: // ... 11 | case 3: // ... 12 | case 4: // ... 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/for_test_code/test_condition_variable_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | std::mutex mutex; 9 | std::condition_variable cond; 10 | std::deque queue; 11 | int curCount = 0; 12 | bool stop = false; 13 | void test_emit_signal(){ 14 | { 15 | std::unique_lock lock(mutex); 16 | curCount --; 17 | queue.push_back(curCount); 18 | }//lock.unlock(); 19 | cond.notify_one(); 20 | } 21 | 22 | void test_case(int count) 23 | { 24 | for(int i = 0; i< count && (!stop) ; i++){ 25 | test_emit_signal(); 26 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); 27 | } 28 | } 29 | void test_wait (int StopValue = 0){ 30 | auto thread_id = std::this_thread::get_id(); 31 | for(;;){ 32 | std::this_thread::sleep_for(std::chrono::milliseconds(50)); 33 | std::unique_lock lock(mutex); 34 | cond.wait(lock ,[](){return stop || !queue.empty();}); 35 | // 这一步会原子地unlock mutex 并进入blocking,不会与enqueue 死锁 36 | if(stop && queue.empty()){ 37 | std::cout << "thread_id:"< 3 | #include 4 | #include 5 | 6 | class scoped_thread { 7 | std::thread t; 8 | 9 | public: 10 | explicit scoped_thread(std::thread x) : t(std::move(x)) { 11 | if (!t.joinable()) { 12 | throw std::logic_error("no thread"); 13 | } 14 | } 15 | ~scoped_thread() { t.join(); } 16 | scoped_thread(const scoped_thread&) = delete; 17 | scoped_thread& operator=(const scoped_thread&) = delete; 18 | }; 19 | 20 | void f() { std::cout << "OK"; } 21 | 22 | int main() { scoped_thread t(std::thread{f}); } 23 | -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/shared_ptr_A_B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct A; 5 | struct B; 6 | 7 | struct A { 8 | std::shared_ptr pointer; 9 | ~A() { 10 | std::cout << "~A" << std::endl; 11 | } 12 | }; 13 | struct B { 14 | std::weak_ptr pointer; 15 | // std::shared_ptr pointer; 16 | ~B() { 17 | std::cout << "~B" << std::endl; 18 | } 19 | }; 20 | 21 | int main() { 22 | auto a = std::make_shared(); 23 | auto b = std::make_shared(); 24 | a->pointer = b; 25 | b->pointer = a; 26 | } 27 | -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/spinlock_mutex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | class spinlock_mutex { 7 | std::atomic_flag flag = ATOMIC_FLAG_INIT; 8 | 9 | public: 10 | void lock() { 11 | while (flag.test_and_set(std::memory_order_acquire)) { 12 | } 13 | } 14 | void unlock() { flag.clear(std::memory_order_release); } 15 | }; 16 | 17 | spinlock_mutex m; 18 | 19 | void f(int n) { 20 | for (int i = 0; i < 100; ++i) { 21 | m.lock(); 22 | std::cout << "Output from thread " << n << '\n'; 23 | m.unlock(); 24 | } 25 | } 26 | 27 | int main() { 28 | std::vector v; 29 | for (int i = 0; i < 10; ++i) { 30 | v.emplace_back(f, i); 31 | } 32 | for (auto& x : v) { 33 | x.join(); 34 | } 35 | } -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/test_gdb_step.txt: -------------------------------------------------------------------------------- 1 | 多线程死锁调试小技巧 2 | 编译代码 3 | # g++ -Wall -std=c++11 dead_lock_demo.cpp -o dead_lock_demo -g -pthread 4 | # ./dead_lock_demo 5 | # ps -aux | grep dead_lock_demo 6 | 7 | # 使用gdb attach 8 | # gdb attach pid 9 | # info thread 10 | # thread 2 11 | # where 12 | # f 4 13 | 14 | # thread 3 15 | # where 16 | # f 4 17 | 18 | 19 | # 使用gdb core dump 20 | # kill -11 pid 21 | # gdb dead_lock_demo core 22 | # thread apply all bt 23 | # thread 2 24 | # f 4 -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/test_jthread_cpp_20.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | //g++ -std=c++20 -pthread 6 | void thread_proc(std::stop_token st){ 7 | while (!st.stop_requested()){ 8 | std::this_thread::sleep_for(std::chrono::seconds(1)); 9 | } 10 | std::cout << "Thread " << std::this_thread::get_id() << " exit" << std::endl; 11 | } 12 | 13 | extern "C" 14 | int main(){ 15 | std::jthread thr(&thread_proc); 16 | std::this_thread::sleep_for(std::chrono::seconds(3)); 17 | thr.request_stop(); 18 | thr.join(); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/test_mutex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //https://en.cppreference.com/w/cpp/thread/mutex 8 | std::map g_pages; 9 | std::mutex g_pages_mutex; 10 | 11 | void save_page(const std::string &url) 12 | { 13 | // simulate a long page fetch 14 | std::this_thread::sleep_for(std::chrono::seconds(2)); 15 | std::string result = "fake content"; 16 | 17 | std::lock_guard guard(g_pages_mutex); 18 | g_pages[url] = result; 19 | } 20 | 21 | int main() 22 | { 23 | std::thread t1(save_page, "http://foo"); 24 | std::thread t2(save_page, "http://bar"); 25 | t1.join(); 26 | t2.join(); 27 | 28 | // safe to access g_pages without lock now, as the threads are joined 29 | for (const auto &pair : g_pages) { 30 | std::cout << pair.first << " => " << pair.second << '\n'; 31 | } 32 | } -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/test_recursive_mutex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class X { 6 | std::recursive_mutex m; 7 | std::string shared; 8 | public: 9 | void fun1() { 10 | std::lock_guard lk(m); 11 | shared = "fun1"; 12 | std::cout << "in fun1, shared variable is now " << shared << '\n'; 13 | } 14 | void fun2() { 15 | std::lock_guard lk(m); 16 | shared = "fun2"; 17 | std::cout << "in fun2, shared variable is now " << shared << '\n'; 18 | fun1(); // recursive lock becomes useful here 19 | std::cout << "back in fun2, shared variable is " << shared << '\n'; 20 | }; 21 | }; 22 | 23 | int main() 24 | { 25 | X x; 26 | std::thread t1(&X::fun1, &x); 27 | std::thread t2(&X::fun2, &x); 28 | t1.join(); 29 | t2.join(); 30 | } -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/test_shared_mutex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | // https://en.cppreference.com/w/cpp/thread/shared_mutex 7 | class ThreadSafeCounter { 8 | public: 9 | ThreadSafeCounter() = default; 10 | 11 | // Multiple threads/readers can read the counter's value at the same time. 12 | unsigned int get() const { 13 | std::shared_lock lock(mutex_); 14 | return value_; 15 | } 16 | 17 | // Only one thread/writer can increment/write the counter's value. 18 | unsigned int increment() { 19 | std::unique_lock lock(mutex_); 20 | return ++value_; 21 | } 22 | 23 | // Only one thread/writer can reset/write the counter's value. 24 | void reset() { 25 | std::unique_lock lock(mutex_); 26 | value_ = 0; 27 | } 28 | 29 | private: 30 | mutable std::shared_mutex mutex_; 31 | unsigned int value_ = 0; 32 | }; 33 | 34 | int main() { 35 | ThreadSafeCounter counter; 36 | 37 | auto increment_and_print = [&counter]() { 38 | for (int i = 0; i < 3; i++) { 39 | std::cout << std::this_thread::get_id() << ' ' << counter.increment() << '\n'; 40 | 41 | // Note: Writing to std::cout actually needs to be synchronized as well 42 | // by another std::mutex. This has been omitted to keep the example small. 43 | } 44 | }; 45 | 46 | std::thread thread1(increment_and_print); 47 | std::thread thread2(increment_and_print); 48 | 49 | thread1.join(); 50 | thread2.join(); 51 | } 52 | -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/test_thread.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | // g++ -std=c++11 -pthread 5 | // https://en.cppreference.com/w/cpp/thread 6 | 7 | void func_byPass(int value = 1){ 8 | std::thread::id pid = std::this_thread::get_id(); 9 | std::cout <<"pid:"<< pid<< __func__ <<":"<< value < p){ 33 | std::cout << __func__ <<":"<< *p< p(new int(43)); 56 | std::thread t4(FuncPoint, std::move(p)); 57 | t4.join(); 58 | 59 | 60 | std::this_thread::sleep_for(std::chrono::seconds(1)); 61 | } 62 | -------------------------------------------------------------------------------- /multiThread_notes/附件/test_demo/thread_guard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class thread_guard { 5 | std::thread& t; 6 | public: 7 | explicit thread_guard(std::thread& x) : t(x) {} 8 | ~thread_guard() { 9 | if (t.joinable()) { 10 | t.join(); 11 | } 12 | } 13 | thread_guard(const thread_guard&) = delete; 14 | thread_guard& operator=(const thread_guard&) = delete; 15 | }; 16 | 17 | void f() { std::cout << "OK"; } 18 | int main() { 19 | std::thread t{f}; 20 | thread_guard g{t}; 21 | } 22 | -------------------------------------------------------------------------------- /multiThread_notes/附件/参考资料.txt: -------------------------------------------------------------------------------- 1 | CppCoreGuidelines 2 | Cpp-Concurrency-in-Action-2ed 3 | efftive_modern_c++ 4 | modern_cpp_tutirial 5 | 陈硕: multithreaded_server 6 | http://files.cppblog.com/Solstice/multithreaded_server.pdf 7 | https://www.cnblogs.com/Solstice/archive/2013/01/28/2879366.html -------------------------------------------------------------------------------- /multiThread_notes/附件/智能指针与内存管理.assets/28051715-e28b1c2264504cb1a275b916a641ecbb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/multiThread_notes/附件/智能指针与内存管理.assets/28051715-e28b1c2264504cb1a275b916a641ecbb.png -------------------------------------------------------------------------------- /multiThread_notes/附件/智能指针与内存管理.assets/28051719-683eba58c3744417b9f5829aaedf872b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/multiThread_notes/附件/智能指针与内存管理.assets/28051719-683eba58c3744417b9f5829aaedf872b.png -------------------------------------------------------------------------------- /multiThread_notes/附件/智能指针与内存管理.assets/28051720-e25d8dd42ec440aa9f1675c52ef1f898.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/multiThread_notes/附件/智能指针与内存管理.assets/28051720-e25d8dd42ec440aa9f1675c52ef1f898.png -------------------------------------------------------------------------------- /multiThread_notes/附件/智能指针与内存管理.assets/28051721-38f027311ff449709a42aa2d6caeb24d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/multiThread_notes/附件/智能指针与内存管理.assets/28051721-38f027311ff449709a42aa2d6caeb24d.png -------------------------------------------------------------------------------- /multiThread_notes/附件/智能指针与内存管理.assets/28051721-66b4ed146abc44a1b3c3155c0326ded8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/multiThread_notes/附件/智能指针与内存管理.assets/28051721-66b4ed146abc44a1b3c3155c0326ded8.png -------------------------------------------------------------------------------- /multiThread_notes/附件/智能指针与内存管理.assets/28051722-fd09bc0bd2984d7c84e2068268d4a9e5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/multiThread_notes/附件/智能指针与内存管理.assets/28051722-fd09bc0bd2984d7c84e2068268d4a9e5.png -------------------------------------------------------------------------------- /multiThread_notes/附件/智能指针与内存管理.assets/pointers1-1640249520497.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/multiThread_notes/附件/智能指针与内存管理.assets/pointers1-1640249520497.png -------------------------------------------------------------------------------- /multiThread_notes/附件/智能指针与内存管理.assets/pointers1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/multiThread_notes/附件/智能指针与内存管理.assets/pointers1.png -------------------------------------------------------------------------------- /multiThread_notes/附件/智能指针与内存管理.assets/pointers2-1640249520562.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/multiThread_notes/附件/智能指针与内存管理.assets/pointers2-1640249520562.png -------------------------------------------------------------------------------- /multiThread_notes/附件/智能指针与内存管理.assets/pointers2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/multiThread_notes/附件/智能指针与内存管理.assets/pointers2.png -------------------------------------------------------------------------------- /multiThread_notes/附件/智能指针与内存管理.assets/shared_ptr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/multiThread_notes/附件/智能指针与内存管理.assets/shared_ptr.png -------------------------------------------------------------------------------- /multiThread_notes/附件/智能指针与内存管理.assets/unique_ptr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/multiThread_notes/附件/智能指针与内存管理.assets/unique_ptr.png -------------------------------------------------------------------------------- /multiThread_notes/附件/术语.txt: -------------------------------------------------------------------------------- 1 | Multi Thread 2 | Dead Lock 3 | Data Race 4 | scoped_lock 5 | mutex 6 | lock_guard 7 | unique_lock 8 | shared_ptr 9 | race condition 10 | once_flag 11 | call_once 12 | Instance 13 | join 14 | condition variable 15 | hardware_concurrency 16 | detach 17 | std::ref 18 | recursive_mutex 19 | shared_mutex 20 | async 21 | future 22 | packaged_task 23 | promise 24 | shared_future 25 | chrono 26 | lambda 27 | function -------------------------------------------------------------------------------- /printf/C++圣经汇总——378.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/printf/C++圣经汇总——378.pdf -------------------------------------------------------------------------------- /printf/数据结构计算机网络操作系统--273.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/printf/数据结构计算机网络操作系统--273.pdf -------------------------------------------------------------------------------- /printf/算法合集解析_275.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/printf/算法合集解析_275.pdf -------------------------------------------------------------------------------- /printf/编程基础知识——301.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/printf/编程基础知识——301.pdf -------------------------------------------------------------------------------- /操作系统笔记32.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/操作系统笔记32.pdf -------------------------------------------------------------------------------- /牛客校招面试题(附答案与解析)全集/pdf在线免费解密.url: -------------------------------------------------------------------------------- 1 | [{000214A0-0000-0000-C000-000000000046}] 2 | Prop3=19,11 3 | [InternetShortcut] 4 | URL=https://smallpdf.com/cn/unlock-pdf 5 | IDList= 6 | -------------------------------------------------------------------------------- /牛客校招面试题(附答案与解析)全集/牛客校招面试题测试(附答案与解析).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/牛客校招面试题(附答案与解析)全集/牛客校招面试题测试(附答案与解析).pdf -------------------------------------------------------------------------------- /牛客校招面试题(附答案与解析)全集/牛客校招面试题测试(附答案与解析)java篇.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/牛客校招面试题(附答案与解析)全集/牛客校招面试题测试(附答案与解析)java篇.pdf -------------------------------------------------------------------------------- /牛客校招面试题(附答案与解析)全集/牛客校招面试题(附答案与解析)c++篇.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/牛客校招面试题(附答案与解析)全集/牛客校招面试题(附答案与解析)c++篇.pdf -------------------------------------------------------------------------------- /牛客校招面试题(附答案与解析)全集/牛客校招面试题(附答案与解析)前端.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/牛客校招面试题(附答案与解析)全集/牛客校招面试题(附答案与解析)前端.pdf -------------------------------------------------------------------------------- /牛客校招面试题(附答案与解析)全集/牛客网C++面试真题——部分注释.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/牛客校招面试题(附答案与解析)全集/牛客网C++面试真题——部分注释.pdf -------------------------------------------------------------------------------- /算法题总结 274.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/算法题总结 274.pdf -------------------------------------------------------------------------------- /计算机网络--23.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/计算机网络--23.pdf -------------------------------------------------------------------------------- /设计模式.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yzhu798/CodingInterviewsNotes/4657780334d79f180fbea557a14e077fac6aad1d/设计模式.pdf --------------------------------------------------------------------------------