├── .DS_Store ├── .gitignore ├── LICENSE ├── MJ恋上数据结构 ├── .DS_Store ├── day1 │ ├── 01_TheComplexity │ │ ├── 01_TheComplexity.xcodeproj │ │ │ ├── project.pbxproj │ │ │ └── project.xcworkspace │ │ │ │ ├── contents.xcworkspacedata │ │ │ │ └── xcshareddata │ │ │ │ └── IDEWorkspaceChecks.plist │ │ └── 01_TheComplexity │ │ │ ├── TimeTool.cpp │ │ │ ├── TimeTool.hpp │ │ │ └── main.cpp │ └── 02_ArrayList │ │ ├── 02_ArrayList.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ ├── contents.xcworkspacedata │ │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ │ └── 02_ArrayList │ │ ├── ArrayList.cpp │ │ ├── ArrayList.hpp │ │ └── main.cpp ├── day2 │ ├── 01_TheComplexity │ │ ├── 01_TheComplexity.xcodeproj │ │ │ ├── project.pbxproj │ │ │ └── project.xcworkspace │ │ │ │ └── xcshareddata │ │ │ │ └── IDEWorkspaceChecks.plist │ │ └── 01_TheComplexity │ │ │ ├── TimeTool.cpp │ │ │ ├── TimeTool.hpp │ │ │ └── main.cpp │ └── 02_ArrayList │ │ ├── .DS_Store │ │ ├── 02_ArrayList.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ ├── contents.xcworkspacedata │ │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ │ └── 02_ArrayList │ │ ├── Array.hpp │ │ ├── ArrayList.hpp │ │ └── main.cpp ├── day3 │ └── LinkedList │ │ ├── .DS_Store │ │ ├── LinkedList.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ ├── contents.xcworkspacedata │ │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ │ └── LinkedList │ │ ├── LinkedList.hpp │ │ └── main.cpp ├── day4 │ ├── 02_ArrayList │ │ ├── 02_ArrayList.xcodeproj │ │ │ ├── project.pbxproj │ │ │ └── project.xcworkspace │ │ │ │ └── xcshareddata │ │ │ │ └── IDEWorkspaceChecks.plist │ │ └── 02_ArrayList │ │ │ ├── Array.hpp │ │ │ ├── ArrayList1.hpp │ │ │ ├── ArrayList2.hpp │ │ │ └── main.cpp │ └── LinkedList │ │ ├── LinkedList.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ │ └── LinkedList │ │ ├── LinkedList.hpp │ │ ├── SingleLinkedList.hpp │ │ ├── SingleLinkedList2.hpp │ │ └── main.cpp ├── day5 │ ├── 02-Array-Stack │ │ ├── 02-Array-Stack.xcodeproj │ │ │ ├── project.pbxproj │ │ │ └── project.xcworkspace │ │ │ │ └── xcshareddata │ │ │ │ └── IDEWorkspaceChecks.plist │ │ └── 02-Array-Stack │ │ │ ├── ArrayList.hpp │ │ │ ├── ArrayStack.hpp │ │ │ ├── Stack.hpp │ │ │ └── main.mm │ ├── 02_ArrayList │ │ ├── 02_ArrayList.xcodeproj │ │ │ ├── project.pbxproj │ │ │ └── project.xcworkspace │ │ │ │ └── xcshareddata │ │ │ │ └── IDEWorkspaceChecks.plist │ │ └── 02_ArrayList │ │ │ ├── Array.hpp │ │ │ ├── ArrayList1.hpp │ │ │ ├── ArrayList2.hpp │ │ │ └── main.cpp │ └── LinkedList │ │ ├── LinkedList.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ │ └── LinkedList │ │ ├── Circle │ │ ├── CircleLinkedList.hpp │ │ └── SingleCircleLinkedList.hpp │ │ ├── LinkedList.hpp │ │ ├── Single │ │ ├── SingleLinkedList.hpp │ │ └── SingleLinkedList2.hpp │ │ └── main.cpp └── day6 │ ├── 02-Array-Stack │ ├── 02-Array-Stack.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ └── 02-Array-Stack │ │ ├── ArrayList.hpp │ │ ├── ArrayStack.hpp │ │ ├── Stack.hpp │ │ └── main.mm │ └── 08-Queues-Comparison │ ├── 08-Queues-Comparison.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist │ └── 08-Queues-Comparison │ ├── ArrayList.hpp │ ├── ArrayQueue.hpp │ ├── LoopQueue.hpp │ ├── Queue.hpp │ └── main.cpp ├── OC版本数据结构 ├── CoreFoundation │ ├── .DS_Store │ ├── CoreFoundation.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ ├── CoreFoundation │ │ ├── AppDelegate.h │ │ ├── AppDelegate.m │ │ ├── Assets.xcassets │ │ │ ├── AppIcon.appiconset │ │ │ │ └── Contents.json │ │ │ └── Contents.json │ │ ├── Base.lproj │ │ │ ├── LaunchScreen.storyboard │ │ │ └── Main.storyboard │ │ ├── Info.plist │ │ ├── MTFoundation │ │ │ ├── MTArray.h │ │ │ ├── MTArray.m │ │ │ ├── MTDictionary.h │ │ │ ├── MTDictionary.m │ │ │ ├── MTMutableArray.h │ │ │ ├── MTMutableArray.m │ │ │ ├── MTMutableDictionary.h │ │ │ ├── MTMutableDictionary.m │ │ │ ├── MTSet.h │ │ │ └── MTSet.m │ │ ├── ViewController.h │ │ ├── ViewController.m │ │ └── main.m │ ├── CoreFoundationTests │ │ ├── CoreFoundationTests.m │ │ └── Info.plist │ └── CoreFoundationUITests │ │ ├── CoreFoundationUITests.m │ │ └── Info.plist ├── LRUCache │ ├── .idea │ │ ├── LRUCache.iml │ │ ├── encodings.xml │ │ ├── misc.xml │ │ ├── modules.xml │ │ ├── vcs.xml │ │ ├── workspace.xml │ │ └── xcode.xml │ ├── LRUCache.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ └── LRUCache │ │ ├── LRUCache.h │ │ ├── LRUCache.mm │ │ ├── LinkList.hpp │ │ ├── Person.h │ │ ├── Person.m │ │ └── main.m └── iOS-DataStructures │ ├── .DS_Store │ ├── iOS-DataStructures.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist │ ├── iOS-DataStructures │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Assets.xcassets │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ └── Contents.json │ ├── Base.lproj │ │ ├── LaunchScreen.storyboard │ │ └── Main.storyboard │ ├── DataStructures │ │ ├── ArrayList.h │ │ ├── ArrayList.m │ │ ├── ArrayQueue.h │ │ ├── ArrayQueue.m │ │ ├── ArrayStack.h │ │ ├── ArrayStack.m │ │ ├── LinkedList.h │ │ ├── LinkedList.m │ │ ├── LinkedListStack.h │ │ ├── LinkedListStack.m │ │ ├── LoopQueue.h │ │ └── LoopQueue.m │ ├── Info.plist │ ├── Person.h │ ├── Person.m │ ├── ViewController.h │ ├── ViewController.m │ └── main.mm │ ├── iOS-DataStructuresTests │ ├── Info.plist │ ├── TestArray.m │ ├── TestStack.m │ └── iOS_DataStructuresTests.m │ └── iOS-DataStructuresUITests │ ├── Info.plist │ └── iOS_DataStructuresUITests.m ├── leetcode ├── .DS_Store ├── 栈 │ ├── _20_有效的括号 │ │ ├── _20_有效的括号.xcodeproj │ │ │ ├── project.pbxproj │ │ │ └── project.xcworkspace │ │ │ │ └── xcshareddata │ │ │ │ └── IDEWorkspaceChecks.plist │ │ └── _20_有效的括号 │ │ │ ├── Stack │ │ │ ├── ArrayList.hpp │ │ │ ├── ArrayStack.hpp │ │ │ └── Stack.hpp │ │ │ └── main.cpp │ └── _232_用栈实现队列 │ │ ├── _232_用栈实现队列.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ │ └── _232_用栈实现队列 │ │ ├── Stack │ │ ├── ArrayList.hpp │ │ ├── ArrayStack.hpp │ │ └── Stack.hpp │ │ └── main.cpp └── 链表 │ ├── 203_linkedlistremove │ ├── .DS_Store │ ├── 203_linkedlistremove.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ ├── contents.xcworkspacedata │ │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ └── 203_linkedlistremove │ │ └── main.cpp │ ├── 83_删除排序链表中的重复元素 │ ├── .DS_Store │ ├── 83_删除排序链表中的重复元素.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ ├── contents.xcworkspacedata │ │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ └── 83_删除排序链表中的重复元素 │ │ └── main.cpp │ ├── 876_链表的中间结点 │ ├── 876_链表的中间结点.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ ├── contents.xcworkspacedata │ │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ └── 876_链表的中间结点 │ │ └── main.cpp │ ├── _141_环形链表 │ ├── _141_环形链表.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ └── _141_环形链表 │ │ └── main.cpp │ └── _206_反转链表 │ ├── _206_反转链表.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist │ └── _206_反转链表 │ └── main.cpp └── readme.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BestiOSDev/MJ_DataStructures/e2bab856549ab1d199b701c4f726cfc3689de201/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | # CocoaPods 32 | # 33 | # We recommend against adding the Pods directory to your .gitignore. However 34 | # you should judge for yourself, the pros and cons are mentioned at: 35 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 36 | # 37 | # Pods/ 38 | 39 | # Carthage 40 | # 41 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 42 | # Carthage/Checkouts 43 | 44 | Carthage/Build 45 | 46 | # fastlane 47 | # 48 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 49 | # screenshots whenever they are needed. 50 | # For more information about the recommended setup visit: 51 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 52 | 53 | fastlane/report.xml 54 | fastlane/Preview.html 55 | fastlane/screenshots/**/*.png 56 | fastlane/test_output 57 | 58 | # Code Injection 59 | # 60 | # After new code Injection tools there's a generated folder /iOSInjectionProject 61 | # https://github.com/johnno1962/injectionforxcode 62 | 63 | iOSInjectionProject/ 64 | -------------------------------------------------------------------------------- /MJ恋上数据结构/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BestiOSDev/MJ_DataStructures/e2bab856549ab1d199b701c4f726cfc3689de201/MJ恋上数据结构/.DS_Store -------------------------------------------------------------------------------- /MJ恋上数据结构/day1/01_TheComplexity/01_TheComplexity.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day1/01_TheComplexity/01_TheComplexity.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day1/01_TheComplexity/01_TheComplexity/TimeTool.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // TimeTool.cpp 3 | // 01_TheComplexity 4 | // 5 | // Created by dzb on 2019/4/15. 6 | // Copyright © 2019年 dzb. All rights reserved. 7 | // 8 | 9 | #include "TimeTool.hpp" 10 | #include 11 | 12 | 13 | void TimeTool::task(char *title,int n,Func func) { 14 | if (func == nullptr) { 15 | return; 16 | } 17 | std::cout< 13 | 14 | typedef void(*Func)(int n); 15 | 16 | class TimeTool { 17 | 18 | public: 19 | //C++类的静态方法 20 | static void task(char *title,int n,Func func); 21 | }; 22 | 23 | #endif /* TimeTool_hpp */ 24 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day1/01_TheComplexity/01_TheComplexity/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // 01_TheComplexity 4 | // 5 | // Created by dzb on 2019/4/15. 6 | // Copyright © 2019年 dzb. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | #include "TimeTool.hpp" 13 | 14 | using namespace std; 15 | 16 | /* 0 1 2 3 4 5 17 | * 0 1 1 2 3 5 8 13 .... 18 | */ 19 | 20 | // O(2^n) 21 | int fib1(int n) { 22 | if (n <= 1) return n; 23 | return fib1(n - 1) + fib1(n - 2); 24 | } 25 | 26 | // O(n) 27 | int fib2(int n) { 28 | if (n <= 1) return n; 29 | 30 | int first = 0; 31 | int second = 1; 32 | for (int i = 0; i < n - 1; i++) { 33 | int sum = first + second; 34 | first = second; 35 | second = sum; 36 | } 37 | return second; 38 | } 39 | 40 | void test1(int n) { 41 | // 汇编指令 42 | 43 | // 1 44 | if (n > 10) { 45 | std::cout<<"n > 10"< 5) { // 2 47 | std::cout<<"n > 5"< 0) { 106 | cout<<"test"< 0) { 114 | cout<<"test"< 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day1/02_ArrayList/02_ArrayList.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day1/02_ArrayList/02_ArrayList/ArrayList.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // ArrayList.cpp 3 | // 02_ArrayList 4 | // 5 | // Created by dzb on 2019/4/15. 6 | // Copyright © 2019年 dzb. All rights reserved. 7 | // 8 | 9 | #include "ArrayList.hpp" 10 | #include 11 | 12 | static int DEFAULT_CAPACITY = 10; 13 | static int ELEMENT_NOT_FOUND = 10; 14 | 15 | //MARK:构造方法 16 | 17 | //无参构造方法 调用有参构造函数 相当于initWithCapacity:10 18 | ArrayList::ArrayList() : ArrayList(DEFAULT_CAPACITY) { 19 | std::cout<<"ArrayList() 无参函数" << std::endl; 20 | 21 | } 22 | // 有参构造函数 23 | ArrayList::ArrayList(int capacity) { 24 | std::cout<<"ArrayList(int capacity) 有参构造函数" << std::endl; 25 | 26 | if (capacity < DEFAULT_CAPACITY) { 27 | capacity = DEFAULT_CAPACITY; 28 | } 29 | m_size = 0; 30 | m_capacity = capacity; 31 | //创建int数组 大小为capacity 初始化所有元素为0 32 | m_elements = new int[capacity]{0}; 33 | } 34 | //析构函数 35 | ArrayList::~ArrayList() { 36 | if (m_elements != nullptr) { 37 | delete [] m_elements; 38 | } 39 | std::cout<<"~ArrayList() 析构函数" << std::endl; 40 | } 41 | /** 42 | * 清除所有元素 43 | */ 44 | void ArrayList::clear() { 45 | m_size = 0; 46 | } 47 | /** 48 | * 元素的数量 49 | */ 50 | int ArrayList::size() { 51 | return m_size; 52 | } 53 | /** 54 | * 是否为空 55 | */ 56 | bool ArrayList::isEmpty() { 57 | return m_size == 0; 58 | } 59 | /** 60 | * 是否包含某个元素 61 | */ 62 | bool ArrayList::contains(int element) { 63 | return indexOf(element) != ELEMENT_NOT_FOUND; 64 | } 65 | /** 66 | * 添加元素到尾部 67 | */ 68 | void ArrayList::add(int element) { 69 | 70 | } 71 | 72 | /** 73 | * 获取index位置的元素 74 | * @param index 索引 75 | */ 76 | int ArrayList::get(int index) { 77 | if (index < 0 || index >= m_size) { 78 | throw "Index out of bounds"; 79 | } 80 | return m_elements[index]; 81 | } 82 | 83 | /** 84 | * 设置index位置的元素 85 | * @param index 索引 86 | * @param element 元素 87 | * @return 原来的元素ֵ 88 | */ 89 | int ArrayList::set(int index, int element) { 90 | if (index < 0 || index >= m_size) { 91 | throw "Index out of bounds"; 92 | } 93 | int old = m_elements[index]; 94 | m_elements[index] = element; 95 | return old; 96 | } 97 | 98 | /** 99 | * 在index位置插入一个元素 100 | * @param index 索引 101 | * @param element 元素 102 | */ 103 | void ArrayList::add(int index, int element) { 104 | 105 | } 106 | 107 | /** 108 | * 删除index位置的元素 109 | * @param index 索引 110 | * @return 要删除的元素的值 111 | */ 112 | int ArrayList::remove(int index) { 113 | return 0; 114 | } 115 | 116 | /** 117 | * 查看元素的索引 118 | * @param element 元素 119 | * @return 返回查找结果 120 | */ 121 | int ArrayList::indexOf(int element) { 122 | for (int i = 0; i < m_size; i++) { 123 | if (m_elements[i] == element) return i; 124 | } 125 | return ELEMENT_NOT_FOUND; 126 | } 127 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day1/02_ArrayList/02_ArrayList/ArrayList.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ArrayList.hpp 3 | // 02_ArrayList 4 | // 5 | // Created by dzb on 2019/4/15. 6 | // Copyright © 2019年 dzb. All rights reserved. 7 | // 8 | 9 | #ifndef ArrayList_hpp 10 | #define ArrayList_hpp 11 | 12 | #include 13 | 14 | 15 | 16 | class ArrayList { 17 | private: 18 | int m_size; 19 | int *m_elements; 20 | int m_capacity; 21 | public: 22 | //无参构造函数 23 | ArrayList(); 24 | //有参构造函数 25 | ArrayList(int capacity); 26 | //析构函数 27 | ~ArrayList(); 28 | /** 29 | * 清除所有元素 30 | */ 31 | void clear(); 32 | /** 33 | * 元素的数量 34 | */ 35 | int size(); 36 | /** 37 | * 是否为空 38 | */ 39 | bool isEmpty(); 40 | 41 | /** 42 | * 是否包含某个元素 43 | */ 44 | bool contains(int element); 45 | 46 | /** 47 | * 添加元素到尾部 48 | */ 49 | void add(int element); 50 | 51 | /** 52 | * 获取index位置的元素 53 | */ 54 | int get(int index); 55 | 56 | /** 57 | * 设置index位置的元素 58 | * @return 原来的元素ֵ 59 | */ 60 | int set(int index, int element); 61 | 62 | /** 63 | * 在index位置插入一个元素 64 | */ 65 | void add(int index, int element); 66 | 67 | /** 68 | * 删除index位置的元素 69 | */ 70 | int remove(int index); 71 | /** 72 | * 查看元素的索引 73 | */ 74 | int indexOf(int element); 75 | 76 | }; 77 | 78 | #endif /* ArrayList_hpp */ 79 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day1/02_ArrayList/02_ArrayList/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // 02_ArrayList 4 | // 5 | // Created by dzb on 2019/4/15. 6 | // Copyright © 2019年 dzb. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "ArrayList.hpp" 11 | 12 | 13 | int main(int argc, const char * argv[]) { 14 | int array[] = {11,22,33}; 15 | ArrayList *list = new ArrayList(); 16 | list->clear(); 17 | 18 | // 1 19 | // size++; 20 | list->add(99); 21 | 22 | // 2 23 | // size++ 24 | list->add(88); 25 | 26 | // list.get(2); 27 | // list.set(2, element) 28 | 29 | delete list; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day2/01_TheComplexity/01_TheComplexity.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day2/01_TheComplexity/01_TheComplexity/TimeTool.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // TimeTool.cpp 3 | // 01_TheComplexity 4 | // 5 | // Created by dzb on 2019/4/15. 6 | // Copyright © 2019年 dzb. All rights reserved. 7 | // 8 | 9 | #include "TimeTool.hpp" 10 | #include 11 | 12 | 13 | void TimeTool::task(char *title,int n,Func func) { 14 | if (func == nullptr) { 15 | return; 16 | } 17 | std::cout< 13 | 14 | typedef void(*Func)(int n); 15 | 16 | class TimeTool { 17 | 18 | public: 19 | //C++类的静态方法 20 | static void task(char *title,int n,Func func); 21 | }; 22 | 23 | #endif /* TimeTool_hpp */ 24 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day2/01_TheComplexity/01_TheComplexity/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // 01_TheComplexity 4 | // 5 | // Created by dzb on 2019/4/15. 6 | // Copyright © 2019年 dzb. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | #include "TimeTool.hpp" 13 | 14 | using namespace std; 15 | 16 | /* 0 1 2 3 4 5 17 | * 0 1 1 2 3 5 8 13 .... 18 | */ 19 | 20 | // O(2^n) 21 | int fib1(int n) { 22 | if (n <= 1) return n; 23 | return fib1(n - 1) + fib1(n - 2); 24 | } 25 | 26 | // O(n) 27 | int fib2(int n) { 28 | if (n <= 1) return n; 29 | 30 | int first = 0; 31 | int second = 1; 32 | for (int i = 0; i < n - 1; i++) { 33 | int sum = first + second; 34 | first = second; 35 | second = sum; 36 | } 37 | return second; 38 | } 39 | 40 | int fib3(int n) { 41 | if (n <= 1) return n; 42 | 43 | int first = 0; 44 | int second = 1; 45 | while (n-- > 1) { 46 | second += first; 47 | first = second - first; 48 | } 49 | return second; 50 | } 51 | 52 | void test1(int n) { 53 | // 汇编指令 54 | 55 | // 1 56 | if (n > 10) { 57 | std::cout<<"n > 10"< 5) { // 2 59 | std::cout<<"n > 5"< 0) { 118 | cout<<"test"< 0) { 126 | cout<<"test"< 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day2/02_ArrayList/02_ArrayList.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day2/02_ArrayList/02_ArrayList/Array.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Array.hpp 3 | // 02_ArrayList 4 | // 5 | // Created by dzb on 2019/4/17. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #ifndef Array_hpp 10 | #define Array_hpp 11 | 12 | #include 13 | 14 | 15 | ///Array 接口类 不可被直接拿来使用 定义了 Array 的一些接口 16 | ///子类才用共用基础方式 重新虚函数 17 | template 18 | class Array { 19 | 20 | public: 21 | /** 22 | * 清除所有元素 23 | */ 24 | virtual void clear(); 25 | /** 26 | * 元素的数量 27 | */ 28 | virtual int size(); 29 | /** 30 | * 是否为空 31 | */ 32 | virtual bool isEmpty(); 33 | 34 | /** 35 | * 是否包含某个元素 36 | */ 37 | virtual bool contains(E element); 38 | 39 | /** 40 | * 添加元素到尾部 41 | */ 42 | virtual void add(E element); 43 | 44 | /** 45 | * 获取index位置的元素 46 | */ 47 | virtual E get(int index); 48 | 49 | /** 50 | * 设置index位置的元素 51 | * @return 原来的元素ֵ 52 | */ 53 | virtual E set(int index, E element); 54 | 55 | /** 56 | * 在index位置插入一个元素 57 | */ 58 | virtual void add(int index, E element); 59 | 60 | /** 61 | * 删除index位置的元素 62 | */ 63 | virtual E remove(int index); 64 | /** 65 | * 查看元素的索引 66 | */ 67 | virtual int indexOf(E element); 68 | //打印数组元素 69 | virtual void toString(); 70 | }; 71 | 72 | /** 73 | * 清除所有元素 74 | */ 75 | template 76 | void Array::clear() { 77 | 78 | } 79 | 80 | /** 81 | * 元素的数量 82 | */ 83 | template 84 | int Array::size() { 85 | return 0; 86 | } 87 | 88 | /** 89 | * 是否为空 90 | */ 91 | template 92 | bool Array::isEmpty() { 93 | return true; 94 | } 95 | 96 | 97 | /** 98 | * 是否包含某个元素 99 | */ 100 | template 101 | bool Array::contains(E element) { 102 | return false; 103 | } 104 | 105 | /** 106 | * 添加元素到尾部 107 | */ 108 | template 109 | void Array::add(E element) { 110 | 111 | } 112 | 113 | /** 114 | * 获取index位置的元素 115 | */ 116 | template 117 | E Array::get(int index) { 118 | return 0; 119 | } 120 | 121 | /** 122 | * 设置index位置的元素 123 | * @return 原来的元素ֵ 124 | */ 125 | template 126 | E Array::set(int index, E element) { 127 | return 0; 128 | } 129 | 130 | /** 131 | * 在index位置插入一个元素 132 | */ 133 | template 134 | void Array::add(int index, E element) { 135 | 136 | } 137 | 138 | /** 139 | * 删除index位置的元素 140 | */ 141 | template 142 | E Array::remove(int index) { 143 | return NULL; 144 | } 145 | /** 146 | * 查看元素的索引 147 | */ 148 | template 149 | int Array::indexOf(E element) { 150 | return 0; 151 | } 152 | 153 | ///打印数组内容 154 | template 155 | void Array::toString() { 156 | 157 | } 158 | 159 | 160 | #endif /* Array_hpp */ 161 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day2/02_ArrayList/02_ArrayList/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // 02_ArrayList 4 | // 5 | // Created by dzb on 2019/4/15. 6 | // Copyright © 2019年 dzb. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "Array.hpp" 11 | #include "ArrayList.hpp" 12 | 13 | using namespace std; 14 | 15 | //Person类 16 | class Person { 17 | private: 18 | int m_age; 19 | char *m_name; 20 | 21 | public: 22 | Person(int age,char *name) : m_age(age),m_name(name) { 23 | 24 | } 25 | ~Person() { 26 | cout << "~Person()" << endl; 27 | } 28 | void toString(); 29 | }; 30 | 31 | //基本类型包装类 32 | template 33 | class NSNumber { 34 | private: 35 | T _data; 36 | public: 37 | NSNumber(T data) : _data(data) { 38 | 39 | } 40 | ~NSNumber() { 41 | cout << "~NSNumber()" << endl; 42 | } 43 | int intValue() { 44 | return (int)_data; 45 | } 46 | double doubleValue() { 47 | return (double)_data; 48 | } 49 | bool boolValue() { 50 | return (bool)_data; 51 | } 52 | 53 | }; 54 | 55 | 56 | void test1() { 57 | // int -> Integer 58 | 59 | // 所有的类,最终都继承java.lang.Object 60 | 61 | // new是向堆空间申请内存 62 | ArrayList *persons = new ArrayList(); 63 | persons->add(new Person(10,"Jack")); 64 | persons->add(new Person(15,"Rose")); 65 | persons->add(new Person(12,"James")); 66 | persons->clear(); 67 | persons->add(new Person(22,"abc")); 68 | persons->toString(); 69 | 70 | ArrayList*> *ints = new ArrayList*>(); 71 | ints->add(new NSNumber(10)); 72 | ints->add(new NSNumber(10)); 73 | ints->add(new NSNumber(22)); 74 | ints->add(new NSNumber(33)); 75 | 76 | ints->toString(); 77 | 78 | ints->clear(); 79 | 80 | delete ints; 81 | 82 | } 83 | 84 | void test2() { 85 | 86 | ArrayList *persons = new ArrayList(); 87 | persons->add(new Person(10,"Jack")); 88 | 89 | persons->add(nullptr); 90 | persons->add(new Person(15,"Rose")); 91 | persons->add(nullptr); 92 | persons->add(new Person(12,"James")); 93 | persons->add(nullptr); 94 | 95 | persons->toString(); 96 | 97 | delete persons; 98 | 99 | } 100 | 101 | int main(int argc, const char * argv[]) { 102 | 103 | test1(); 104 | test2(); 105 | 106 | return 0; 107 | } 108 | 109 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day3/LinkedList/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BestiOSDev/MJ_DataStructures/e2bab856549ab1d199b701c4f726cfc3689de201/MJ恋上数据结构/day3/LinkedList/.DS_Store -------------------------------------------------------------------------------- /MJ恋上数据结构/day3/LinkedList/LinkedList.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day3/LinkedList/LinkedList.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day3/LinkedList/LinkedList/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // LinkedList 4 | // 5 | // Created by dzb on 2019/4/21. 6 | // Copyright © 2019年 dzb. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | #include "LinkedList.hpp" 12 | using namespace std; 13 | 14 | class Person { 15 | public: 16 | int age; 17 | string name; 18 | Person(string name,int age) { 19 | this->name = name; 20 | this->age = age; 21 | } 22 | ~Person() { 23 | cout<<"~Person()"<name == this->name && p->age == this->age) { 28 | return true; 29 | } 30 | return false; 31 | } 32 | }; 33 | 34 | void test1() { 35 | 36 | // 建议 请勿使用堆空间 指向栈空间内存 除非在函数作用域结束后 不再使用链表 37 | // 往里边内部添加元素 尽量使用指针类型 38 | 39 | LinkedList *list = new LinkedList(); 40 | list->add(10); 41 | list->add(20); 42 | list->add(30); 43 | list->for_each_elements([](int element){ 44 | cout << element << endl; 45 | }); 46 | // int a = list->get(0); 47 | // cout << a << endl; 48 | //在这个拉姆达表达式内部可以做内存释放工作 只有new出来的内存 才需要释放内存 49 | list->clear_with_completion([](int element){ 50 | 51 | }); 52 | list->add(100); 53 | list->add(200); 54 | list->add(350); 55 | list->toString(); 56 | 57 | 58 | int e = list->remove(0); 59 | cout << e << endl; 60 | delete list; 61 | 62 | } 63 | 64 | void test2() { 65 | 66 | Person *p1 = new Person(string("dzb"),20); 67 | Person *p2 = new Person(string("james"),24); 68 | Person *p3 = new Person(string("kobe"),24); 69 | LinkedList *list = new LinkedList(); 70 | list->add(p1); 71 | list->add(p2); 72 | list->add(p3); 73 | list->for_each_elements([](Person *person){ 74 | cout << person->name << " age " << person->age << endl; 75 | }); 76 | //在这个拉姆达表达式内部可以做内存释放工作 只有new出来的内存 才需要释放内存 77 | list->clear_with_completion([](Person *person){ 78 | if (person != NULL) { 79 | delete person; 80 | } 81 | }); 82 | 83 | list->add(p1); 84 | list->add(p2); 85 | list->add(p3); 86 | list->toString(); 87 | 88 | ///自定义判断添加规则 重载 == 操作符 89 | int index = list->indexOf(p3); 90 | cout << "p3 index is " << index << endl; 91 | 92 | Person *e = list->remove(0); 93 | cout << e->name << endl; 94 | delete list; 95 | 96 | } 97 | 98 | int main(int argc, const char * argv[]) { 99 | 100 | test1(); 101 | test2(); 102 | 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day4/02_ArrayList/02_ArrayList.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day4/02_ArrayList/02_ArrayList/Array.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Array.hpp 3 | // 02_ArrayList1 4 | // 5 | // Created by dzb on 2019/4/17. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #ifndef Array_hpp 10 | #define Array_hpp 11 | 12 | #include 13 | 14 | 15 | static int DEFAULT_CAPACITY = 10; 16 | static int ELEMENT_NOT_FOUND = 10; 17 | 18 | 19 | ///Array 接口类 不可被直接拿来使用 定义了 Array 的一些接口 20 | ///子类才用共用基础方式 重新虚函数 21 | template 22 | class Array { 23 | 24 | public: 25 | /** 26 | * 清除所有元素 27 | */ 28 | virtual void clear_with_completion(void(*for_each)(E)); 29 | /** 30 | * 元素的数量 31 | */ 32 | virtual int size(); 33 | /** 34 | * 是否为空 35 | */ 36 | virtual bool isEmpty(); 37 | 38 | /** 39 | * 是否包含某个元素 40 | */ 41 | virtual bool contains(E element); 42 | 43 | /** 44 | * 添加元素到尾部 45 | */ 46 | virtual void add(E element); 47 | 48 | /** 49 | * 获取index位置的元素 50 | */ 51 | virtual E get(int index); 52 | 53 | /** 54 | * 设置index位置的元素 55 | * @return 原来的元素ֵ 56 | */ 57 | virtual E set(int index, E element); 58 | 59 | /** 60 | * 在index位置插入一个元素 61 | */ 62 | virtual void add(int index, E element); 63 | 64 | /** 65 | * 删除index位置的元素 66 | */ 67 | virtual E remove(int index); 68 | /** 69 | * 查看元素的索引 70 | */ 71 | virtual int indexOf(E element); 72 | //打印数组元素 73 | virtual void toString(); 74 | }; 75 | 76 | /** 77 | * 清除所有元素 78 | */ 79 | template 80 | void Array::clear_with_completion(void(*for_each)(E)) { 81 | 82 | } 83 | 84 | /** 85 | * 元素的数量 86 | */ 87 | template 88 | int Array::size() { 89 | return 0; 90 | } 91 | 92 | /** 93 | * 是否为空 94 | */ 95 | template 96 | bool Array::isEmpty() { 97 | return true; 98 | } 99 | 100 | 101 | /** 102 | * 是否包含某个元素 103 | */ 104 | template 105 | bool Array::contains(E element) { 106 | return false; 107 | } 108 | 109 | /** 110 | * 添加元素到尾部 111 | */ 112 | template 113 | void Array::add(E element) { 114 | 115 | } 116 | 117 | /** 118 | * 获取index位置的元素 119 | */ 120 | template 121 | E Array::get(int index) { 122 | return 0; 123 | } 124 | 125 | /** 126 | * 设置index位置的元素 127 | * @return 原来的元素ֵ 128 | */ 129 | template 130 | E Array::set(int index, E element) { 131 | return 0; 132 | } 133 | 134 | /** 135 | * 在index位置插入一个元素 136 | */ 137 | template 138 | void Array::add(int index, E element) { 139 | 140 | } 141 | 142 | /** 143 | * 删除index位置的元素 144 | */ 145 | template 146 | E Array::remove(int index) { 147 | return NULL; 148 | } 149 | /** 150 | * 查看元素的索引 151 | */ 152 | template 153 | int Array::indexOf(E element) { 154 | return 0; 155 | } 156 | 157 | ///打印数组内容 158 | template 159 | void Array::toString() { 160 | 161 | } 162 | 163 | 164 | #endif /* Array_hpp */ 165 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day4/02_ArrayList/02_ArrayList/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // 02_ArrayList1 4 | // 5 | // Created by dzb on 2019/4/15. 6 | // Copyright © 2019年 dzb. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "Array.hpp" 11 | #include "ArrayList1.hpp" 12 | #include "ArrayList2.hpp" 13 | 14 | using namespace std; 15 | 16 | //Person类 17 | class Person { 18 | private: 19 | int m_age; 20 | char *m_name; 21 | 22 | public: 23 | Person(int age,char *name) : m_age(age),m_name(name) { 24 | 25 | } 26 | ~Person() { 27 | cout << "~Person()" << endl; 28 | } 29 | void toString(); 30 | }; 31 | 32 | //基本类型包装类 33 | template 34 | class NSNumber { 35 | private: 36 | T _data; 37 | public: 38 | NSNumber(T data) : _data(data) { 39 | 40 | } 41 | ~NSNumber() { 42 | cout << "~NSNumber()" << endl; 43 | } 44 | int intValue() { 45 | return (int)_data; 46 | } 47 | double doubleValue() { 48 | return (double)_data; 49 | } 50 | bool boolValue() { 51 | return (bool)_data; 52 | } 53 | 54 | }; 55 | 56 | 57 | void test1() { 58 | // int -> Integer 59 | 60 | // 所有的类,最终都继承java.lang.Object 61 | 62 | // new是向堆空间申请内存 63 | ArrayList1 *persons = new ArrayList1(); 64 | persons->add(new Person(10,"Jack")); 65 | persons->add(new Person(15,"Rose")); 66 | persons->add(new Person(12,"James")); 67 | persons->clear_with_completion([](Person *p){ 68 | delete p; 69 | }); 70 | persons->add(new Person(22,"abc")); 71 | persons->toString(); 72 | 73 | ArrayList1*> *ints = new ArrayList1*>(); 74 | ints->add(new NSNumber(10)); 75 | ints->add(new NSNumber(10)); 76 | ints->add(new NSNumber(22)); 77 | ints->add(new NSNumber(33)); 78 | 79 | ints->toString(); 80 | 81 | ints->clear_with_completion([](NSNumber *number){ 82 | delete number; 83 | }); 84 | 85 | delete ints; 86 | 87 | } 88 | 89 | void test2() { 90 | 91 | ArrayList1 *persons = new ArrayList1(); 92 | persons->add(new Person(10,"Jack")); 93 | 94 | persons->add(nullptr); 95 | persons->add(new Person(15,"Rose")); 96 | persons->add(nullptr); 97 | persons->add(new Person(12,"James")); 98 | persons->add(nullptr); 99 | 100 | persons->toString(); 101 | 102 | delete persons; 103 | 104 | } 105 | 106 | void test3() { 107 | 108 | ArrayList2 *list2 = new ArrayList2(); 109 | for (int i = 0; i<100; i++) { 110 | list2->add(i); 111 | } 112 | 113 | for (int i = 0; i<100; i++) { 114 | int ret = list2->remove(0); 115 | cout << ret << endl; 116 | } 117 | 118 | delete list2; 119 | 120 | } 121 | 122 | int main(int argc, const char * argv[]) { 123 | // 124 | // test1(); 125 | // test2(); 126 | // 127 | test3(); 128 | 129 | return 0; 130 | } 131 | 132 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day4/LinkedList/LinkedList.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day4/LinkedList/LinkedList/LinkedList.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // LinkedList.hpp 3 | // LinkedList 4 | // 5 | // Created by dzb on 2019/4/21. 6 | // Copyright © 2019年 dzb. All rights reserved. 7 | // 8 | 9 | #ifndef LinkedList_hpp 10 | #define LinkedList_hpp 11 | 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | static const int ELEMENT_NOT_FOUND = -1; 17 | 18 | template 19 | //单向链表采用尾插法 20 | class LinkedList { 21 | 22 | class Node { //内部类 23 | public: 24 | E m_element; 25 | Node *m_next; //后驱 26 | Node *m_prev; //前驱 27 | //无参构造方法 28 | Node() : Node(NULL,NULL,NULL){} 29 | //有参构造方法 30 | Node(E element,Node *next,Node *pre) : m_element(element),m_next(next),m_prev(pre) { 31 | } 32 | //析构方法 33 | ~Node() { 34 | cout<<"~Node()" << endl; 35 | } 36 | }; 37 | 38 | public: 39 | //构造方法 40 | LinkedList(); 41 | //析构方法 42 | ~LinkedList(); 43 | /** 44 | * 元素的数量 45 | */ 46 | int size(); 47 | 48 | /** 49 | * 是否为空 50 | */ 51 | bool isEmpty(); 52 | 53 | /** 54 | * 是否包含某个元素 55 | */ 56 | bool contains(E element); 57 | 58 | /** 59 | * 添加元素到尾部 60 | */ 61 | void add(E element); 62 | 63 | /** 64 | * 获取index位置的元素 65 | */ 66 | E get(int index); 67 | 68 | /** 69 | * 设置index位置的元素 70 | */ 71 | E set(int index, E element); 72 | 73 | /** 74 | * 在index位置插入一个元素 75 | */ 76 | void add(int index, E element); 77 | 78 | /** 79 | * 删除index位置的元素 80 | */ 81 | E remove(int index); 82 | 83 | /** 84 | * 查看元素的索引 85 | */ 86 | int indexOf(E element); 87 | ///遍历所有元素 88 | void for_each_elements(void(*for_each)(E)); 89 | ///销毁链表的元素 90 | void clear_with_completion(void(*for_each)(E)); 91 | //打印链表元素 92 | void toString(); 93 | 94 | private: 95 | Node *_header; 96 | int m_size; 97 | void outOfBounds(int index) { 98 | throw "Index out of bounds"; 99 | } 100 | void rangeCheck(int index) { 101 | if (index < 0 || index >= m_size) { 102 | outOfBounds(index); 103 | } 104 | } 105 | void rangeCheckForAdd(int index) { 106 | if (index < 0 || index > m_size) { 107 | outOfBounds(index); 108 | } 109 | } 110 | Node *node(int index) { 111 | 112 | if (index < (m_size >> 1)) { //从前便利 113 | LinkedList::Node *cur = _header->m_next; 114 | for (int i = 0; i < index; i++) { 115 | cur = cur->m_next; 116 | } 117 | return cur; 118 | } else { ///从后遍历 119 | LinkedList::Node *cur = _header->m_prev; 120 | for (int i = m_size - 1; i > index; i--) { 121 | cur = cur->m_prev; 122 | } 123 | return cur; 124 | } 125 | 126 | } 127 | }; 128 | 129 | //无参构造函数 130 | template 131 | LinkedList::LinkedList() { 132 | this->m_size = 0; 133 | this->_header = new LinkedList::Node(); 134 | _header->m_next = _header->m_prev = _header; 135 | } 136 | 137 | //析构函数 138 | template 139 | LinkedList::~LinkedList() { 140 | cout<<"~LinkedList()"<clear_with_completion(NULL); 143 | } 144 | 145 | /** 146 | * 元素的数量 147 | */ 148 | template 149 | int LinkedList::size() { 150 | return m_size; 151 | } 152 | 153 | /** 154 | * 是否为空 155 | */ 156 | template 157 | bool LinkedList::isEmpty() { 158 | return m_size == 0; 159 | } 160 | 161 | /** 162 | * 是否包含某个元素 163 | */ 164 | template 165 | bool LinkedList::contains(E element) { 166 | return indexOf(element) != ELEMENT_NOT_FOUND; 167 | } 168 | 169 | /** 170 | * 添加元素到尾部 171 | */ 172 | template 173 | void LinkedList::add(E element) { 174 | add(m_size,element); 175 | } 176 | 177 | 178 | /** 179 | * 获取index位置的元素 180 | */ 181 | template 182 | E LinkedList::get(int index) { 183 | rangeCheck(index); 184 | LinkedList::Node *pFind = node(index); 185 | E element = pFind->m_element; 186 | return element; 187 | } 188 | 189 | /** 190 | * 设置index位置的元素 返回上一个值 需要开发者自己拿到这个值后管理内存 191 | */ 192 | template 193 | E LinkedList::set(int index, E element) { 194 | rangeCheck(index); 195 | LinkedList::Node *pFind = node(index); 196 | E old = pFind->m_element; 197 | pFind->m_element = element; 198 | return old; 199 | } 200 | 201 | /** 202 | * 在index位置插入一个元素 203 | */ 204 | template 205 | void LinkedList::add(int index, E element) { 206 | rangeCheckForAdd(index); 207 | //把双向循环链表 想象成汉堡包🍔 就不难立即 add 方法怎么实现 208 | LinkedList::Node *cur = new LinkedList::Node(); 209 | cur->m_element = element; 210 | cur->m_next = _header->m_next; 211 | _header->m_next = cur; 212 | cur->m_prev = _header; 213 | cur->m_next->m_prev = cur; 214 | m_size++; 215 | } 216 | 217 | /** 218 | * 删除index位置的元素 219 | */ 220 | template 221 | E LinkedList::remove(int index) { 222 | rangeCheck(index); 223 | LinkedList::Node *cur = node(index); ///要删除节点 224 | LinkedList::Node *prev = cur->m_prev; ///要删除节点的前驱 225 | prev->m_next = cur->m_next; 226 | prev->m_next->m_prev = prev; 227 | E element = cur->m_element; //要删除的元素 228 | delete cur; //删除节点 对于节点数据需要开发者自行管理内存 229 | m_size--; 230 | return element; 231 | } 232 | 233 | /** 234 | * 查看元素的索引 235 | */ 236 | template 237 | int LinkedList::indexOf(E element) { 238 | LinkedList::Node *cur = _header->m_next; 239 | for (int i = 0; im_element) { 241 | return i; 242 | } 243 | cur = cur->m_next; 244 | } 245 | return ELEMENT_NOT_FOUND; 246 | } 247 | 248 | ///遍历所有元素 249 | template 250 | void LinkedList::for_each_elements(void(*for_each)(E)) { 251 | LinkedList::Node *sh = _header->m_next; 252 | while (sh != _header) { 253 | E obj = sh->m_element; 254 | sh = sh->m_next; 255 | if (for_each != NULL) { 256 | (*for_each)(obj); 257 | } 258 | } 259 | } 260 | ///销毁链表的元素 261 | template 262 | void LinkedList::clear_with_completion(void(*for_each)(E)) { 263 | if (size() == 0) return; 264 | while (m_size > 0) { 265 | E old = remove(m_size-1); 266 | if (for_each != NULL) { 267 | (*for_each)(old); 268 | } 269 | } 270 | } 271 | 272 | //打印元素 273 | template 274 | void LinkedList::toString() { 275 | std::cout << "[ "; 276 | for (int i = 0; i 13 | #include 14 | using namespace std; 15 | 16 | static const int SL_ELEMENT_NOT_FOUND2 = -1; 17 | 18 | template 19 | //单向链表采用尾插法 20 | /** 21 | * 增加一个虚拟头结点 22 | * @author MJ Lee 23 | * 24 | */ 25 | class SingleLinkedList2 { 26 | 27 | class Node { //内部类 28 | public: 29 | E m_element; 30 | Node *m_next; 31 | //无参构造方法 32 | Node() : Node(NULL,NULL){} 33 | //有参构造方法 34 | Node(E element,Node *next) : m_element(element),m_next(next) { 35 | } 36 | //析构方法 37 | ~Node() { 38 | cout<<"~Node()" << endl; 39 | } 40 | }; 41 | 42 | public: 43 | //构造方法 44 | SingleLinkedList2(); 45 | //析构方法 46 | ~SingleLinkedList2(); 47 | /** 48 | * 元素的数量 49 | */ 50 | int size(); 51 | 52 | /** 53 | * 是否为空 54 | */ 55 | bool isEmpty(); 56 | 57 | /** 58 | * 是否包含某个元素 59 | */ 60 | bool contains(E element); 61 | 62 | /** 63 | * 添加元素到尾部 64 | */ 65 | void add(E element); 66 | 67 | /** 68 | * 获取index位置的元素 69 | */ 70 | E get(int index); 71 | 72 | /** 73 | * 设置index位置的元素 74 | */ 75 | E set(int index, E element); 76 | 77 | /** 78 | * 在index位置插入一个元素 79 | */ 80 | void add(int index, E element); 81 | 82 | /** 83 | * 删除index位置的元素 84 | */ 85 | E remove(int index); 86 | 87 | /** 88 | * 查看元素的索引 89 | */ 90 | int indexOf(E element); 91 | ///遍历所有元素 92 | void for_each_elements(void(*for_each)(E)); 93 | ///销毁链表的元素 94 | void clear_with_completion(void(*for_each)(E)); 95 | //打印链表元素 96 | void toString(); 97 | 98 | private: 99 | Node *_dummyHead; 100 | int m_size; 101 | void outOfBounds(int index) { 102 | throw "Index out of bounds"; 103 | } 104 | void rangeCheck(int index) { 105 | if (index < 0 || index >= m_size) { 106 | outOfBounds(index); 107 | } 108 | } 109 | void rangeCheckForAdd(int index) { 110 | if (index < 0 || index > m_size) { 111 | outOfBounds(index); 112 | } 113 | } 114 | Node *node(int index,Node *dummyHead) { 115 | SingleLinkedList2::Node *prev = dummyHead; 116 | for (int i = 0; im_next; 118 | } 119 | return prev; 120 | } 121 | }; 122 | 123 | //无参构造函数 124 | template 125 | SingleLinkedList2::SingleLinkedList2() { 126 | this->_dummyHead = new SingleLinkedList2::Node(); 127 | this->m_size = 0; 128 | } 129 | 130 | //析构函数 131 | template 132 | SingleLinkedList2::~SingleLinkedList2() { 133 | cout<<"~SingleLinkedList2()"<clear_with_completion(NULL); 136 | } 137 | 138 | /** 139 | * 元素的数量 140 | */ 141 | template 142 | int SingleLinkedList2::size() { 143 | return m_size; 144 | } 145 | 146 | /** 147 | * 是否为空 148 | */ 149 | template 150 | bool SingleLinkedList2::isEmpty() { 151 | return m_size == 0; 152 | } 153 | 154 | /** 155 | * 是否包含某个元素 156 | */ 157 | template 158 | bool SingleLinkedList2::contains(E element) { 159 | return indexOf(element) != SL_ELEMENT_NOT_FOUND2; 160 | } 161 | 162 | /** 163 | * 添加元素到尾部 164 | */ 165 | template 166 | void SingleLinkedList2::add(E element) { 167 | add(m_size,element); 168 | } 169 | 170 | 171 | /** 172 | * 获取index位置的元素 173 | */ 174 | template 175 | E SingleLinkedList2::get(int index) { 176 | rangeCheck(index); 177 | SingleLinkedList2::Node *pFind = node(index,_dummyHead->m_next); 178 | E element = pFind->m_element; 179 | return element; 180 | } 181 | 182 | /** 183 | * 设置index位置的元素 返回上一个值 需要开发者自己拿到这个值后管理内存 184 | */ 185 | template 186 | E SingleLinkedList2::set(int index, E element) { 187 | rangeCheck(index); 188 | SingleLinkedList2::Node *pFind = node(index,_dummyHead->m_next); 189 | E old = pFind->m_element; 190 | pFind->m_element = element; 191 | return old; 192 | } 193 | 194 | /** 195 | * 在index位置插入一个元素 196 | */ 197 | template 198 | void SingleLinkedList2::add(int index, E element) { 199 | rangeCheckForAdd(index); 200 | 201 | Node *prev = node(index,_dummyHead); 202 | SingleLinkedList2::Node *cur = new SingleLinkedList2::Node(element,prev->m_next); 203 | prev->m_next = cur; 204 | 205 | m_size++; 206 | } 207 | 208 | /** 209 | * 删除index位置的元素 210 | */ 211 | template 212 | E SingleLinkedList2::remove(int index) { 213 | rangeCheck(index); 214 | E element; //要删除的元素 215 | SingleLinkedList2::Node *prev = node(index,_dummyHead); //要删除节点前驱 216 | SingleLinkedList2::Node *cur = prev->m_next; //要删除的节点 217 | prev->m_next = cur->m_next; 218 | element = cur->m_element; 219 | m_size--; 220 | delete cur; //删除节点 对于节点数据需要开发者自行管理内存 221 | return element; 222 | } 223 | 224 | /** 225 | * 查看元素的索引 226 | */ 227 | template 228 | int SingleLinkedList2::indexOf(E element) { 229 | SingleLinkedList2::Node *cur = _dummyHead->m_next; 230 | for (int i = 0; im_element) { 232 | return i; 233 | } 234 | cur = cur->m_next; 235 | } 236 | return SL_ELEMENT_NOT_FOUND2; 237 | } 238 | 239 | ///遍历所有元素 240 | template 241 | void SingleLinkedList2::for_each_elements(void(*for_each)(E)) { 242 | SingleLinkedList2::Node *sh = _dummyHead->m_next; 243 | while (sh != NULL) { 244 | E obj = sh->m_element; 245 | sh = sh->m_next; 246 | if (for_each != NULL) { 247 | (*for_each)(obj); 248 | } 249 | } 250 | } 251 | ///销毁链表的元素 252 | template 253 | void SingleLinkedList2::clear_with_completion(void(*for_each)(E)) { 254 | if (size() == 0) return; 255 | while (m_size > 0) { 256 | E old = remove(m_size-1); 257 | if (for_each != NULL) { 258 | (*for_each)(old); 259 | } 260 | } 261 | } 262 | 263 | //打印元素 264 | template 265 | void SingleLinkedList2::toString() { 266 | std::cout << "[ "; 267 | for (int i = 0; i 10 | #include 11 | #include "LinkedList.hpp" 12 | #include "SingleLinkedList.hpp" 13 | #include "SingleLinkedList2.hpp" 14 | 15 | using namespace std; 16 | 17 | class Person { 18 | public: 19 | int age; 20 | string name; 21 | Person(string name,int age) { 22 | this->name = name; 23 | this->age = age; 24 | } 25 | ~Person() { 26 | cout<<"~Person()"<name == this->name && p->age == this->age) { 31 | return true; 32 | } 33 | return false; 34 | } 35 | }; 36 | 37 | 38 | /** 39 | 双向循环链表 40 | */ 41 | void test1() { 42 | 43 | // 建议 请勿使用堆空间 指向栈空间内存 除非在函数作用域结束后 不再使用链表 44 | // 往里边内部添加元素 尽量使用指针类型 45 | 46 | LinkedList *list = new LinkedList(); 47 | list->add(10); 48 | list->add(20); 49 | list->add(30); 50 | list->for_each_elements([](int element){ 51 | cout << element << endl; 52 | }); 53 | // int a = list->get(0); 54 | // cout << a << endl; 55 | //在这个拉姆达表达式内部可以做内存释放工作 只有new出来的内存 才需要释放内存 56 | list->clear_with_completion([](int element){ 57 | 58 | }); 59 | list->add(100); 60 | list->add(200); 61 | list->add(350); 62 | list->toString(); 63 | 64 | int a = list->get(2); 65 | cout << a << endl; 66 | 67 | bool ret = list->contains(33); 68 | cout << ret << endl; 69 | 70 | list->set(0,44); 71 | list->toString(); 72 | 73 | int e = list->remove(0); 74 | cout << e << endl; 75 | delete list; 76 | 77 | } 78 | 79 | 80 | /** 81 | 双向循环链表 测试 2 82 | */ 83 | void test4() { 84 | LinkedList *list = new LinkedList(); 85 | for (int i = 0; i<100; i++) { 86 | list->add(i); 87 | } 88 | list->toString(); 89 | 90 | list->clear_with_completion(NULL); 91 | list->toString(); 92 | 93 | } 94 | 95 | /** 96 | 单向链表不带虚拟节点 97 | */ 98 | void test2() { 99 | 100 | Person *p1 = new Person(string("dzb"),20); 101 | Person *p2 = new Person(string("james"),24); 102 | Person *p3 = new Person(string("kobe"),24); 103 | SingleLinkedList *list = new SingleLinkedList(); 104 | list->add(p1); 105 | list->add(p2); 106 | list->add(p3); 107 | list->for_each_elements([](Person *person){ 108 | cout << person->name << " age " << person->age << endl; 109 | }); 110 | //在这个拉姆达表达式内部可以做内存释放工作 只有new出来的内存 才需要释放内存 111 | list->clear_with_completion([](Person *person){ 112 | if (person != NULL) { 113 | delete person; 114 | } 115 | }); 116 | 117 | list->add(p1); 118 | list->add(p2); 119 | list->add(p3); 120 | list->toString(); 121 | 122 | ///自定义判断添加规则 重载 == 操作符 123 | int index = list->indexOf(p3); 124 | cout << "p3 index is " << index << endl; 125 | 126 | Person *e = list->remove(0); 127 | cout << e->name << endl; 128 | delete list; 129 | 130 | } 131 | 132 | ///单向链表带虚拟头节点 133 | void test3() { 134 | 135 | // 建议 请勿使用堆空间 指向栈空间内存 除非在函数作用域结束后 不再使用链表 136 | // 往里边内部添加元素 尽量使用指针类型 137 | 138 | SingleLinkedList2 *list = new SingleLinkedList2(); 139 | list->add(10); 140 | list->add(20); 141 | list->add(30); 142 | //获取 143 | int a = list->get(2); 144 | cout << "a = " << a << endl; 145 | list->for_each_elements([](int element){ 146 | cout << element << endl; 147 | }); 148 | // int a = list->get(0); 149 | // cout << a << endl; 150 | //在这个拉姆达表达式内部可以做内存释放工作 只有new出来的内存 才需要释放内存 151 | list->clear_with_completion([](int element){ 152 | 153 | }); 154 | list->add(100); 155 | list->add(200); 156 | list->add(350); 157 | list->toString(); 158 | 159 | 160 | int e = list->remove(0); 161 | cout << e << endl; 162 | delete list; 163 | 164 | } 165 | 166 | int main(int argc, const char * argv[]) { 167 | // 168 | // test1(); 169 | //// test2(); 170 | // test3(); 171 | test4(); 172 | 173 | return 0; 174 | } 175 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day5/02-Array-Stack/02-Array-Stack.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day5/02-Array-Stack/02-Array-Stack/ArrayList.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ArrayList.hpp 3 | // 02-Create-Our-Own-Array 4 | // 5 | // Created by dzb on 2019/2/27. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #ifndef ArrayList_hpp 10 | #define ArrayList_hpp 11 | 12 | #include 13 | #include 14 | 15 | #pragma mark - ArrayList interface 16 | 17 | template 18 | class ArrayList { 19 | 20 | private: 21 | T *_data; /// int型指针数组 22 | int _size; ///数组元素个数 23 | int _capacity; ///数组容量 24 | // 将数组空间的容量变成newCapacity大小 25 | void resize(int newCapacity); 26 | public: 27 | ///构造方法 28 | explicit ArrayList(); 29 | explicit ArrayList(int capacity); 30 | ///析构函数 31 | ~ArrayList(); 32 | // 获取数组的容量 33 | int getCapacity(); 34 | ///获取数组中的元素个数 35 | int getSize(); 36 | // 返回数组是否为空 37 | bool isEmpty(); 38 | // 向所有元素后添加一个新元素 39 | void addLast(T element); 40 | // 在所有元素前添加一个新元素 41 | void addFirst(T element); 42 | /// 在数组任意位置插入一个元素 43 | void insert(T element,int index); 44 | /// 获取index索引位置的元素 45 | T objectAtIndex(int index); 46 | /// 获取数组最后一个元素 47 | T getLast(); 48 | /// 获取数组首元素 49 | T getFirst(); 50 | /// 修改index索引位置的元素为e 51 | void setObject(T element,int index); 52 | // 查找数组中是否有元素e 53 | bool contains(T e); 54 | // 查找数组中元素e所在的索引,如果不存在元素e,则返回-1 55 | int find(T e); 56 | // 从数组中删除index位置的元素, 返回删除的元素 57 | T remove(int index); 58 | // 从数组中删除第一个元素, 返回删除的元素 59 | T removeFirst(); 60 | // 从数组中删除最后一个元素, 返回删除的元素 61 | T removeLast(); 62 | // 从数组中删除元素e 63 | void removeElement(T e); 64 | ///打印内部元素 65 | void print(); 66 | }; 67 | 68 | #pragma mark - ArrayList implementation 69 | 70 | using namespace std; 71 | 72 | template 73 | /** 74 | 默认数组容量是10 75 | */ 76 | ArrayList::ArrayList() : ArrayList(10) { 77 | cout << "ArrayList()构造函数" << endl; 78 | } 79 | 80 | template 81 | ArrayList ::ArrayList(int capacity) { 82 | _capacity = capacity; 83 | _size = 0; 84 | _data = new T[capacity](); 85 | cout << "ArrayList(int capacity)构造函数" << endl; 86 | } 87 | 88 | template 89 | int ArrayList::getCapacity() { 90 | return _capacity; 91 | } 92 | 93 | template 94 | int ArrayList::getSize() { 95 | return _size; 96 | } 97 | 98 | template 99 | bool ArrayList::isEmpty() { 100 | return _size == 0; 101 | } 102 | 103 | template 104 | ArrayList::~ArrayList() { 105 | cout << "~ArrayList()析构函数" << endl; 106 | if (_data != nullptr) { 107 | delete [] _data; 108 | _data = nullptr; 109 | } 110 | } 111 | 112 | #pragma mark - 添加元素 113 | template 114 | // 向所有元素后添加一个新元素 115 | void ArrayList::addLast(T element) { 116 | insert(element,_size); 117 | } 118 | template 119 | // 在所有元素前添加一个新元素 120 | void ArrayList::addFirst(T element) { 121 | insert(element,0); 122 | } 123 | 124 | template 125 | void ArrayList::insert(T element,int index) { 126 | 127 | if(index < 0 || index > _size) { 128 | throw "Add failed. Require index >= 0 and index <= size."; 129 | } 130 | 131 | if (_size == _capacity) { 132 | resize(2 * _capacity); 133 | } 134 | 135 | ///交换索引位置 移动数组索引位置 主要是针对往数组中间位置插入元素时候 需要移动元素位置 136 | for(int i = ( _size - 1);(i >= index && i >= 0); i--) { 137 | _data[i + 1] = _data[i]; 138 | } 139 | 140 | _data[index] = element; 141 | _size ++; 142 | } 143 | 144 | template 145 | /// 修改index索引位置的元素为e 146 | void ArrayList::setObject(T element, int index) { 147 | if(index < 0 || index >= _size) { 148 | throw "Set failed. Index is illegal."; 149 | } 150 | _data[index] = element; 151 | } 152 | #pragma mark - 查找 153 | 154 | /// 获取数组最后一个元素 155 | template 156 | T ArrayList::getLast() { 157 | return objectAtIndex(_size); 158 | } 159 | 160 | /// 获取数组首元素 161 | template 162 | T ArrayList::getFirst() { 163 | return objectAtIndex(0); 164 | } 165 | 166 | template 167 | // 查找数组中是否有元素e 168 | bool ArrayList::contains(T e) { 169 | for(int i = 0 ; i < _size ; i ++){ 170 | if(_data[i] == e) 171 | return true; 172 | } 173 | return false; 174 | } 175 | 176 | template 177 | // 查找数组中元素e所在的索引,如果不存在元素e,则返回-1 178 | int ArrayList::find(T e) { 179 | for(int i = 0 ; i < _size ; i ++){ 180 | if(_data[i] == e) 181 | return i; 182 | } 183 | return -1; 184 | } 185 | 186 | template 187 | /// 获取index索引位置的元素 188 | T ArrayList::objectAtIndex(int index) { 189 | if (index < 0 || index >= _size) { 190 | throw "Get failed. Index is illegal." ; 191 | } 192 | return _data[index]; 193 | } 194 | 195 | #pragma mark - 删除 196 | template 197 | // 从数组中删除index位置的元素, 返回删除的元素 198 | T ArrayList::remove(int index) { 199 | if(index < 0 || index >= _size) { 200 | throw "Remove failed. Index is illegal." ; 201 | } 202 | T ret = _data[index]; 203 | for(int i = index + 1 ; i < _size ; i ++) 204 | _data[i - 1] = _data[i]; 205 | _size --; 206 | ///数组的缩容操作 207 | if(_size == _capacity / 4 && _capacity / 2 != 0) { 208 | resize(_capacity / 2); 209 | } 210 | return ret; 211 | } 212 | template 213 | // 从数组中删除第一个元素, 返回删除的元素 214 | T ArrayList::removeFirst() { 215 | return remove(0); 216 | } 217 | template 218 | // 从数组中删除最后一个元素, 返回删除的元素 219 | T ArrayList::removeLast() { 220 | return remove(_size-1); 221 | } 222 | 223 | template 224 | // 从数组中删除元素e 225 | void ArrayList::removeElement(T e) { 226 | int index = find(e); 227 | if(index != -1) 228 | remove(index); 229 | } 230 | 231 | template 232 | void ArrayList::print() { 233 | std::cout << "["; 234 | for (int i = 0; i < _size; ++i) { 235 | T t = _data[i]; 236 | std::cout << t; 237 | if (i != _size - 1) { 238 | std::cout << ", "; 239 | } 240 | } 241 | std::cout << "]" << endl; 242 | } 243 | 244 | template 245 | 246 | // 将数组空间的容量变成newCapacity大小 247 | void ArrayList::resize(int newCapacity){ 248 | T *oldData = _data; ///旧数据 249 | T *newData = new T[newCapacity](); ///新数组 250 | 251 | size_t size = sizeof(T) * _size; 252 | memcpy(newData,oldData,size); ///对旧的数组进行值的拷贝 253 | // 也可以通过便利数组 将旧数组的值 拷贝到新数组中 254 | // for(int i = 0 ; i < _size ; i ++) 255 | // newData[i] = oldData[i]; 256 | // 257 | delete [] oldData; 258 | _data = newData; 259 | _capacity = newCapacity; 260 | } 261 | 262 | 263 | #endif /* ArrayList_hpp */ 264 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day5/02-Array-Stack/02-Array-Stack/ArrayStack.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ArrayStack.hpp 3 | // 02-Array-Stack 4 | // 5 | // Created by dzb on 2019/3/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #ifndef ArrayStack_hpp 10 | #define ArrayStack_hpp 11 | 12 | #include 13 | #include "Stack.hpp" 14 | #include "ArrayList.hpp" 15 | 16 | 17 | template 18 | class ArrayStack : public Stack { 19 | private: 20 | ArrayList *array; 21 | public: 22 | ArrayStack(); 23 | ArrayStack(int capacity); 24 | ~ArrayStack(); 25 | int getSize(); 26 | int getCapacity(); 27 | bool isEmpty(); 28 | void push(E e); 29 | E pop(); 30 | E peek(); 31 | ///输出栈中所有元素 32 | void toString(); 33 | }; 34 | #pragma mark 构造 析构函数 35 | template 36 | ArrayStack::ArrayStack() : ArrayStack(10) { 37 | 38 | } 39 | 40 | template 41 | ArrayStack::ArrayStack(int capacity) { 42 | cout<<"ArrayStack::ArrayStack(int capacity) "<(capacity); 44 | } 45 | 46 | template 47 | ArrayStack::~ArrayStack() { 48 | if (array != nullptr) { 49 | delete array; 50 | array = nullptr; 51 | } 52 | cout << "ArrayStack::~ArrayStack()" << endl; 53 | } 54 | 55 | template 56 | int ArrayStack::getSize() { 57 | return array->getSize(); 58 | } 59 | template 60 | bool ArrayStack::isEmpty() { 61 | return array->isEmpty(); 62 | } 63 | 64 | template 65 | int ArrayStack::getCapacity() { 66 | return 0; 67 | } 68 | 69 | template 70 | void ArrayStack::push(E e) { 71 | array->addLast(e); 72 | } 73 | 74 | template 75 | E ArrayStack::pop() { 76 | return array->removeLast(); 77 | } 78 | 79 | template 80 | E ArrayStack::peek() { 81 | return array->getLast(); 82 | } 83 | 84 | template 85 | void ArrayStack::toString() { 86 | cout << "Stack: ["; 87 | for (int i = 0; i getSize(); ++i) { 88 | E t = array->objectAtIndex(i); 89 | if (i != 0) { 90 | cout << ", "; 91 | } 92 | std::cout << t; 93 | } 94 | cout << "] top" << endl; 95 | } 96 | 97 | #endif /* ArrayStack_hpp */ 98 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day5/02-Array-Stack/02-Array-Stack/Stack.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stack.hpp 3 | // 02-Array-Stack 4 | // 5 | // Created by dzb on 2019/3/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #ifndef Stack_hpp 10 | #define Stack_hpp 11 | 12 | #include 13 | 14 | ///Stakc虚基类 15 | template 16 | class Stack { 17 | 18 | public: 19 | virtual int getSize() { return 0; } 20 | virtual bool isEmpty() { return false; } 21 | virtual void push(E e) { } 22 | virtual E pop() { return (E)(0);} 23 | virtual E peek() { return (E)0; } 24 | }; 25 | 26 | 27 | #endif /* Stack_hpp */ 28 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day5/02-Array-Stack/02-Array-Stack/main.mm: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // 02-Array-Stack 4 | // 5 | // Created by dzb on 2019/3/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "ArrayStack.hpp" 11 | #import 12 | 13 | using namespace std; 14 | 15 | int main() { 16 | { 17 | ArrayStack stack(10); 18 | stack.push(10); 19 | stack.push(100); 20 | stack.toString(); 21 | } 22 | getchar(); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day5/02_ArrayList/02_ArrayList.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day5/02_ArrayList/02_ArrayList/Array.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Array.hpp 3 | // 02_ArrayList1 4 | // 5 | // Created by dzb on 2019/4/17. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #ifndef Array_hpp 10 | #define Array_hpp 11 | 12 | #include 13 | 14 | 15 | static int DEFAULT_CAPACITY = 10; 16 | static int ELEMENT_NOT_FOUND = 10; 17 | 18 | 19 | ///Array 接口类 不可被直接拿来使用 定义了 Array 的一些接口 20 | ///子类才用共用基础方式 重新虚函数 21 | template 22 | class Array { 23 | 24 | public: 25 | /** 26 | * 清除所有元素 27 | */ 28 | virtual void clear_with_completion(void(*for_each)(E)); 29 | /** 30 | * 元素的数量 31 | */ 32 | virtual int size(); 33 | /** 34 | * 是否为空 35 | */ 36 | virtual bool isEmpty(); 37 | 38 | /** 39 | * 是否包含某个元素 40 | */ 41 | virtual bool contains(E element); 42 | 43 | /** 44 | * 添加元素到尾部 45 | */ 46 | virtual void add(E element); 47 | 48 | /** 49 | * 获取index位置的元素 50 | */ 51 | virtual E get(int index); 52 | 53 | /** 54 | * 设置index位置的元素 55 | * @return 原来的元素ֵ 56 | */ 57 | virtual E set(int index, E element); 58 | 59 | /** 60 | * 在index位置插入一个元素 61 | */ 62 | virtual void add(int index, E element); 63 | 64 | /** 65 | * 删除index位置的元素 66 | */ 67 | virtual E remove(int index); 68 | /** 69 | * 查看元素的索引 70 | */ 71 | virtual int indexOf(E element); 72 | //打印数组元素 73 | virtual void toString(); 74 | }; 75 | 76 | /** 77 | * 清除所有元素 78 | */ 79 | template 80 | void Array::clear_with_completion(void(*for_each)(E)) { 81 | 82 | } 83 | 84 | /** 85 | * 元素的数量 86 | */ 87 | template 88 | int Array::size() { 89 | return 0; 90 | } 91 | 92 | /** 93 | * 是否为空 94 | */ 95 | template 96 | bool Array::isEmpty() { 97 | return true; 98 | } 99 | 100 | 101 | /** 102 | * 是否包含某个元素 103 | */ 104 | template 105 | bool Array::contains(E element) { 106 | return false; 107 | } 108 | 109 | /** 110 | * 添加元素到尾部 111 | */ 112 | template 113 | void Array::add(E element) { 114 | 115 | } 116 | 117 | /** 118 | * 获取index位置的元素 119 | */ 120 | template 121 | E Array::get(int index) { 122 | return 0; 123 | } 124 | 125 | /** 126 | * 设置index位置的元素 127 | * @return 原来的元素ֵ 128 | */ 129 | template 130 | E Array::set(int index, E element) { 131 | return 0; 132 | } 133 | 134 | /** 135 | * 在index位置插入一个元素 136 | */ 137 | template 138 | void Array::add(int index, E element) { 139 | 140 | } 141 | 142 | /** 143 | * 删除index位置的元素 144 | */ 145 | template 146 | E Array::remove(int index) { 147 | return NULL; 148 | } 149 | /** 150 | * 查看元素的索引 151 | */ 152 | template 153 | int Array::indexOf(E element) { 154 | return 0; 155 | } 156 | 157 | ///打印数组内容 158 | template 159 | void Array::toString() { 160 | 161 | } 162 | 163 | 164 | #endif /* Array_hpp */ 165 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day5/02_ArrayList/02_ArrayList/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // 02_ArrayList1 4 | // 5 | // Created by dzb on 2019/4/15. 6 | // Copyright © 2019年 dzb. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "Array.hpp" 11 | #include "ArrayList1.hpp" 12 | #include "ArrayList2.hpp" 13 | 14 | using namespace std; 15 | 16 | //Person类 17 | class Person { 18 | private: 19 | int m_age; 20 | char *m_name; 21 | 22 | public: 23 | Person(int age,char *name) : m_age(age),m_name(name) { 24 | 25 | } 26 | ~Person() { 27 | cout << "~Person()" << endl; 28 | } 29 | void toString(); 30 | }; 31 | 32 | //基本类型包装类 33 | template 34 | class NSNumber { 35 | private: 36 | T _data; 37 | public: 38 | NSNumber(T data) : _data(data) { 39 | 40 | } 41 | ~NSNumber() { 42 | cout << "~NSNumber()" << endl; 43 | } 44 | int intValue() { 45 | return (int)_data; 46 | } 47 | double doubleValue() { 48 | return (double)_data; 49 | } 50 | bool boolValue() { 51 | return (bool)_data; 52 | } 53 | 54 | }; 55 | 56 | 57 | void test1() { 58 | // int -> Integer 59 | 60 | // 所有的类,最终都继承java.lang.Object 61 | 62 | // new是向堆空间申请内存 63 | ArrayList1 *persons = new ArrayList1(); 64 | persons->add(new Person(10,"Jack")); 65 | persons->add(new Person(15,"Rose")); 66 | persons->add(new Person(12,"James")); 67 | persons->clear_with_completion([](Person *p){ 68 | delete p; 69 | }); 70 | persons->add(new Person(22,"abc")); 71 | persons->toString(); 72 | 73 | ArrayList1*> *ints = new ArrayList1*>(); 74 | ints->add(new NSNumber(10)); 75 | ints->add(new NSNumber(10)); 76 | ints->add(new NSNumber(22)); 77 | ints->add(new NSNumber(33)); 78 | 79 | ints->toString(); 80 | 81 | ints->clear_with_completion([](NSNumber *number){ 82 | delete number; 83 | }); 84 | 85 | delete ints; 86 | 87 | } 88 | 89 | void test2() { 90 | 91 | ArrayList1 *persons = new ArrayList1(); 92 | persons->add(new Person(10,"Jack")); 93 | 94 | persons->add(nullptr); 95 | persons->add(new Person(15,"Rose")); 96 | persons->add(nullptr); 97 | persons->add(new Person(12,"James")); 98 | persons->add(nullptr); 99 | 100 | persons->toString(); 101 | 102 | delete persons; 103 | 104 | } 105 | 106 | void test3() { 107 | 108 | ArrayList2 *list2 = new ArrayList2(); 109 | for (int i = 0; i<100; i++) { 110 | list2->add(i); 111 | } 112 | 113 | for (int i = 0; i<100; i++) { 114 | int ret = list2->remove(0); 115 | cout << ret << endl; 116 | } 117 | 118 | delete list2; 119 | 120 | } 121 | 122 | int main(int argc, const char * argv[]) { 123 | // 124 | // test1(); 125 | // test2(); 126 | // 127 | test3(); 128 | 129 | return 0; 130 | } 131 | 132 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day5/LinkedList/LinkedList.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day5/LinkedList/LinkedList/Circle/SingleCircleLinkedList.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // SingleCircleLinkedList.hpp 3 | // SingleCircleLinkedList 4 | // 5 | // Created by dzb on 2019/4/28. 6 | // Copyright © 2019 dzb. All rights reserved. 7 | // 8 | 9 | #ifndef SingleCircleLinkedList_hpp 10 | #define SingleCircleLinkedList_hpp 11 | 12 | #include 13 | 14 | using namespace std; 15 | 16 | static const int CIRCLE_ELEMENT_NOT_FOUND = -1; 17 | 18 | template 19 | //双向循环链表 20 | class SingleCircleLinkedList { 21 | 22 | class Node { //内部类 23 | public: 24 | E m_element; 25 | Node *m_next; //后驱 26 | //无参构造方法 27 | Node() : Node(NULL,NULL,NULL){} 28 | //有参构造方法 29 | Node(E element,Node *next,Node *pre) : m_element(element),m_next(next) { 30 | } 31 | //析构方法 32 | ~Node() { 33 | cout<<"~Node()" << endl; 34 | } 35 | }; 36 | 37 | public: 38 | //构造方法 39 | SingleCircleLinkedList(); 40 | //析构方法 41 | ~SingleCircleLinkedList(); 42 | /** 43 | * 元素的数量 44 | */ 45 | int size(); 46 | 47 | /** 48 | * 是否为空 49 | */ 50 | bool isEmpty(); 51 | 52 | /** 53 | * 是否包含某个元素 54 | */ 55 | bool contains(E element); 56 | 57 | /** 58 | * 添加元素到尾部 59 | */ 60 | void add(E element); 61 | 62 | /** 63 | * 获取index位置的元素 64 | */ 65 | E get(int index); 66 | 67 | /** 68 | * 设置index位置的元素 69 | */ 70 | E set(int index, E element); 71 | 72 | /** 73 | * 在index位置插入一个元素 74 | */ 75 | void add(int index, E element); 76 | 77 | /** 78 | * 删除index位置的元素 79 | */ 80 | E remove(int index); 81 | 82 | /** 83 | * 查看元素的索引 84 | */ 85 | int indexOf(E element); 86 | ///遍历所有元素 87 | void for_each_elements(void(*for_each)(E)); 88 | ///销毁链表的元素 89 | void clear_with_completion(void(*for_each)(E)); 90 | //打印链表元素 91 | void toString(); 92 | 93 | private: 94 | Node *_header; 95 | int m_size; 96 | void outOfBounds(int index) { 97 | throw "Index out of bounds"; 98 | } 99 | void rangeCheck(int index) { 100 | if (index < 0 || index >= m_size) { 101 | outOfBounds(index); 102 | } 103 | } 104 | void rangeCheckForAdd(int index) { 105 | if (index < 0 || index > m_size) { 106 | outOfBounds(index); 107 | } 108 | } 109 | Node *node(int index) { 110 | SingleCircleLinkedList::Node *cur = _header->m_next; 111 | for (int i = 0; i < index; i++) { 112 | cur = cur->m_next; 113 | } 114 | return cur; 115 | } 116 | }; 117 | 118 | //无参构造函数 119 | template 120 | SingleCircleLinkedList::SingleCircleLinkedList() { 121 | this->m_size = 0; 122 | this->_header = new SingleCircleLinkedList::Node(); 123 | _header->m_next = _header; 124 | } 125 | 126 | //析构函数 127 | template 128 | SingleCircleLinkedList::~SingleCircleLinkedList() { 129 | cout<<"~SingleCircleLinkedList()"<clear_with_completion(NULL); 132 | } 133 | 134 | /** 135 | * 元素的数量 136 | */ 137 | template 138 | int SingleCircleLinkedList::size() { 139 | return m_size; 140 | } 141 | 142 | /** 143 | * 是否为空 144 | */ 145 | template 146 | bool SingleCircleLinkedList::isEmpty() { 147 | return m_size == 0; 148 | } 149 | 150 | /** 151 | * 是否包含某个元素 152 | */ 153 | template 154 | bool SingleCircleLinkedList::contains(E element) { 155 | return indexOf(element) != ELEMENT_NOT_FOUND; 156 | } 157 | 158 | /** 159 | * 添加元素到尾部 160 | */ 161 | template 162 | void SingleCircleLinkedList::add(E element) { 163 | add(m_size,element); 164 | } 165 | 166 | 167 | /** 168 | * 获取index位置的元素 169 | */ 170 | template 171 | E SingleCircleLinkedList::get(int index) { 172 | rangeCheck(index); 173 | SingleCircleLinkedList::Node *pFind = node(index); 174 | E element = pFind->m_element; 175 | return element; 176 | } 177 | 178 | /** 179 | * 设置index位置的元素 返回上一个值 需要开发者自己拿到这个值后管理内存 180 | */ 181 | template 182 | E SingleCircleLinkedList::set(int index, E element) { 183 | rangeCheck(index); 184 | SingleCircleLinkedList::Node *pFind = node(index); 185 | E old = pFind->m_element; 186 | pFind->m_element = element; 187 | return old; 188 | } 189 | 190 | /** 191 | * 在index位置插入一个元素 192 | */ 193 | template 194 | void SingleCircleLinkedList::add(int index, E element) { 195 | rangeCheckForAdd(index); 196 | //把双向循环链表 想象成汉堡包🍔 就不难立即 add 方法怎么实现 197 | SingleCircleLinkedList::Node *cur = new SingleCircleLinkedList::Node(); 198 | cur->m_element = element; 199 | cur->m_next = _header->m_next; 200 | _header->m_next = cur; 201 | m_size++; 202 | } 203 | 204 | /** 205 | * 删除index位置的元素 206 | */ 207 | template 208 | E SingleCircleLinkedList::remove(int index) { 209 | rangeCheck(index); 210 | SingleCircleLinkedList::Node *prev = node(index-1); ///要删除节点 211 | SingleCircleLinkedList::Node *cur = prev->m_next; ///要删除节点的前驱 212 | prev->m_next = cur->m_next; 213 | E element = cur->m_element; //要删除的元素 214 | delete cur; //删除节点 对于节点数据需要开发者自行管理内存 215 | m_size--; 216 | return element; 217 | } 218 | 219 | /** 220 | * 查看元素的索引 221 | */ 222 | template 223 | int SingleCircleLinkedList::indexOf(E element) { 224 | SingleCircleLinkedList::Node *cur = _header->m_next; 225 | for (int i = 0; im_element) { 227 | return i; 228 | } 229 | cur = cur->m_next; 230 | } 231 | return ELEMENT_NOT_FOUND; 232 | } 233 | 234 | ///遍历所有元素 235 | template 236 | void SingleCircleLinkedList::for_each_elements(void(*for_each)(E)) { 237 | SingleCircleLinkedList::Node *sh = _header->m_next; 238 | while (sh != _header) { 239 | E obj = sh->m_element; 240 | sh = sh->m_next; 241 | if (for_each != NULL) { 242 | (*for_each)(obj); 243 | } 244 | } 245 | } 246 | ///销毁链表的元素 247 | template 248 | void SingleCircleLinkedList::clear_with_completion(void(*for_each)(E)) { 249 | if (size() == 0) return; 250 | while (m_size > 0) { 251 | E old = remove(m_size-1); 252 | if (for_each != NULL) { 253 | (*for_each)(old); 254 | } 255 | } 256 | } 257 | 258 | //打印元素 259 | template 260 | void SingleCircleLinkedList::toString() { 261 | std::cout << "[ "; 262 | for (int i = 0; i 13 | #include 14 | using namespace std; 15 | 16 | static const int ELEMENT_NOT_FOUND = -1; 17 | 18 | template 19 | //双向循环链表 20 | class LinkedList { 21 | 22 | class Node { //内部类 23 | public: 24 | E m_element; 25 | Node *m_next; //后驱 26 | Node *m_prev; //前驱 27 | //无参构造方法 28 | Node() : Node(NULL,NULL,NULL){} 29 | //有参构造方法 30 | Node(E element,Node *next,Node *pre) : m_element(element),m_next(next),m_prev(pre) { 31 | } 32 | //析构方法 33 | ~Node() { 34 | cout<<"~Node()" << endl; 35 | } 36 | }; 37 | 38 | public: 39 | //构造方法 40 | LinkedList(); 41 | //析构方法 42 | ~LinkedList(); 43 | /** 44 | * 元素的数量 45 | */ 46 | int size(); 47 | 48 | /** 49 | * 是否为空 50 | */ 51 | bool isEmpty(); 52 | 53 | /** 54 | * 是否包含某个元素 55 | */ 56 | bool contains(E element); 57 | 58 | /** 59 | * 添加元素到尾部 60 | */ 61 | void add(E element); 62 | 63 | /** 64 | * 获取index位置的元素 65 | */ 66 | E get(int index); 67 | 68 | /** 69 | * 设置index位置的元素 70 | */ 71 | E set(int index, E element); 72 | 73 | /** 74 | * 在index位置插入一个元素 75 | */ 76 | void add(int index, E element); 77 | 78 | /** 79 | * 删除index位置的元素 80 | */ 81 | E remove(int index); 82 | 83 | /** 84 | * 查看元素的索引 85 | */ 86 | int indexOf(E element); 87 | ///遍历所有元素 88 | void for_each_elements(void(*for_each)(E)); 89 | ///销毁链表的元素 90 | void clear_with_completion(void(*for_each)(E)); 91 | //打印链表元素 92 | void toString(); 93 | 94 | private: 95 | Node *_header; 96 | int m_size; 97 | void outOfBounds(int index) { 98 | throw "Index out of bounds"; 99 | } 100 | void rangeCheck(int index) { 101 | if (index < 0 || index >= m_size) { 102 | outOfBounds(index); 103 | } 104 | } 105 | void rangeCheckForAdd(int index) { 106 | if (index < 0 || index > m_size) { 107 | outOfBounds(index); 108 | } 109 | } 110 | Node *node(int index) { 111 | 112 | if (index < (m_size >> 1)) { //从前便利 113 | LinkedList::Node *cur = _header->m_next; 114 | for (int i = 0; i < index; i++) { 115 | cur = cur->m_next; 116 | } 117 | return cur; 118 | } else { ///从后遍历 119 | LinkedList::Node *cur = _header->m_prev; 120 | for (int i = m_size - 1; i > index; i--) { 121 | cur = cur->m_prev; 122 | } 123 | return cur; 124 | } 125 | 126 | } 127 | }; 128 | 129 | //无参构造函数 130 | template 131 | LinkedList::LinkedList() { 132 | this->m_size = 0; 133 | this->_header = new LinkedList::Node(); 134 | _header->m_next = _header->m_prev = _header; 135 | } 136 | 137 | //析构函数 138 | template 139 | LinkedList::~LinkedList() { 140 | cout<<"~LinkedList()"<clear_with_completion(NULL); 143 | } 144 | 145 | /** 146 | * 元素的数量 147 | */ 148 | template 149 | int LinkedList::size() { 150 | return m_size; 151 | } 152 | 153 | /** 154 | * 是否为空 155 | */ 156 | template 157 | bool LinkedList::isEmpty() { 158 | return m_size == 0; 159 | } 160 | 161 | /** 162 | * 是否包含某个元素 163 | */ 164 | template 165 | bool LinkedList::contains(E element) { 166 | return indexOf(element) != ELEMENT_NOT_FOUND; 167 | } 168 | 169 | /** 170 | * 添加元素到尾部 171 | */ 172 | template 173 | void LinkedList::add(E element) { 174 | add(m_size,element); 175 | } 176 | 177 | 178 | /** 179 | * 获取index位置的元素 180 | */ 181 | template 182 | E LinkedList::get(int index) { 183 | rangeCheck(index); 184 | LinkedList::Node *pFind = node(index); 185 | E element = pFind->m_element; 186 | return element; 187 | } 188 | 189 | /** 190 | * 设置index位置的元素 返回上一个值 需要开发者自己拿到这个值后管理内存 191 | */ 192 | template 193 | E LinkedList::set(int index, E element) { 194 | rangeCheck(index); 195 | LinkedList::Node *pFind = node(index); 196 | E old = pFind->m_element; 197 | pFind->m_element = element; 198 | return old; 199 | } 200 | 201 | /** 202 | * 在index位置插入一个元素 203 | */ 204 | template 205 | void LinkedList::add(int index, E element) { 206 | rangeCheckForAdd(index); 207 | //把双向循环链表 想象成汉堡包🍔 就不难立即 add 方法怎么实现 208 | LinkedList::Node *cur = new LinkedList::Node(); 209 | cur->m_element = element; 210 | cur->m_next = _header->m_next; 211 | _header->m_next = cur; 212 | cur->m_prev = _header; 213 | cur->m_next->m_prev = cur; 214 | m_size++; 215 | } 216 | 217 | /** 218 | * 删除index位置的元素 219 | */ 220 | template 221 | E LinkedList::remove(int index) { 222 | rangeCheck(index); 223 | LinkedList::Node *cur = node(index); ///要删除节点 224 | LinkedList::Node *prev = cur->m_prev; ///要删除节点的前驱 225 | prev->m_next = cur->m_next; 226 | prev->m_next->m_prev = prev; 227 | E element = cur->m_element; //要删除的元素 228 | delete cur; //删除节点 对于节点数据需要开发者自行管理内存 229 | m_size--; 230 | return element; 231 | } 232 | 233 | /** 234 | * 查看元素的索引 235 | */ 236 | template 237 | int LinkedList::indexOf(E element) { 238 | LinkedList::Node *cur = _header->m_next; 239 | for (int i = 0; im_element) { 241 | return i; 242 | } 243 | cur = cur->m_next; 244 | } 245 | return ELEMENT_NOT_FOUND; 246 | } 247 | 248 | ///遍历所有元素 249 | template 250 | void LinkedList::for_each_elements(void(*for_each)(E)) { 251 | LinkedList::Node *sh = _header->m_next; 252 | while (sh != _header) { 253 | E obj = sh->m_element; 254 | sh = sh->m_next; 255 | if (for_each != NULL) { 256 | (*for_each)(obj); 257 | } 258 | } 259 | } 260 | ///销毁链表的元素 261 | template 262 | void LinkedList::clear_with_completion(void(*for_each)(E)) { 263 | if (size() == 0) return; 264 | while (m_size > 0) { 265 | E old = remove(m_size-1); 266 | if (for_each != NULL) { 267 | (*for_each)(old); 268 | } 269 | } 270 | } 271 | 272 | //打印元素 273 | template 274 | void LinkedList::toString() { 275 | std::cout << "[ "; 276 | for (int i = 0; i 13 | #include 14 | using namespace std; 15 | 16 | static const int SL_ELEMENT_NOT_FOUND2 = -1; 17 | 18 | template 19 | //单向链表采用尾插法 20 | /** 21 | * 增加一个虚拟头结点 22 | * @author MJ Lee 23 | * 24 | */ 25 | class SingleLinkedList2 { 26 | 27 | class Node { //内部类 28 | public: 29 | E m_element; 30 | Node *m_next; 31 | //无参构造方法 32 | Node() : Node(NULL,NULL){} 33 | //有参构造方法 34 | Node(E element,Node *next) : m_element(element),m_next(next) { 35 | } 36 | //析构方法 37 | ~Node() { 38 | cout<<"~Node()" << endl; 39 | } 40 | }; 41 | 42 | public: 43 | //构造方法 44 | SingleLinkedList2(); 45 | //析构方法 46 | ~SingleLinkedList2(); 47 | /** 48 | * 元素的数量 49 | */ 50 | int size(); 51 | 52 | /** 53 | * 是否为空 54 | */ 55 | bool isEmpty(); 56 | 57 | /** 58 | * 是否包含某个元素 59 | */ 60 | bool contains(E element); 61 | 62 | /** 63 | * 添加元素到尾部 64 | */ 65 | void add(E element); 66 | 67 | /** 68 | * 获取index位置的元素 69 | */ 70 | E get(int index); 71 | 72 | /** 73 | * 设置index位置的元素 74 | */ 75 | E set(int index, E element); 76 | 77 | /** 78 | * 在index位置插入一个元素 79 | */ 80 | void add(int index, E element); 81 | 82 | /** 83 | * 删除index位置的元素 84 | */ 85 | E remove(int index); 86 | 87 | /** 88 | * 查看元素的索引 89 | */ 90 | int indexOf(E element); 91 | ///遍历所有元素 92 | void for_each_elements(void(*for_each)(E)); 93 | ///销毁链表的元素 94 | void clear_with_completion(void(*for_each)(E)); 95 | //打印链表元素 96 | void toString(); 97 | 98 | private: 99 | Node *_dummyHead; 100 | int m_size; 101 | void outOfBounds(int index) { 102 | throw "Index out of bounds"; 103 | } 104 | void rangeCheck(int index) { 105 | if (index < 0 || index >= m_size) { 106 | outOfBounds(index); 107 | } 108 | } 109 | void rangeCheckForAdd(int index) { 110 | if (index < 0 || index > m_size) { 111 | outOfBounds(index); 112 | } 113 | } 114 | Node *node(int index,Node *dummyHead) { 115 | SingleLinkedList2::Node *prev = dummyHead; 116 | for (int i = 0; im_next; 118 | } 119 | return prev; 120 | } 121 | }; 122 | 123 | //无参构造函数 124 | template 125 | SingleLinkedList2::SingleLinkedList2() { 126 | this->_dummyHead = new SingleLinkedList2::Node(); 127 | this->m_size = 0; 128 | } 129 | 130 | //析构函数 131 | template 132 | SingleLinkedList2::~SingleLinkedList2() { 133 | cout<<"~SingleLinkedList2()"<clear_with_completion(NULL); 136 | } 137 | 138 | /** 139 | * 元素的数量 140 | */ 141 | template 142 | int SingleLinkedList2::size() { 143 | return m_size; 144 | } 145 | 146 | /** 147 | * 是否为空 148 | */ 149 | template 150 | bool SingleLinkedList2::isEmpty() { 151 | return m_size == 0; 152 | } 153 | 154 | /** 155 | * 是否包含某个元素 156 | */ 157 | template 158 | bool SingleLinkedList2::contains(E element) { 159 | return indexOf(element) != SL_ELEMENT_NOT_FOUND2; 160 | } 161 | 162 | /** 163 | * 添加元素到尾部 164 | */ 165 | template 166 | void SingleLinkedList2::add(E element) { 167 | add(m_size,element); 168 | } 169 | 170 | 171 | /** 172 | * 获取index位置的元素 173 | */ 174 | template 175 | E SingleLinkedList2::get(int index) { 176 | rangeCheck(index); 177 | SingleLinkedList2::Node *pFind = node(index,_dummyHead->m_next); 178 | E element = pFind->m_element; 179 | return element; 180 | } 181 | 182 | /** 183 | * 设置index位置的元素 返回上一个值 需要开发者自己拿到这个值后管理内存 184 | */ 185 | template 186 | E SingleLinkedList2::set(int index, E element) { 187 | rangeCheck(index); 188 | SingleLinkedList2::Node *pFind = node(index,_dummyHead->m_next); 189 | E old = pFind->m_element; 190 | pFind->m_element = element; 191 | return old; 192 | } 193 | 194 | /** 195 | * 在index位置插入一个元素 196 | */ 197 | template 198 | void SingleLinkedList2::add(int index, E element) { 199 | rangeCheckForAdd(index); 200 | 201 | Node *prev = node(index,_dummyHead); 202 | SingleLinkedList2::Node *cur = new SingleLinkedList2::Node(element,prev->m_next); 203 | prev->m_next = cur; 204 | 205 | m_size++; 206 | } 207 | 208 | /** 209 | * 删除index位置的元素 210 | */ 211 | template 212 | E SingleLinkedList2::remove(int index) { 213 | rangeCheck(index); 214 | E element; //要删除的元素 215 | SingleLinkedList2::Node *prev = node(index,_dummyHead); //要删除节点前驱 216 | SingleLinkedList2::Node *cur = prev->m_next; //要删除的节点 217 | prev->m_next = cur->m_next; 218 | element = cur->m_element; 219 | m_size--; 220 | delete cur; //删除节点 对于节点数据需要开发者自行管理内存 221 | return element; 222 | } 223 | 224 | /** 225 | * 查看元素的索引 226 | */ 227 | template 228 | int SingleLinkedList2::indexOf(E element) { 229 | SingleLinkedList2::Node *cur = _dummyHead->m_next; 230 | for (int i = 0; im_element) { 232 | return i; 233 | } 234 | cur = cur->m_next; 235 | } 236 | return SL_ELEMENT_NOT_FOUND2; 237 | } 238 | 239 | ///遍历所有元素 240 | template 241 | void SingleLinkedList2::for_each_elements(void(*for_each)(E)) { 242 | SingleLinkedList2::Node *sh = _dummyHead->m_next; 243 | while (sh != NULL) { 244 | E obj = sh->m_element; 245 | sh = sh->m_next; 246 | if (for_each != NULL) { 247 | (*for_each)(obj); 248 | } 249 | } 250 | } 251 | ///销毁链表的元素 252 | template 253 | void SingleLinkedList2::clear_with_completion(void(*for_each)(E)) { 254 | if (size() == 0) return; 255 | while (m_size > 0) { 256 | E old = remove(m_size-1); 257 | if (for_each != NULL) { 258 | (*for_each)(old); 259 | } 260 | } 261 | } 262 | 263 | //打印元素 264 | template 265 | void SingleLinkedList2::toString() { 266 | std::cout << "[ "; 267 | for (int i = 0; i 10 | #include 11 | #include "LinkedList.hpp" 12 | #include "CircleLinkedList.hpp" 13 | #include "SingleLinkedList.hpp" 14 | #include "SingleLinkedList2.hpp" 15 | #include "SingleCircleLinkedList.hpp" 16 | 17 | using namespace std; 18 | 19 | class Person { 20 | public: 21 | int age; 22 | string name; 23 | Person(string name,int age) { 24 | this->name = name; 25 | this->age = age; 26 | } 27 | ~Person() { 28 | cout<<"~Person()"<name == this->name && p->age == this->age) { 33 | return true; 34 | } 35 | return false; 36 | } 37 | }; 38 | 39 | 40 | /** 41 | 双向循环链表 42 | */ 43 | void test1() { 44 | 45 | // 建议 请勿使用堆空间 指向栈空间内存 除非在函数作用域结束后 不再使用链表 46 | // 往里边内部添加元素 尽量使用指针类型 47 | 48 | LinkedList *list = new LinkedList(); 49 | list->add(10); 50 | list->add(20); 51 | list->add(30); 52 | list->for_each_elements([](int element){ 53 | cout << element << endl; 54 | }); 55 | // int a = list->get(0); 56 | // cout << a << endl; 57 | //在这个拉姆达表达式内部可以做内存释放工作 只有new出来的内存 才需要释放内存 58 | list->clear_with_completion([](int element){ 59 | 60 | }); 61 | list->add(100); 62 | list->add(200); 63 | list->add(350); 64 | list->toString(); 65 | 66 | int a = list->get(2); 67 | cout << a << endl; 68 | 69 | bool ret = list->contains(33); 70 | cout << ret << endl; 71 | 72 | list->set(0,44); 73 | list->toString(); 74 | 75 | int e = list->remove(0); 76 | cout << e << endl; 77 | delete list; 78 | 79 | } 80 | 81 | 82 | /** 83 | 双向循环链表 测试 2 84 | */ 85 | void test4() { 86 | LinkedList *list = new LinkedList(); 87 | for (int i = 0; i<100; i++) { 88 | list->add(i); 89 | } 90 | list->toString(); 91 | 92 | list->clear_with_completion(NULL); 93 | list->toString(); 94 | 95 | } 96 | 97 | /** 98 | 单向链表不带虚拟节点 99 | */ 100 | void test2() { 101 | 102 | Person *p1 = new Person(string("dzb"),20); 103 | Person *p2 = new Person(string("james"),24); 104 | Person *p3 = new Person(string("kobe"),24); 105 | SingleLinkedList *list = new SingleLinkedList(); 106 | list->add(p1); 107 | list->add(p2); 108 | list->add(p3); 109 | list->for_each_elements([](Person *person){ 110 | cout << person->name << " age " << person->age << endl; 111 | }); 112 | //在这个拉姆达表达式内部可以做内存释放工作 只有new出来的内存 才需要释放内存 113 | list->clear_with_completion([](Person *person){ 114 | if (person != NULL) { 115 | delete person; 116 | } 117 | }); 118 | 119 | list->add(p1); 120 | list->add(p2); 121 | list->add(p3); 122 | list->toString(); 123 | 124 | ///自定义判断添加规则 重载 == 操作符 125 | int index = list->indexOf(p3); 126 | cout << "p3 index is " << index << endl; 127 | 128 | Person *e = list->remove(0); 129 | cout << e->name << endl; 130 | delete list; 131 | 132 | } 133 | 134 | ///单向链表带虚拟头节点 135 | void test3() { 136 | 137 | // 建议 请勿使用堆空间 指向栈空间内存 除非在函数作用域结束后 不再使用链表 138 | // 往里边内部添加元素 尽量使用指针类型 139 | 140 | SingleLinkedList2 *list = new SingleLinkedList2(); 141 | list->add(10); 142 | list->add(20); 143 | list->add(30); 144 | //获取 145 | int a = list->get(2); 146 | cout << "a = " << a << endl; 147 | list->for_each_elements([](int element){ 148 | cout << element << endl; 149 | }); 150 | // int a = list->get(0); 151 | // cout << a << endl; 152 | //在这个拉姆达表达式内部可以做内存释放工作 只有new出来的内存 才需要释放内存 153 | list->clear_with_completion([](int element){ 154 | 155 | }); 156 | list->add(100); 157 | list->add(200); 158 | list->add(350); 159 | list->toString(); 160 | 161 | 162 | int e = list->remove(0); 163 | cout << e << endl; 164 | delete list; 165 | 166 | } 167 | 168 | 169 | /** 170 | 单向链表单向循环 171 | */ 172 | void test5() { 173 | 174 | 175 | // 建议 请勿使用堆空间 指向栈空间内存 除非在函数作用域结束后 不再使用链表 176 | // 往里边内部添加元素 尽量使用指针类型 177 | 178 | SingleCircleLinkedList *list = new SingleCircleLinkedList(); 179 | list->add(10); 180 | list->add(20); 181 | list->add(30); 182 | //获取 183 | int a = list->get(2); 184 | cout << "a = " << a << endl; 185 | list->for_each_elements([](int element){ 186 | cout << element << endl; 187 | }); 188 | // int a = list->get(0); 189 | // cout << a << endl; 190 | //在这个拉姆达表达式内部可以做内存释放工作 只有new出来的内存 才需要释放内存 191 | list->clear_with_completion([](int element){ 192 | 193 | }); 194 | list->add(100); 195 | list->add(200); 196 | list->add(350); 197 | list->toString(); 198 | 199 | 200 | int e = list->remove(0); 201 | cout << e << endl; 202 | delete list; 203 | } 204 | 205 | /** 206 | 双向循环链表 待 current 指针 207 | */ 208 | void test6() { 209 | 210 | // 建议 请勿使用堆空间 指向栈空间内存 除非在函数作用域结束后 不再使用链表 211 | // 往里边内部添加元素 尽量使用指针类型 212 | 213 | CircleLinkedList *list = new CircleLinkedList(); 214 | list->add(10); 215 | list->add(20); 216 | list->add(30); 217 | //获取 218 | int a = list->get(2); 219 | cout << "a = " << a << endl; 220 | list->for_each_elements([](int element){ 221 | cout << element << endl; 222 | }); 223 | // int a = list->get(0); 224 | // cout << a << endl; 225 | //在这个拉姆达表达式内部可以做内存释放工作 只有new出来的内存 才需要释放内存 226 | list->clear_with_completion([](int element){ 227 | 228 | }); 229 | list->add(100); 230 | list->add(200); 231 | list->add(350); 232 | list->toString(); 233 | 234 | 235 | int e = list->remove(0); 236 | cout << e << endl; 237 | delete list; 238 | 239 | } 240 | 241 | int main(int argc, const char * argv[]) { 242 | // 243 | // test1(); 244 | //// test2(); 245 | // test3(); 246 | // test5(); 247 | test6(); 248 | 249 | return 0; 250 | } 251 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day6/02-Array-Stack/02-Array-Stack.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day6/02-Array-Stack/02-Array-Stack/ArrayStack.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ArrayStack.hpp 3 | // 02-Array-Stack 4 | // 5 | // Created by dzb on 2019/3/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #ifndef ArrayStack_hpp 10 | #define ArrayStack_hpp 11 | 12 | #include 13 | #include "Stack.hpp" 14 | #include "ArrayList.hpp" 15 | 16 | 17 | template 18 | class ArrayStack : public Stack { 19 | private: 20 | ArrayList *array; 21 | public: 22 | ArrayStack(); 23 | ArrayStack(int capacity); 24 | ~ArrayStack(); 25 | int getSize(); 26 | int getCapacity(); 27 | bool isEmpty(); 28 | void push(E e); 29 | E pop(); 30 | E peek(); 31 | ///输出栈中所有元素 32 | void toString(); 33 | }; 34 | #pragma mark 构造 析构函数 35 | template 36 | ArrayStack::ArrayStack() : ArrayStack(10) { 37 | 38 | } 39 | 40 | template 41 | ArrayStack::ArrayStack(int capacity) { 42 | cout<<"ArrayStack::ArrayStack(int capacity) "<(capacity); 44 | } 45 | 46 | template 47 | ArrayStack::~ArrayStack() { 48 | if (array != nullptr) { 49 | delete array; 50 | array = nullptr; 51 | } 52 | cout << "ArrayStack::~ArrayStack()" << endl; 53 | } 54 | 55 | template 56 | int ArrayStack::getSize() { 57 | return array->getSize(); 58 | } 59 | template 60 | bool ArrayStack::isEmpty() { 61 | return array->isEmpty(); 62 | } 63 | 64 | template 65 | int ArrayStack::getCapacity() { 66 | return 0; 67 | } 68 | 69 | template 70 | void ArrayStack::push(E e) { 71 | array->addLast(e); 72 | } 73 | 74 | template 75 | E ArrayStack::pop() { 76 | return array->removeLast(); 77 | } 78 | 79 | template 80 | E ArrayStack::peek() { 81 | return array->getLast(); 82 | } 83 | 84 | template 85 | void ArrayStack::toString() { 86 | cout << "Stack: ["; 87 | for (int i = 0; i getSize(); ++i) { 88 | E t = array->objectAtIndex(i); 89 | if (i != 0) { 90 | cout << ", "; 91 | } 92 | std::cout << t; 93 | } 94 | cout << "] top" << endl; 95 | } 96 | 97 | #endif /* ArrayStack_hpp */ 98 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day6/02-Array-Stack/02-Array-Stack/Stack.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stack.hpp 3 | // 02-Array-Stack 4 | // 5 | // Created by dzb on 2019/3/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #ifndef Stack_hpp 10 | #define Stack_hpp 11 | 12 | #include 13 | 14 | ///Stakc虚基类 15 | template 16 | class Stack { 17 | 18 | public: 19 | virtual int getSize() { return 0; } 20 | virtual bool isEmpty() { return false; } 21 | virtual void push(E e) { } 22 | virtual E pop() { return (E)(0);} 23 | virtual E peek() { return (E)0; } 24 | }; 25 | 26 | 27 | #endif /* Stack_hpp */ 28 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day6/02-Array-Stack/02-Array-Stack/main.mm: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // 02-Array-Stack 4 | // 5 | // Created by dzb on 2019/3/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "ArrayStack.hpp" 11 | #import 12 | 13 | using namespace std; 14 | 15 | int main() { 16 | { 17 | ArrayStack stack(10); 18 | stack.push(10); 19 | stack.push(100); 20 | stack.toString(); 21 | } 22 | getchar(); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day6/08-Queues-Comparison/08-Queues-Comparison.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day6/08-Queues-Comparison/08-Queues-Comparison/ArrayQueue.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ArrayQueue.hpp 3 | // 05-Array-Queue 4 | // 5 | // Created by dzb on 2019/3/4. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #ifndef ArrayQueue_hpp 10 | #define ArrayQueue_hpp 11 | 12 | #include 13 | #include "Queue.hpp" 14 | #include 15 | #include "ArrayList.hpp" 16 | 17 | using namespace std; 18 | 19 | template 20 | class ArrayQueue : public Queue { 21 | private: 22 | ArrayList *m_array; 23 | public: 24 | ///构造方法 25 | explicit ArrayQueue(); 26 | explicit ArrayQueue(int capacity); 27 | ///析构函数 28 | ~ArrayQueue(); 29 | int getSize(); 30 | bool isEmpty(); 31 | void enqueue(E e); 32 | int getCapacity(); 33 | E dequeue(); 34 | E getFront(); 35 | void toString(); 36 | }; 37 | 38 | ///构造方法 39 | template 40 | ArrayQueue::ArrayQueue() :ArrayQueue(10) { } 41 | 42 | template 43 | ArrayQueue::ArrayQueue(int capacity) { 44 | m_array = new ArrayList(capacity); 45 | } 46 | ///析构函数 47 | template 48 | ArrayQueue::~ArrayQueue() { 49 | if (m_array != nullptr) { 50 | delete m_array; 51 | m_array = nullptr; 52 | } 53 | } 54 | 55 | template 56 | int ArrayQueue::getSize() { 57 | return m_array->getSize(); 58 | } 59 | 60 | template 61 | bool ArrayQueue::isEmpty() { 62 | return m_array->isEmpty(); 63 | } 64 | 65 | template 66 | int ArrayQueue::getCapacity() { 67 | return m_array->getCapacity(); 68 | } 69 | 70 | 71 | template 72 | void ArrayQueue::enqueue(E e) { 73 | m_array->addLast(e); 74 | } 75 | 76 | template 77 | E ArrayQueue::dequeue() { 78 | return m_array->removeFirst(); 79 | } 80 | 81 | template 82 | E ArrayQueue::getFront() { 83 | return m_array->getFirst(); 84 | } 85 | 86 | template 87 | void ArrayQueue::toString() { 88 | cout << "Queue: " << "front ["; 89 | size_t _size = m_array->getSize(); 90 | for (int i = 0; i < _size; ++i) { 91 | E e = m_array->objectAtIndex(i); 92 | if (i != 0) { 93 | cout << ", "; 94 | } 95 | cout << e; 96 | } 97 | cout << "] tail" << endl; 98 | } 99 | 100 | #endif /* ArrayQueue_hpp */ 101 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day6/08-Queues-Comparison/08-Queues-Comparison/LoopQueue.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // LoopQueue.hpp 3 | // 06-Loop-Queue 4 | // 5 | // Created by dzb on 2019/3/4. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #ifndef LoopQueue_hpp 10 | #define LoopQueue_hpp 11 | 12 | #include 13 | #include "Queue.hpp" 14 | #include 15 | #include "ArrayList.hpp" 16 | 17 | using namespace std; 18 | 19 | template 20 | class LoopQueue : public Queue { 21 | private: 22 | E *_data; 23 | int _front; ///队首 24 | int _tail; ///队尾 25 | int _size; ///队列元素个数 26 | int _capacity; ///队列容量 27 | // 有兴趣的同学,在完成这一章后,可以思考一下: 28 | // LoopQueue中不声明size,如何完成所有的逻辑? 29 | // 这个问题可能会比大家想象的要难一点点:) 30 | public: 31 | ///构造方法 32 | explicit LoopQueue(); 33 | explicit LoopQueue(int capacity); 34 | ///析构函数 35 | ~LoopQueue(); 36 | int getSize(); 37 | bool isEmpty(); 38 | void enqueue(E e); 39 | int getCapacity(); 40 | E dequeue(); 41 | E getFront(); 42 | void resize(int newCapacity); ///动态扩容和缩容 43 | void toString(); 44 | }; 45 | 46 | 47 | ///构造方法 48 | template 49 | LoopQueue::LoopQueue() :LoopQueue(10) { } 50 | 51 | template 52 | LoopQueue::LoopQueue(int capacity) : _capacity(capacity) , _front(0), _tail(0) , _size(0) { 53 | _data = new E[_capacity+1](); 54 | } 55 | ///析构函数 56 | template 57 | LoopQueue::~LoopQueue() { 58 | if (_data != nullptr) { 59 | delete _data; 60 | _data = nullptr; 61 | } 62 | } 63 | 64 | template 65 | int LoopQueue::getSize() { 66 | return _size; 67 | } 68 | 69 | template 70 | bool LoopQueue::isEmpty() { 71 | return (bool)(_front == _tail); 72 | } 73 | 74 | template 75 | int LoopQueue::getCapacity() { 76 | return (_capacity-1); 77 | } 78 | 79 | // 下一小节再做具体实现 80 | template 81 | void LoopQueue::enqueue(E e) { 82 | if((_tail + 1) % _capacity == _front) { 83 | resize(_capacity * 2); 84 | } 85 | _data[_tail] = e; 86 | _tail = (_tail + 1) % _capacity; 87 | _size ++; 88 | } 89 | // 下一小节再做具体实现 90 | template 91 | E LoopQueue::dequeue() { 92 | 93 | if(isEmpty()) { 94 | throw "Cannot dequeue from an empty queue."; 95 | } 96 | E ret = _data[_front]; 97 | _data[_front] = NULL; 98 | _front = (_front + 1) % _capacity; 99 | _size --; 100 | int capacity = getCapacity(); 101 | if(_size == capacity/4 && capacity / 2 != 0) { 102 | resize(capacity/2); 103 | } 104 | return ret; 105 | } 106 | // 下一小节再做具体实现 107 | template 108 | E LoopQueue::getFront() { 109 | if(isEmpty()) { 110 | throw "Cannot dequeue from an empty queue."; 111 | } 112 | return _data[_front]; 113 | } 114 | 115 | template 116 | void LoopQueue::resize(int newCapacity){ 117 | _capacity = newCapacity + 1; 118 | E *newData = new E[_capacity](); ///新数组 119 | for(int i = 0 ; i < _size ; i ++) { 120 | newData[i] = _data[(i + _front) % _capacity]; 121 | } 122 | delete [] _data; 123 | _data = newData; 124 | _front = 0; 125 | _tail = _size; 126 | } 127 | 128 | template 129 | void LoopQueue::toString() { 130 | 131 | cout << "Queue: size = " <<_size << " , capacity = " <<_capacity << endl; 132 | cout << "front ["; 133 | for (int i = _front; i != _tail; i = (i + 1) % _capacity) { 134 | E e = _data[i]; 135 | cout << e; 136 | if((i + 1) % _capacity != _tail) { 137 | cout << ", "; 138 | } 139 | } 140 | cout << "] tail" << endl; 141 | } 142 | 143 | #endif /* LoopQueue_hpp */ 144 | 145 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day6/08-Queues-Comparison/08-Queues-Comparison/Queue.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Queue.hpp 3 | // 05-Array-Queue 4 | // 5 | // Created by dzb on 2019/3/4. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #ifndef Queue_hpp 10 | #define Queue_hpp 11 | 12 | #include 13 | ///MARK: Queue虚基类 14 | 15 | template 16 | class Queue { 17 | 18 | public: 19 | virtual int getSize() { 20 | return 0; 21 | } 22 | virtual bool isEmpty() { 23 | return false; 24 | } 25 | virtual void enqueue(E e) { 26 | 27 | } 28 | virtual E dequeue() { 29 | return (E)0; 30 | } 31 | virtual E getFront() { 32 | return (E)0; 33 | } 34 | }; 35 | 36 | #endif /* Queue_hpp */ 37 | -------------------------------------------------------------------------------- /MJ恋上数据结构/day6/08-Queues-Comparison/08-Queues-Comparison/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // 05-Array-Queue 4 | // 5 | // Created by dzb on 2019/3/4. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "Queue.hpp" 11 | #include "ArrayQueue.hpp" 12 | #include "LoopQueue.hpp" 13 | #include 14 | #include 15 | 16 | static double testQueue(Queue &q, int opCount){ 17 | 18 | clock_t startTime = clock(); 19 | int random = rand(); 20 | for(int i = 0 ; i < opCount ; i ++) { 21 | q.enqueue(random); 22 | random++; 23 | } 24 | for(int i = 0 ; i < opCount ; i ++) { 25 | q.dequeue(); 26 | } 27 | 28 | clock_t endTime = clock(); 29 | 30 | return double(endTime - startTime) / CLOCKS_PER_SEC; 31 | } 32 | 33 | int main() { 34 | 35 | int opCount = 100000; 36 | ArrayQueue arrayQueue; 37 | double time1 = testQueue(arrayQueue, opCount); 38 | std::cout<<"ArrayQueue, time: " << time1 << " s" << endl; 39 | 40 | LoopQueue loopQueue; 41 | double time2 = testQueue(loopQueue, opCount); 42 | std::cout<<"LoopQueue, time: " << time2 << " s" << endl; 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BestiOSDev/MJ_DataStructures/e2bab856549ab1d199b701c4f726cfc3689de201/OC版本数据结构/CoreFoundation/.DS_Store -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // CoreFoundation 4 | // 5 | // Created by dzb on 2019/4/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // CoreFoundation 4 | // 5 | // Created by dzb on 2019/4/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/MTFoundation/MTArray.h: -------------------------------------------------------------------------------- 1 | // 2 | // MTArray.h 3 | // CoreFoundation 4 | // 5 | // Created by dzb on 2019/4/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | NS_ASSUME_NONNULL_BEGIN 13 | 14 | @interface MTArray <__covariant ObjectType> : NSObject 15 | { 16 | @protected 17 | CFMutableArrayRef _array; 18 | } 19 | 20 | + (instancetype)array; 21 | + (instancetype)arrayWithObject:(ObjectType)anObject; 22 | + (instancetype)arrayWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_NIL_TERMINATION; 23 | + (instancetype)arrayWithArray:(NSArray *)array; 24 | 25 | - (instancetype)initWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_NIL_TERMINATION; 26 | - (instancetype)initWithArray:(NSArray *)array; 27 | 28 | - (BOOL)containsObject:(ObjectType)anObject; 29 | - (ObjectType)objectAtIndex:(NSUInteger)index; 30 | - (NSInteger) indexOfObject:(ObjectType)anObject; 31 | @property (readonly) NSUInteger count; 32 | @property (nullable, nonatomic, readonly) ObjectType firstObject; 33 | @property (nullable, nonatomic, readonly) ObjectType lastObject; 34 | 35 | - (MTArray *)subarrayWithRange:(NSRange)range; 36 | 37 | @end 38 | 39 | NS_ASSUME_NONNULL_END 40 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/MTFoundation/MTArray.m: -------------------------------------------------------------------------------- 1 | // 2 | // MTArray.m 3 | // CoreFoundation 4 | // 5 | // Created by dzb on 2019/4/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "MTArray.h" 10 | #import 11 | typedef id _Nullable (*RFUNC)(id _Nonnull, SEL _Nonnull,...); 12 | 13 | @interface MTArray () 14 | 15 | @end 16 | 17 | @implementation MTArray 18 | 19 | 20 | + (instancetype)array { 21 | return [[self alloc] init]; 22 | } 23 | 24 | + (instancetype)arrayWithObject:(id)anObject { 25 | return [[self array] initWithObjects:anObject, nil]; 26 | } 27 | 28 | +(instancetype)arrayWithObjects:(id)firstObj, ... { 29 | 30 | MTArray *arr = [self array]; 31 | va_list ap; 32 | va_start(ap, firstObj); 33 | 34 | id obj = firstObj; 35 | while (obj != nil) { 36 | CFArrayAppendValue(arr->_array,(__bridge const void *)(obj)); 37 | obj = va_arg(ap, id); 38 | } 39 | va_end(ap); 40 | return arr; 41 | } 42 | 43 | + (instancetype)arrayWithArray:(NSArray *)array { 44 | return [[MTArray alloc] initWithArray:array]; 45 | } 46 | 47 | - (instancetype)init { 48 | if (self = [super init]) { 49 | _array = CFArrayCreateMutable(kCFAllocatorDefault,0, &kCFTypeArrayCallBacks); 50 | } 51 | return self; 52 | } 53 | 54 | - (instancetype)initWithObjects:(id)firstObj, ... { 55 | if (self = [super init]) { 56 | _array = CFArrayCreateMutable(kCFAllocatorDefault,0, &kCFTypeArrayCallBacks); 57 | va_list ap; 58 | va_start(ap, firstObj); 59 | id obj = firstObj; 60 | while (obj != nil) { 61 | CFArrayAppendValue(_array,(__bridge const void *)(obj)); 62 | obj = va_arg(ap, id); 63 | } 64 | va_end(ap); 65 | } 66 | return self; 67 | } 68 | 69 | - (instancetype)initWithArray:(NSArray *)array { 70 | if (self = [super init]) { 71 | _array = CFArrayCreateMutable(kCFAllocatorDefault,0, &kCFTypeArrayCallBacks); 72 | for (id obj in array) { 73 | CFArrayAppendValue(_array,(__bridge const void *)(obj)); 74 | } 75 | } 76 | return self; 77 | } 78 | 79 | - (BOOL)containsObject:(id)anObject { 80 | return CFArrayContainsValue(_array,CFRangeMake(0,self.count), (__bridge const void *)(anObject)); 81 | } 82 | 83 | - (id)objectAtIndex:(NSUInteger)index { 84 | return (__bridge id)CFArrayGetValueAtIndex(_array,index); 85 | } 86 | 87 | - (NSUInteger)count { 88 | return CFArrayGetCount(_array); 89 | } 90 | 91 | - (id)firstObject { 92 | if (self.count == 0) { 93 | return nil; 94 | } 95 | return [self objectAtIndex:0]; 96 | } 97 | 98 | - (id)lastObject { 99 | if (self.count == 0) { 100 | return nil; 101 | } 102 | return [self objectAtIndex:self.count-1]; 103 | } 104 | 105 | - (NSInteger)indexOfObject:(id)anObject { 106 | CFIndex index = CFArrayGetFirstIndexOfValue(_array,CFRangeMake(0, self.count), (__bridge const void *)(anObject)); 107 | if (index != kCFNotFound) { 108 | return index; 109 | } else { 110 | return NSNotFound; 111 | } 112 | } 113 | 114 | - (NSString *)description { 115 | return [NSString stringWithFormat:@"%@",_array]; 116 | } 117 | 118 | - (MTArray *)subarrayWithRange:(NSRange)range { 119 | if (range.location < 0 || range.location > self.count-1) { 120 | return nil; 121 | } 122 | if (range.location + range.length > self.count) { 123 | return nil; 124 | } 125 | const void **values = malloc(sizeof(void*)*range.length); 126 | CFArrayGetValues(_array,CFRangeMake(range.location, range.length), values); 127 | CFMutableArrayRef newArr = CFArrayCreateMutable(kCFAllocatorDefault,0, &kCFTypeArrayCallBacks); 128 | 129 | for (int i = 0; imutationsPtr = (unsigned long *)&state->mutationsPtr; 164 | count = MIN(len, [self count] - state->state); 165 | /* If a mutation has occurred then it's possible that we are being asked to 166 | * get objects from after the end of the array. Don't pass negative values 167 | * to memcpy. 168 | */ 169 | if (count > 0) 170 | { 171 | IMP imp = [self methodForSelector: @selector(objectAtIndex:)]; 172 | int p = (int)state->state; 173 | int i; 174 | 175 | for (i = 0; i < count; i++, p++) 176 | { 177 | RFUNC funcPt =(RFUNC)imp; 178 | id objc = funcPt(self,@selector(objectAtIndex:),p); 179 | buffer[i] = objc; 180 | } 181 | state->state += count; 182 | } 183 | else 184 | { 185 | count = 0; 186 | } 187 | state->itemsPtr = buffer; 188 | return count; 189 | 190 | } 191 | 192 | @end 193 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/MTFoundation/MTDictionary.h: -------------------------------------------------------------------------------- 1 | // 2 | // MTDictionary.h 3 | // CoreFoundation 4 | // 5 | // Created by dzb on 2019/4/7. 6 | // Copyright © 2019年 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | @class MTArray; 12 | 13 | NS_ASSUME_NONNULL_BEGIN 14 | 15 | @interface MTDictionary : NSObject 16 | { 17 | @protected 18 | CFMutableDictionaryRef _dict; 19 | } 20 | 21 | 22 | + (instancetype)dictionary; 23 | + (instancetype)dictionaryWithObject:(ObjectType)object forKey:(KeyType )key; 24 | 25 | + (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...; 26 | 27 | + (instancetype)dictionaryWithObjects:(MTArray *)objects forKeys:(MTArray *)keys; 28 | 29 | - (instancetype)initWithObjectsAndKeys:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION; 30 | - (instancetype)initWithObjects:(MTArray *)objects forKeys:(MTArray *)keys; 31 | 32 | 33 | @property (readonly) NSUInteger count; 34 | - (nullable ObjectType)objectForKey:(KeyType)aKey; 35 | @property (readonly, copy) MTArray *allKeys; 36 | @property (readonly, copy) MTArray *allValues; 37 | - (void)enumerateKeysAndObjectsUsingBlock:(void (^)(KeyType key, ObjectType obj, BOOL *stop))block; 38 | 39 | 40 | @end 41 | 42 | NS_ASSUME_NONNULL_END 43 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/MTFoundation/MTMutableArray.h: -------------------------------------------------------------------------------- 1 | // 2 | // MTMutableArray.h 3 | // CoreFoundation 4 | // 5 | // Created by dzb on 2019/4/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "MTArray.h" 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface MTMutableArray : MTArray 14 | 15 | - (void)addObject:(ObjectType)anObject; 16 | - (void)insertObject:(ObjectType)anObject atIndex:(NSUInteger)index; 17 | - (void)removeLastObject; 18 | - (void)removeObjectAtIndex:(NSUInteger)index; 19 | - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(ObjectType)anObject; 20 | 21 | - (void)addObjectsFromArray:(NSArray *)otherArray; 22 | - (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2; 23 | - (void)removeAllObjects; 24 | - (void)removeObject:(ObjectType)anObject; 25 | 26 | @end 27 | 28 | NS_ASSUME_NONNULL_END 29 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/MTFoundation/MTMutableArray.m: -------------------------------------------------------------------------------- 1 | // 2 | // MTMutableArray.m 3 | // CoreFoundation 4 | // 5 | // Created by dzb on 2019/4/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "MTMutableArray.h" 10 | 11 | @implementation MTMutableArray 12 | 13 | - (void)addObject:(id)anObject { 14 | CFArrayAppendValue(_array,(__bridge const void *)(anObject)); 15 | } 16 | 17 | - (void)insertObject:(id)anObject atIndex:(NSUInteger)index { 18 | CFArrayInsertValueAtIndex(_array,index, (__bridge const void *)(anObject)); 19 | } 20 | 21 | - (void)removeObjectAtIndex:(NSUInteger)index { 22 | CFArrayRemoveValueAtIndex(_array, index); 23 | } 24 | 25 | - (void)removeLastObject { 26 | if (self.count == 0) { return; } 27 | [self removeObjectAtIndex:self.count-1]; 28 | } 29 | 30 | - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject { 31 | if (index > self.count-1) { 32 | return; 33 | } 34 | CFArraySetValueAtIndex(_array, index, (__bridge const void *)(anObject)); 35 | } 36 | 37 | - (void)addObjectsFromArray:(NSArray *)otherArray { 38 | for (id obj in otherArray) { 39 | [self addObject:obj]; 40 | } 41 | } 42 | 43 | - (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2 { 44 | CFArrayExchangeValuesAtIndices(_array,idx1, idx2); 45 | } 46 | 47 | - (void)removeAllObjects { 48 | CFArrayRemoveAllValues(_array); 49 | } 50 | 51 | - (void)removeObject:(id)anObject { 52 | NSInteger index = [self indexOfObject:anObject]; 53 | if (index != NSNotFound) { 54 | [self removeObjectAtIndex:index]; 55 | } 56 | } 57 | 58 | 59 | 60 | @end 61 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/MTFoundation/MTMutableDictionary.h: -------------------------------------------------------------------------------- 1 | // 2 | // MTMutableDictionary.h 3 | // CoreFoundation 4 | // 5 | // Created by dzb on 2019/4/7. 6 | // Copyright © 2019年 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "MTDictionary.h" 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface MTMutableDictionary : MTDictionary 14 | { 15 | NSMutableDictionary *dc; 16 | } 17 | 18 | - (void)removeObjectForKey:(KeyType)aKey; 19 | - (void)setObject:(ObjectType)anObject forKey:(KeyType )aKey; 20 | 21 | - (void)removeAllObjects; 22 | - (void)removeObjectsForKeys:(NSArray *)keyArray; 23 | - (void)setDictionary:(MTDictionary *)otherDictionary; 24 | 25 | @end 26 | 27 | NS_ASSUME_NONNULL_END 28 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/MTFoundation/MTMutableDictionary.m: -------------------------------------------------------------------------------- 1 | // 2 | // MTMutableDictionary.m 3 | // CoreFoundation 4 | // 5 | // Created by dzb on 2019/4/7. 6 | // Copyright © 2019年 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "MTArray.h" 10 | #import "MTMutableDictionary.h" 11 | 12 | @implementation MTMutableDictionary 13 | 14 | - (void)removeAllObjects { 15 | if (self.count == 0) return; 16 | MTArray *keys = self.allKeys; 17 | for (NSString *Key in keys) { 18 | [self removeObjectForKey:Key]; 19 | } 20 | } 21 | 22 | - (void)removeObjectForKey:(id)aKey { 23 | if (!aKey) return; 24 | CFDictionaryRemoveValue(_dict, (__bridge const void *)(aKey)); 25 | } 26 | 27 | - (void)setObject:(id)anObject forKey:(id)aKey { 28 | if (!anObject || !aKey) { 29 | return; 30 | } 31 | CFDictionarySetValue(_dict, (__bridge const void *)(aKey), (__bridge const void *)(anObject)); 32 | } 33 | 34 | - (void)removeObjectsForKeys:(NSArray *)keyArray { 35 | for (NSString *key in keyArray) { 36 | [self removeObjectForKey:key]; 37 | } 38 | } 39 | 40 | - (void)setDictionary:(MTDictionary *)otherDictionary { 41 | [otherDictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key, id _Nonnull obj, BOOL * _Nonnull stop) { 42 | [self setObject:obj forKey:key]; 43 | }]; 44 | } 45 | 46 | @end 47 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/MTFoundation/MTSet.h: -------------------------------------------------------------------------------- 1 | // 2 | // MTSet.h 3 | // CoreFoundation 4 | // 5 | // Created by dzb on 2019/4/7. 6 | // Copyright © 2019年 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface MTSet : NSObject 14 | { 15 | CFMutableStringRef *_set; 16 | } 17 | 18 | + (instancetype)set; 19 | + (instancetype)setWithObject:(ObjectType)object; 20 | + (instancetype)setWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_NIL_TERMINATION; 21 | + (instancetype)setWithSet:(NSSet *)set; 22 | + (instancetype)setWithArray:(NSArray *)array; 23 | 24 | - (instancetype)initWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_NIL_TERMINATION; 25 | - (instancetype)initWithSet:(NSSet *)set; 26 | - (instancetype)initWithArray:(NSArray *)array; 27 | @end 28 | 29 | NS_ASSUME_NONNULL_END 30 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/MTFoundation/MTSet.m: -------------------------------------------------------------------------------- 1 | // 2 | // MTSet.m 3 | // CoreFoundation 4 | // 5 | // Created by dzb on 2019/4/7. 6 | // Copyright © 2019年 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "MTSet.h" 10 | 11 | @implementation MTSet 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // CoreFoundation 4 | // 5 | // Created by dzb on 2019/4/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // CoreFoundation 4 | // 5 | // Created by dzb on 2019/4/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "MTArray.h" 11 | #import "MTMutableDictionary.h" 12 | #import "MTDictionary.h" 13 | #import "MTMutableArray.h" 14 | #import 15 | 16 | @interface ViewController () 17 | { 18 | CFArrayRef _array; 19 | } 20 | @end 21 | 22 | @implementation ViewController 23 | 24 | void callBack(const void *value, void *context) { 25 | NSLog(@"%@",value); 26 | } 27 | 28 | - (void)viewDidLoad { 29 | [super viewDidLoad]; 30 | NSSet *s = [NSSet set]; 31 | // [self testArray]; 32 | [self performSelector:@selector(testDictionary) withObject:nil afterDelay:3.0f]; 33 | } 34 | 35 | - (void) testArray { 36 | 37 | CFAbsoluteTime startTime =CFAbsoluteTimeGetCurrent(); 38 | NSMutableArray *array1 = [NSMutableArray array]; 39 | // MTMutableArray *array2 = [MTMutableArray array]; 40 | for (int i = 0; i<1000000; i++) { 41 | [array1 addObject:@(i)]; 42 | // [array2 addObject:@(i)]; 43 | } 44 | CFAbsoluteTime linkTime = (CFAbsoluteTimeGetCurrent() - startTime); 45 | NSLog(@"Linked in %f ms", linkTime *1000.0); 46 | 47 | } 48 | 49 | - (void) testDictionary { 50 | // NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@100,@"age",@200, nil]; 51 | 52 | 53 | CFAbsoluteTime startTime =CFAbsoluteTimeGetCurrent(); 54 | // NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 55 | MTMutableDictionary *dict1 = [MTMutableDictionary dictionary]; 56 | 57 | // MTMutableArray *array2 = [MTMutableArray array]; 58 | for (int i = 0; i<1000000; i++) { 59 | NSString *key = [NSString stringWithFormat:@"%d",i]; 60 | NSNumber *value = @(i); 61 | [dict1 setObject:value forKey:key]; 62 | // [dict setObject:value forKey:key]; 63 | // [array2 addObject:@(i)]; 64 | } 65 | CFAbsoluteTime linkTime = (CFAbsoluteTimeGetCurrent() - startTime); 66 | NSLog(@"Linked in %f ms", linkTime *1000.0); 67 | 68 | // MTDictionary *dict2 = [MTDictionary dictionary]; 69 | // NSLog(@"%@",dict2); 70 | // 71 | // MTDictionary *dict3 = [MTDictionary dictionaryWithObject:@"dzb" forKey:@"name"]; 72 | // NSLog(@"%@",dict3); 73 | // 74 | // MTDictionary *dict4 = [MTDictionary dictionaryWithObjects:[MTArray arrayWithObjects:@"dzb",@20, nil] forKeys:[MTArray arrayWithObjects:@"name",@"age", nil]]; 75 | // NSLog(@"%@",dict4); 76 | // 77 | // MTDictionary *dict5 = [[MTDictionary alloc] initWithObjectsAndKeys:@"dzb",@"name" ,nil]; 78 | // NSLog(@"%@",dict5); 79 | // 80 | // MTDictionary *dict6 = [[MTDictionary alloc] initWithObjects:[MTArray arrayWithObjects:@"dzb",@20, nil] forKeys:[MTArray arrayWithObjects:@"name",@"age", nil]]; 81 | // NSLog(@"%@",dict6); 82 | 83 | // NSNumber *age = [dict1 objectForKey:@"score"]; 84 | // NSLog(@"age = %@",age); 85 | // 86 | // [dict1 enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { 87 | // NSLog(@"%@ %@ %p",key,obj,stop); 88 | // }]; 89 | // 90 | // NSLog(@"%@",dict1.allValues); 91 | // NSLog(@"%@",dict1.allKeys); 92 | // 93 | // [dict1 setObject:@33 forKey:@"age"]; 94 | // NSLog(@"dict1 = %@",dict1); 95 | // 96 | // [dict1 removeAllObjects]; 97 | // 98 | // NSLog(@"dict1 = %@",dict1); 99 | 100 | } 101 | 102 | 103 | @end 104 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundation/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // CoreFoundation 4 | // 5 | // Created by dzb on 2019/4/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundationTests/CoreFoundationTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // CoreFoundationTests.m 3 | // CoreFoundationTests 4 | // 5 | // Created by dzb on 2019/4/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface CoreFoundationTests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation CoreFoundationTests 16 | 17 | - (void)setUp { 18 | // Put setup code here. This method is called before the invocation of each test method in the class. 19 | } 20 | 21 | - (void)tearDown { 22 | // Put teardown code here. This method is called after the invocation of each test method in the class. 23 | } 24 | 25 | - (void)testExample { 26 | // This is an example of a functional test case. 27 | // Use XCTAssert and related functions to verify your tests produce the correct results. 28 | } 29 | 30 | - (void)testPerformanceExample { 31 | // This is an example of a performance test case. 32 | [self measureBlock:^{ 33 | // Put the code you want to measure the time of here. 34 | }]; 35 | } 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundationTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundationUITests/CoreFoundationUITests.m: -------------------------------------------------------------------------------- 1 | // 2 | // CoreFoundationUITests.m 3 | // CoreFoundationUITests 4 | // 5 | // Created by dzb on 2019/4/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface CoreFoundationUITests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation CoreFoundationUITests 16 | 17 | - (void)setUp { 18 | // Put setup code here. This method is called before the invocation of each test method in the class. 19 | 20 | // In UI tests it is usually best to stop immediately when a failure occurs. 21 | self.continueAfterFailure = NO; 22 | 23 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. 24 | [[[XCUIApplication alloc] init] launch]; 25 | 26 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. 27 | } 28 | 29 | - (void)tearDown { 30 | // Put teardown code here. This method is called after the invocation of each test method in the class. 31 | } 32 | 33 | - (void)testExample { 34 | // Use recording to get started writing UI tests. 35 | // Use XCTAssert and related functions to verify your tests produce the correct results. 36 | } 37 | 38 | @end 39 | -------------------------------------------------------------------------------- /OC版本数据结构/CoreFoundation/CoreFoundationUITests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /OC版本数据结构/LRUCache/.idea/LRUCache.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /OC版本数据结构/LRUCache/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /OC版本数据结构/LRUCache/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /OC版本数据结构/LRUCache/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /OC版本数据结构/LRUCache/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /OC版本数据结构/LRUCache/.idea/xcode.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /OC版本数据结构/LRUCache/LRUCache.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /OC版本数据结构/LRUCache/LRUCache/LRUCache.h: -------------------------------------------------------------------------------- 1 | // 2 | // LRUCache.h 3 | // LRUCache 4 | // 5 | // Created by dzb on 2019/3/18. 6 | // Copyright © 2019年 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @protocol LRUCacheDelegate; 14 | 15 | @interface LRUCache : NSObject 16 | 17 | @property (nonatomic,strong,class) LRUCache *shareCache; 18 | /** 19 | 是否包含key 20 | */ 21 | - (BOOL)containsObjectForKey:(id)key; 22 | 23 | /** 24 | 根据键取对应值 25 | */ 26 | - (nullable id)objectForKey:(id)key; 27 | 28 | /** 29 | 通过键值来存储数据 30 | */ 31 | - (void)setObject:(nullable id)object forKey:(id)key; 32 | 33 | /** 34 | 删除缓存中指定键的值。 35 | 36 | key标识要删除的值的键。如果为nil,则此方法无效。 37 | */ 38 | - (void)removeObjectForKey:(id)key; 39 | 40 | /** 41 | Empties the cache immediately. 42 | */ 43 | - (void)removeAllObjects; 44 | 45 | /** The number of objects in the cache (read-only) */ 46 | @property (readonly) NSUInteger totalCount; 47 | 48 | #pragma mark - Limit 49 | 50 | /** 51 | default is 10 最大缓存10个对象 52 | */ 53 | @property (nonatomic,assign) NSUInteger countLimit; 54 | 55 | /** 56 | 间隔多少秒检查缓存情况 默认5秒间隔 57 | */ 58 | @property NSTimeInterval autoTrimInterval; 59 | 60 | /** 61 | 委托对象 62 | */ 63 | @property (nonatomic,weak) iddelegate; 64 | 65 | /** 66 | 收到内存警告时 是否清理缓存 67 | */ 68 | @property (nonatomic,assign) BOOL shouldRemoveAllObjectsOnMemoryWarning; 69 | 70 | @end 71 | 72 | NS_ASSUME_NONNULL_END 73 | 74 | 75 | @protocol LRUCacheDelegate 76 | 77 | /** 78 | 每次被淘汰的缓存 就会调用这个方法 79 | 80 | @param lru 缓存类 81 | @param obj 将要被淘汰的缓存对象 82 | */ 83 | - (void) lurcache:(LRUCache *_Nullable)lru willEvictObject:(id _Nullable )obj; 84 | 85 | @end 86 | -------------------------------------------------------------------------------- /OC版本数据结构/LRUCache/LRUCache/Person.h: -------------------------------------------------------------------------------- 1 | // 2 | // Person.h 3 | // LRUCache 4 | // 5 | // Created by dzb on 2019/3/18. 6 | // Copyright © 2019年 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface Person : NSObject 14 | @property (nonatomic,strong) NSString *name; 15 | 16 | -(id)initWithName:(NSString *)aName; 17 | 18 | @end 19 | 20 | NS_ASSUME_NONNULL_END 21 | -------------------------------------------------------------------------------- /OC版本数据结构/LRUCache/LRUCache/Person.m: -------------------------------------------------------------------------------- 1 | // 2 | // Person.m 3 | // LRUCache 4 | // 5 | // Created by dzb on 2019/3/18. 6 | // Copyright © 2019年 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "Person.h" 10 | 11 | @implementation Person 12 | 13 | -(id)initWithName:(NSString *)aName { 14 | if (self = [super init]) { 15 | self.name = aName; 16 | } 17 | return self; 18 | } 19 | 20 | - (void)dealloc 21 | { 22 | NSLog(@"Person dealloc"); 23 | } 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /OC版本数据结构/LRUCache/LRUCache/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // LRUCache 4 | // 5 | // Created by dzb on 2019/3/18. 6 | // Copyright © 2019年 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "Person.h" 10 | #include "LRUCache.h" 11 | //#import 12 | 13 | int main(int argc, const char * argv[]) { 14 | @autoreleasepool { 15 | 16 | LRUCache *cache = [LRUCache shareCache]; 17 | cache.countLimit = 10; 18 | 19 | for (int i = 0; i<100; i++) { 20 | NSString *key = [NSString stringWithFormat:@"key_%d",i]; 21 | Person *p = [[Person alloc] initWithName:key]; 22 | [cache setObject:p forKey:key]; 23 | } 24 | 25 | for (int i = 90; i<100; i++) { 26 | NSString *key = [NSString stringWithFormat:@"key_%d",i]; 27 | Person *p = [cache objectForKey:key]; 28 | NSLog(@"%@",p.name); 29 | } 30 | 31 | [cache removeAllObjects]; 32 | 33 | } 34 | 35 | while (1) { 36 | 37 | usleep(1000.0f); 38 | } 39 | 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BestiOSDev/MJ_DataStructures/e2bab856549ab1d199b701c4f726cfc3689de201/OC版本数据结构/iOS-DataStructures/.DS_Store -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // iOS-DataStructures 4 | // 5 | // Created by dzb on 2018/8/5. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // iOS-DataStructures 4 | // 5 | // Created by dzb on 2018/8/5. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/DataStructures/ArrayList.h: -------------------------------------------------------------------------------- 1 | // 2 | // ArrayList.h 3 | // ArrayList 4 | // 5 | // Created by dzb on 2018/7/19. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 自实现一个动态数组 13 | */ 14 | @interface ArrayList < ObjectType > : NSObject 15 | 16 | NS_ASSUME_NONNULL_BEGIN 17 | 18 | + (instancetype)array; //默认容量是10 会动态的扩容和缩减数组长度 19 | + (instancetype)arrayWithCapacity:(NSUInteger)numItems; 20 | - (instancetype)initWithCapacity:(NSUInteger)numItems; 21 | 22 | - (ObjectType)objectAtIndex:(NSUInteger)index; 23 | - (NSUInteger)indexOfObject:(ObjectType)anObject; 24 | - (BOOL)containsObject:(ObjectType)anObject; 25 | 26 | - (void)addObject:(ObjectType)anObject; 27 | - (void)insertObject:(ObjectType)anObject atIndex:(NSUInteger)index; 28 | - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(ObjectType)anObject; 29 | 30 | - (void)removeAllObjects; 31 | - (void)removeLastObject; 32 | - (void)removeObjectAtIndex:(NSUInteger)index; 33 | - (void)removeObject:(ObjectType)anObject; 34 | 35 | @property (readonly) NSUInteger count; 36 | @property (nullable, nonatomic, readonly) ObjectType firstObject; 37 | @property (nullable, nonatomic, readonly) ObjectType lastObject; 38 | 39 | NS_ASSUME_NONNULL_END 40 | 41 | @end 42 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/DataStructures/ArrayQueue.h: -------------------------------------------------------------------------------- 1 | // 2 | // ArrayQueue.h 3 | // ArrayList 4 | // 5 | // Created by dzb on 2018/7/23. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ArrayQueue : NSObject 12 | 13 | + (instancetype) arrayQueue; 14 | + (instancetype) arrayQueueWithCapacity:(NSInteger)capacity; 15 | - (instancetype) initWithCapacity:(NSInteger)capacity; 16 | 17 | - (void) enqueue:(ObjectType)obj; ///入队列 18 | - (id) dequeue; ///出队列 19 | - (void) removeAllObjects; ///移除队列里边所有元素 20 | 21 | ///firstObject 22 | @property (nonatomic,weak) ObjectType firstObject; 23 | ///size 24 | @property (nonatomic,assign,readonly) NSInteger size; 25 | ///isEmpty 26 | @property (nonatomic,assign,getter=isEmpty) BOOL empty; 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/DataStructures/ArrayQueue.m: -------------------------------------------------------------------------------- 1 | // 2 | // ArrayQueue.m 3 | // ArrayList 4 | // 5 | // Created by dzb on 2018/7/23. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "ArrayList.h" 10 | #import "ArrayQueue.h" 11 | 12 | @interface ArrayQueue () 13 | { 14 | ArrayList *_array; 15 | } 16 | 17 | @end 18 | 19 | 20 | @implementation ArrayQueue 21 | 22 | + (instancetype)arrayQueue { 23 | return [[ArrayQueue alloc] initWithCapacity:10]; 24 | } 25 | 26 | + (instancetype)arrayQueueWithCapacity:(NSInteger)capacity { 27 | return [[ArrayQueue alloc] initWithCapacity:capacity]; 28 | } 29 | 30 | - (instancetype)initWithCapacity:(NSInteger)numItems { 31 | if (self = [super init]) { 32 | _array = [ArrayList arrayWithCapacity:numItems]; 33 | } 34 | return self; 35 | } 36 | 37 | - (void)enqueue:(id)obj { 38 | [_array addObject:obj]; 39 | } 40 | 41 | - (id)dequeue { 42 | [_array removeObjectAtIndex:0]; 43 | return nil; 44 | } 45 | 46 | - (id)firstObject { 47 | return [_array firstObject]; 48 | } 49 | 50 | - (void)removeAllObjects { 51 | [_array removeAllObjects]; 52 | } 53 | 54 | - (NSInteger)size { 55 | return _array.count; 56 | } 57 | - (BOOL)isEmpty { 58 | return _array.count == 0; 59 | } 60 | 61 | - (NSString *)description { 62 | 63 | NSMutableString *res = [NSMutableString string]; 64 | [res appendFormat:@"ArrayQueue: %p \n",self]; 65 | [res appendString:@"front [ "]; 66 | for (int i = 0; i<_array.count; i++) { 67 | id object = [_array objectAtIndex:i]; 68 | [res appendFormat:@"%@",object]; 69 | if (i != _array.count - 1) { 70 | [res appendString:@" , "]; 71 | } 72 | } 73 | [res appendString:@" ] tail "]; 74 | return res; 75 | } 76 | 77 | - (void)dealloc 78 | { 79 | 80 | } 81 | 82 | @end 83 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/DataStructures/ArrayStack.h: -------------------------------------------------------------------------------- 1 | // 2 | // ArrayStack.h 3 | // ArrayList 4 | // 5 | // Created by dzb on 2018/7/23. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface ArrayStack : NSObject 14 | 15 | + (instancetype) arrayStack; ///默认容量10 16 | + (instancetype) stackWithCapacity:(NSInteger)numItems; 17 | - (instancetype) initWithCapacity:(NSInteger)numItems; 18 | 19 | - (void) push:(ObjectType)obj; ///入栈一个元素 20 | - (ObjectType) pop; ///出栈一个元素 21 | - (void) removeAllObjects; ///移除栈里边所有元素 22 | 23 | ///size 24 | @property (nonatomic,assign,readonly) NSInteger size; 25 | ///isEmpty 26 | @property (nonatomic,assign,getter=isEmpty) BOOL empty; 27 | 28 | @end 29 | 30 | NS_ASSUME_NONNULL_END 31 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/DataStructures/ArrayStack.m: -------------------------------------------------------------------------------- 1 | // 2 | // ArrayStack.m 3 | // ArrayList 4 | // 5 | // Created by dzb on 2018/7/23. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "ArrayList.h" 10 | #import "ArrayStack.h" 11 | 12 | @interface ArrayStack () 13 | { 14 | ArrayList *_array; 15 | } 16 | @end 17 | 18 | @implementation ArrayStack 19 | 20 | - (instancetype)initWithCapacity:(NSInteger)numItems { 21 | if (self = [super init]) { 22 | _array = [ArrayList arrayWithCapacity:numItems]; 23 | } 24 | return self; 25 | } 26 | 27 | + (instancetype)stackWithCapacity:(NSInteger)numItems { 28 | return [[ArrayStack alloc] initWithCapacity:numItems]; 29 | } 30 | 31 | + (instancetype)arrayStack { 32 | return [[ArrayStack alloc] initWithCapacity:10]; 33 | } 34 | 35 | - (void)push:(id)obj { 36 | [_array addObject:obj]; 37 | } 38 | 39 | - (id)pop { 40 | id object = [_array lastObject]; 41 | [_array removeObject:object]; 42 | return object; 43 | } 44 | 45 | - (void)removeAllObjects { 46 | [_array removeAllObjects]; 47 | } 48 | 49 | - (NSInteger)size { 50 | return _array.count; 51 | } 52 | 53 | - (BOOL)isEmpty { 54 | return _array.count == 0; 55 | } 56 | 57 | - (NSString *)description { 58 | 59 | NSMutableString *res = [NSMutableString string]; 60 | [res appendFormat:@"ArrayStack: %p \n",self]; 61 | [res appendString:@" [ "]; 62 | for (int i = 0; i<_array.count; i++) { 63 | id object = [_array objectAtIndex:i]; 64 | [res appendFormat:@"%@",object]; 65 | if (i != _array.count - 1) { 66 | [res appendString:@" , "]; 67 | } 68 | } 69 | [res appendString:@" ] top "]; 70 | return res; 71 | } 72 | 73 | - (void)dealloc 74 | { 75 | 76 | } 77 | 78 | @end 79 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/DataStructures/LinkedList.h: -------------------------------------------------------------------------------- 1 | // 2 | // LinkedList.h 3 | // ArrayList 4 | // 5 | // Created by dzb on 2018/8/3. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface LinkedList : NSObject 12 | 13 | - (void) addObjectAtFirst:(ObjectType)object; 14 | 15 | - (void) addObjectAtLast:(ObjectType)object; 16 | 17 | - (void) addObject:(ObjectType)object atIndex:(NSInteger)index; 18 | 19 | - (void) updateObject:(ObjectType)object atIndex:(NSInteger)index; 20 | 21 | - (BOOL) containObject:(ObjectType)object; 22 | 23 | - (ObjectType) objectAtIndex:(NSInteger)index; 24 | 25 | - (ObjectType) removeObjectAtIndex:(NSInteger)index; 26 | 27 | - (ObjectType) removeFirstObject; 28 | 29 | - (ObjectType) removeLastObject; 30 | 31 | - (void) removeAllObjects; 32 | 33 | ///count 34 | @property (nonatomic,assign) NSInteger count; 35 | ///empty 36 | @property (nonatomic,assign,getter=isEmpty,readonly) BOOL empty; 37 | ///firstObject 38 | @property (nonatomic,strong,readonly) ObjectType firstObject; 39 | ///lastObject 40 | @property (nonatomic,strong,readonly) ObjectType lastObject; 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/DataStructures/LinkedList.m: -------------------------------------------------------------------------------- 1 | // 2 | // LinkedList.m 3 | // ArrayList 4 | // 5 | // Created by dzb on 2018/8/3. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "LinkedList.h" 10 | 11 | typedef void* AnyObject; 12 | typedef struct node { 13 | AnyObject data; 14 | struct node *next; 15 | } Node; 16 | 17 | @interface LinkedList () 18 | 19 | ///head 20 | @property (nonatomic,assign) Node *dummyHead; 21 | ///size 22 | @property (nonatomic,assign) NSInteger size; 23 | 24 | @end 25 | 26 | @implementation LinkedList 27 | 28 | - (instancetype)init 29 | { 30 | self = [super init]; 31 | if (self) { 32 | Node * dummyHead = (Node*)malloc(sizeof(Node)); 33 | dummyHead->data = nil; 34 | dummyHead->next = nil; 35 | self.dummyHead = dummyHead; 36 | self.size = 0; 37 | } 38 | return self; 39 | } 40 | 41 | - (void)addObjectAtFirst:(id)object { 42 | [self addObject:object atIndex:0]; 43 | } 44 | 45 | - (void)addObjectAtLast:(id)object { 46 | [self addObject:object atIndex:self.size]; 47 | } 48 | 49 | - (void)addObject:(id)object atIndex:(NSInteger)index { 50 | if (index < 0 || index > self.count) { 51 | @throw [NSException exceptionWithName:@"LinkedList is out of bounds" reason:@"Add failed. Illegal index." userInfo:nil]; 52 | return; 53 | } 54 | Node *prev = self.dummyHead; 55 | for (int i = 0; i< index; i++) { 56 | prev = prev->next; 57 | } 58 | Node *cur = (Node*)malloc(sizeof(Node)); 59 | cur->data = (__bridge_retained AnyObject)object; 60 | cur->next = prev->next; 61 | prev->next = cur; 62 | self.size++; 63 | } 64 | 65 | - (id)objectAtIndex:(NSInteger)index { 66 | if (index < 0 || index >= self.count) { 67 | @throw [NSException exceptionWithName:@"LinkedList is out of bounds" reason:@"Add failed. Illegal index." userInfo:nil]; 68 | return nil; 69 | } 70 | Node *cur = self.dummyHead->next; 71 | for (int i = 0; inext; 73 | } 74 | return (__bridge_transfer id)cur->data; 75 | } 76 | 77 | - (id)firstObject { 78 | return [self objectAtIndex:0]; 79 | } 80 | 81 | - (id)lastObject { 82 | return [self objectAtIndex:self.count-1]; 83 | } 84 | 85 | - (void)updateObject:(id)object atIndex:(NSInteger)index { 86 | if (index < 0 || index >= self.count) { 87 | @throw [NSException exceptionWithName:@"LinkedList is out of bounds" reason:@"Add failed. Illegal index." userInfo:nil]; 88 | return; 89 | } 90 | Node *cur = self.dummyHead->next; 91 | for (int i = 0; inext; 93 | } 94 | CFRelease(cur->data); 95 | cur->data = (__bridge_retained AnyObject)object; 96 | } 97 | 98 | - (BOOL)containObject:(id)object { 99 | Node *cur = self.dummyHead->next; 100 | while (cur != NULL) { 101 | id data = (__bridge_transfer id)cur->data; 102 | if ([data isEqual:object]) 103 | return YES; 104 | cur = cur->next; 105 | } 106 | return NO; 107 | } 108 | 109 | 110 | - (id) removeObjectAtIndex:(NSInteger)index { 111 | if (index < 0 || index >= self.count) { 112 | @throw [NSException exceptionWithName:@"LinkedList is out of bounds" reason:@"Add failed. Illegal index." userInfo:nil]; 113 | return nil; 114 | } 115 | Node *prev = self.dummyHead; 116 | for (int i = 0; inext; 118 | } 119 | Node *deleteNode = prev->next; 120 | prev->next = deleteNode->next; 121 | id object = (__bridge_transfer id)deleteNode->data; 122 | free(deleteNode); 123 | deleteNode = NULL; 124 | self.size--; 125 | return object; 126 | } 127 | 128 | - (id) removeFirstObject { 129 | return [self removeObjectAtIndex:0]; 130 | } 131 | 132 | - (id) removeLastObject { 133 | return [self removeObjectAtIndex:self.count-1]; 134 | } 135 | 136 | - (void) removeAllObjects { 137 | while (self.count != 0) { 138 | [self removeFirstObject]; 139 | } 140 | } 141 | 142 | - (NSInteger)count { 143 | return self.size; 144 | } 145 | 146 | - (NSString *)description { 147 | 148 | NSMutableString *string = [NSMutableString stringWithFormat:@"\nLinkedList %p : [ \n" ,self]; 149 | Node *cur = self.dummyHead->next; 150 | while (cur != nil) { 151 | [string appendFormat:@"%@ -> \n",cur->data]; 152 | cur = cur->next; 153 | } 154 | [string appendString:@"NULL\n"]; 155 | [string appendString:@"]\n"]; 156 | 157 | return string; 158 | } 159 | 160 | - (BOOL)isEmpty { 161 | return self.count == 0; 162 | } 163 | 164 | - (void)dealloc 165 | { 166 | [self removeAllObjects]; 167 | } 168 | 169 | @end 170 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/DataStructures/LinkedListStack.h: -------------------------------------------------------------------------------- 1 | // 2 | // LinkedListStack.h 3 | // iOS-DataStructures 4 | // 5 | // Created by dzb on 2018/8/7. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface LinkedListStack : NSObject 12 | 13 | + (instancetype) linkedListStack; 14 | 15 | - (void) push:(ObjectType)obj; ///入栈一个元素 16 | - (ObjectType) pop; ///出栈一个元素 17 | - (void) removeAllObjects; ///移除栈里边所有元素 18 | ///size 19 | @property (nonatomic,assign,readonly) NSInteger size; 20 | ///isEmpty 21 | @property (nonatomic,assign,getter=isEmpty) BOOL empty; 22 | 23 | @end 24 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/DataStructures/LinkedListStack.m: -------------------------------------------------------------------------------- 1 | // 2 | // LinkedListStack.m 3 | // iOS-DataStructures 4 | // 5 | // Created by dzb on 2018/8/7. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "LinkedList.h" 10 | #import "LinkedListStack.h" 11 | 12 | @interface LinkedListStack () 13 | { 14 | LinkedList *_linkedList; 15 | } 16 | @end 17 | 18 | @implementation LinkedListStack 19 | 20 | - (instancetype)init 21 | { 22 | self = [super init]; 23 | if (self) { 24 | _linkedList = [[LinkedList alloc] init]; 25 | } 26 | return self; 27 | } 28 | 29 | + (instancetype)linkedListStack { 30 | return [[LinkedListStack alloc] init]; 31 | } 32 | 33 | - (void)push:(id)obj { 34 | [_linkedList addObjectAtFirst:obj]; 35 | } 36 | 37 | - (id)pop { 38 | return [_linkedList removeFirstObject]; 39 | } 40 | 41 | - (void)removeAllObjects { 42 | [_linkedList removeAllObjects]; 43 | } 44 | 45 | - (NSInteger)size { 46 | return _linkedList.count; 47 | } 48 | 49 | - (BOOL)isEmpty { 50 | return _linkedList.count == 0; 51 | } 52 | 53 | - (NSString *)description { 54 | 55 | NSMutableString *res = [NSMutableString string]; 56 | [res appendFormat:@"ArrayStack: %p \n",self]; 57 | [res appendString:@" [ "]; 58 | for (int i = 0; i<_linkedList.count; i++) { 59 | id object = [_linkedList objectAtIndex:i]; 60 | [res appendFormat:@"%@",object]; 61 | if (i != _linkedList.count - 1) { 62 | [res appendString:@" , "]; 63 | } 64 | } 65 | [res appendString:@" ] top "]; 66 | return res; 67 | } 68 | 69 | - (void)dealloc 70 | { 71 | 72 | } 73 | 74 | @end 75 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/DataStructures/LoopQueue.h: -------------------------------------------------------------------------------- 1 | // 2 | // LoopQueue.h 3 | // ArrayList 4 | // 5 | // Created by dzb on 2018/7/24. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface LoopQueue : NSObject 12 | 13 | + (instancetype) loopQueue; 14 | + (instancetype) loopQueueWithCapacity:(NSInteger)capacity; 15 | - (instancetype) initWithCapacity:(NSInteger)capacity; 16 | 17 | - (void) enqueue:(ObjectType)obj; ///入队列 18 | - (id) dequeue; ///出队列 19 | - (void) removeAllObjects; ///移除队列里边所有元素 20 | 21 | ///firstObject 22 | @property (nonatomic,weak) ObjectType firstObject; 23 | ///size 24 | @property (nonatomic,assign,readonly) NSInteger size; 25 | ///isEmpty 26 | @property (nonatomic,assign,getter=isEmpty) BOOL empty; 27 | 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/DataStructures/LoopQueue.m: -------------------------------------------------------------------------------- 1 | // 2 | // LoopQueue.m 3 | // ArrayList 4 | // 5 | // Created by dzb on 2018/7/24. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "LoopQueue.h" 10 | 11 | static NSInteger const defaultCapacity = 10; 12 | typedef void * AnyObject; 13 | 14 | @interface LoopQueue () 15 | { 16 | @private 17 | AnyObject *_array; 18 | NSInteger _front , _tail; 19 | NSInteger _size; 20 | } 21 | 22 | ///capacity 23 | @property (nonatomic,assign) NSInteger capacity; 24 | 25 | @end 26 | 27 | @implementation LoopQueue 28 | 29 | - (instancetype)initWithCapacity:(NSInteger)capacity { 30 | if (self = [super init]) { 31 | _capacity = capacity + 1; 32 | size_t size = sizeof(AnyObject) * _capacity; 33 | _array = calloc(_capacity, size); 34 | _front = 0; 35 | _size = 0; 36 | _tail = 0; 37 | } 38 | return self; 39 | } 40 | 41 | + (instancetype)loopQueue { 42 | return [[LoopQueue alloc] initWithCapacity:defaultCapacity]; 43 | } 44 | 45 | + (instancetype)loopQueueWithCapacity:(NSInteger)capacity { 46 | return [[LoopQueue alloc] initWithCapacity:capacity]; 47 | } 48 | 49 | 50 | - (id)dequeue { 51 | if (self.isEmpty) { 52 | @throw [NSException exceptionWithName:@"queue empty" reason:@"Cannot dequeue from an empty queue." userInfo:nil]; 53 | return nil; 54 | } 55 | id object = (__bridge_transfer id)(_array[_front]); 56 | _array[_front] = NULL; 57 | _front = (_front + 1) % (_size); 58 | _size--; 59 | if (_size == self.size * 0.25 && self.size * 0.5 != 0) { 60 | [self resize:self.size * 0.5]; 61 | } 62 | return object; 63 | } 64 | 65 | - (void)enqueue:(id)obj { 66 | if (!obj) { 67 | @throw [NSException exceptionWithName:@"Object empty" reason:@"Object cannot a null object" userInfo:nil]; 68 | return; 69 | } 70 | if ((_tail + 1) % self.capacity == _front ) { 71 | [self resize:self.capacity * 2]; 72 | } 73 | _array[_tail] = (__bridge_retained AnyObject)obj; 74 | _tail = (_tail + 1) % self.capacity; 75 | _size ++; 76 | } 77 | 78 | - (void)removeAllObjects { 79 | 80 | } 81 | 82 | /** 83 | 对数组扩容 84 | 85 | @param capacity 新的容量 86 | */ 87 | - (void) resize:(NSInteger)capacity { 88 | 89 | _capacity = capacity + 1; 90 | AnyObject *oldArray = _array; 91 | AnyObject *newArray = calloc(_capacity,sizeof(AnyObject)); 92 | size_t size = sizeof(AnyObject) * self.size; 93 | memcpy(newArray,oldArray,size); ///对旧的数组进行值的拷贝 94 | _array = newArray; 95 | _front = 0; 96 | _tail = self.size; 97 | if (oldArray != NULL) { 98 | free(oldArray); 99 | oldArray = NULL; 100 | } 101 | 102 | } 103 | 104 | - (NSInteger)size { 105 | return _size; 106 | } 107 | 108 | - (BOOL)isEmpty { 109 | return (_front == _tail); 110 | } 111 | 112 | - (id)firstObject { 113 | if (self.isEmpty) { 114 | @throw [NSException exceptionWithName:@"queue empty" reason:@"Cannot dequeue from an empty queue." userInfo:nil]; 115 | return nil; 116 | } 117 | AnyObject obj = _array[_front]; 118 | return (__bridge id)obj; 119 | } 120 | 121 | 122 | - (NSString *)description { 123 | NSMutableString *res = [NSMutableString string]; 124 | [res appendFormat:@"ArrayQueue: %p \n",self]; 125 | [res appendString:@"front [ "]; 126 | for (NSInteger i = 0; i = 0) { 144 | AnyObject *obj = _array[i]; 145 | if (obj != NULL) 146 | CFRelease(obj); 147 | i--; 148 | } 149 | free(_array); 150 | } 151 | } 152 | 153 | @end 154 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/Person.h: -------------------------------------------------------------------------------- 1 | // 2 | // Person.h 3 | // ArrayList 4 | // 5 | // Created by dzb on 2018/7/19. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface Person : NSObject 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/Person.m: -------------------------------------------------------------------------------- 1 | // 2 | // Person.m 3 | // ArrayList 4 | // 5 | // Created by dzb on 2018/7/19. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "Person.h" 10 | 11 | @implementation Person 12 | 13 | - (void)dealloc 14 | { 15 | // NSLog(@"person dealloc %p",self); 16 | } 17 | @end 18 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // iOS-DataStructures 4 | // 5 | // Created by dzb on 2018/8/5. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // ArrayList 4 | // 5 | // Created by dzb on 2018/7/19. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "ArrayList.h" 11 | #import "Person.h" 12 | #import "ArrayQueue.h" 13 | #import "ArrayStack.h" 14 | #import "LoopQueue.h" 15 | #import "LinkedListStack.h" 16 | #import "LinkedList.h" 17 | 18 | @interface ViewController () 19 | { 20 | ArrayList *_array; 21 | ArrayQueue *_queue; 22 | LinkedList * _linkedList; 23 | NSTimer *_timer; 24 | NSMutableArray *_timeArray; 25 | } 26 | @end 27 | 28 | @implementation ViewController 29 | 30 | - (void)viewDidLoad { 31 | [super viewDidLoad]; 32 | 33 | // [self testArray]; 34 | 35 | LoopQueue *queue = [LoopQueue loopQueueWithCapacity:10]; 36 | [queue enqueue:@"dzb"]; 37 | 38 | 39 | // LinkedList *linked = [[LinkedList alloc] init]; 40 | // 41 | // Person *p1 = [Person new]; 42 | // Person *p2 = [Person new]; 43 | // Person *p3 = [Person new]; 44 | // NSLog(@"p1 = %p p2 = %p p3 = %p",p1,p2,p3); 45 | // 46 | // [linked addObjectAtFirst:p1]; 47 | // [linked addObjectAtFirst:p2]; 48 | // [linked addObjectAtFirst:p3]; 49 | // 50 | // NSLog(@"%@",linked); 51 | // 52 | // [linked removeFirstObject]; 53 | // [linked removeFirstObject]; 54 | // [linked removeFirstObject]; 55 | // 56 | // NSLog(@"%@",linked); 57 | 58 | } 59 | 60 | 61 | /** 62 | 编译器匹配 {} [] () 63 | 64 | @param string 字符串 65 | @return 匹配字符串是否正常关闭 66 | */ 67 | - (BOOL) isValid:(NSString *)string { 68 | 69 | ArrayStack *stack = [ArrayStack stackWithCapacity:10]; 70 | NSString *tmp; 71 | for (int i = 0; i *stack = [ArrayStack stackWithCapacity:10]; 102 | for (int i = 0; i<10; i++) { 103 | [stack push:@(i)]; 104 | } 105 | NSLog(@"%ld",(long)stack.size); 106 | NSLog(@"%ld",(long)stack.isEmpty); 107 | NSLog(@"%@",stack); 108 | 109 | id obj = [stack pop]; 110 | NSLog(@"%@",obj); 111 | 112 | NSLog(@"%@",stack); 113 | 114 | [stack removeAllObjects]; 115 | 116 | NSLog(@"%@",stack); 117 | 118 | } 119 | 120 | - (void) testQueue { 121 | 122 | CFAbsoluteTime startTime =CFAbsoluteTimeGetCurrent(); 123 | int count = 100; 124 | Person *p = [Person new]; 125 | ArrayQueue *queue = [ArrayQueue arrayQueue]; 126 | for (int i = 0; i 9) { 146 | [_timer invalidate]; 147 | _timer = nil; 148 | NSLog(@"time is %@",[_timeArray valueForKeyPath:@"@avg.self"]); 149 | return; 150 | } 151 | 152 | dispatch_async(dispatch_get_global_queue(0, 0), ^{ 153 | CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent(); 154 | int number = 100000; 155 | Person *p = [Person new]; 156 | LinkedListStack *linked = [LinkedListStack new]; 157 | for (int i = 0; i_timeArray addObject:@(duration)]; 164 | count++; 165 | }); 166 | 167 | } 168 | 169 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 170 | _array = nil; 171 | } 172 | 173 | @end 174 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructures/main.mm: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // iOS-DataStructures 4 | // 5 | // Created by dzb on 2018/8/5. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | #import 12 | 13 | class Car { 14 | 15 | private: 16 | 17 | void copy(const char *name = NULL) { 18 | if (name == NULL) return; 19 | size_t len = strlen(name) + 1; 20 | _name = new char[len]{}; 21 | strcpy(_name,name); 22 | } 23 | 24 | public: 25 | int _price; 26 | char *_name; 27 | Car(int price = 0,const char *name = NULL) :_price(price) { 28 | copy(name); 29 | } 30 | Car(const Car &car) : _price(car._price) { 31 | copy(car._name); 32 | } 33 | void display() { 34 | std::cout << "price is " << this->_price << " name is " <_name << std::endl; 35 | } 36 | ~Car() { 37 | if (_name != NULL) { 38 | delete[] _name; 39 | _name = NULL; 40 | } 41 | } 42 | }; 43 | 44 | 45 | 46 | int main(int argc, char * argv[]) { 47 | @autoreleasepool { 48 | 49 | char *name = new char[7]{'b','r','y','a','n','t','\0'}; 50 | Car car = Car(100,name); 51 | car.display(); 52 | 53 | Car car2 = Car(car); 54 | car2.display(); 55 | 56 | car._name = (char *)"hellow"; 57 | car.display(); 58 | car2.display(); 59 | 60 | delete[] name; 61 | 62 | // Car c1 = Car(100); 63 | // Car c2 = Car(c1); 64 | // c2.display(); 65 | // 66 | // BuleCar blueCar = BuleCar(10,100,30); 67 | // BuleCar c3 = BuleCar(blueCar); 68 | 69 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructuresTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructuresTests/TestArray.m: -------------------------------------------------------------------------------- 1 | // 2 | // TestArray.m 3 | // iOS-DataStructuresTests 4 | // 5 | // Created by dzb on 2018/8/5. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "ArrayList.h" 11 | #import "Person.h" 12 | 13 | @interface TestArray : XCTestCase 14 | { 15 | NSMutableArray *_timeArray; 16 | } 17 | @end 18 | 19 | @implementation TestArray 20 | 21 | - (void)setUp { 22 | [super setUp]; 23 | 24 | // Put setup code here. This method is called before the invocation of each test method in the class. 25 | } 26 | 27 | - (void)tearDown { 28 | // Put teardown code here. This method is called after the invocation of each test method in the class. 29 | [super tearDown]; 30 | } 31 | 32 | - (void)testExample { 33 | 34 | 35 | } 36 | 37 | - (void) testArray { 38 | 39 | ArrayList *array = [ArrayList arrayWithCapacity:10]; 40 | [array addObject:@"A"]; 41 | [array addObject:@"B"]; 42 | [array addObject:@"C"]; 43 | [array addObject:@"D"]; 44 | [array addObject:@"E"]; 45 | [array addObject:@"F"]; 46 | [array addObject:@"G"]; 47 | 48 | NSLog(@"array = %@",array); 49 | 50 | [array insertObject:@"666" atIndex:5]; 51 | 52 | NSLog(@"array = %@",array); 53 | 54 | [array replaceObjectAtIndex:1 withObject:@"123"]; 55 | 56 | NSLog(@"array = %@",array); 57 | 58 | NSLog(@"firstObject = %@",[array firstObject]); 59 | NSLog(@"lastObject = %@",[array lastObject]); 60 | NSLog(@"array[3] = %@",[array objectAtIndex:3]); 61 | 62 | [array removeLastObject]; 63 | 64 | NSLog(@"array = %@",array); 65 | 66 | [array removeObjectAtIndex:0]; 67 | NSLog(@"array = %@",array); 68 | 69 | NSLog(@"array Count = %lu",(unsigned long)[array count]); 70 | 71 | ///快速便利 72 | for (NSString *str in array) { 73 | NSLog(@"%@",str); 74 | } 75 | 76 | } 77 | 78 | 79 | /** 80 | 测试用例 对比 NSMutableArray 比 ArrayList 性能提升 2.42倍 81 | ArrayList 16.90701246261597ms 82 | NSMutableArray 6.979000568389893ms 83 | */ 84 | - (void) testArrayListAndNSArray { 85 | _timeArray = [NSMutableArray array]; 86 | 87 | ///10万次对比 NSMutableArray 和 ArrayList 88 | int number = 100000; 89 | Person *p = [Person new]; 90 | for (int i = 0; i<10; i++) { 91 | CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent(); 92 | ArrayList *array = [ArrayList arrayWithCapacity:number]; 93 | for (int i = 0; i_timeArray addObject:@(duration)]; 101 | [NSThread sleepForTimeInterval:0.3f]; 102 | } 103 | 104 | NSLog(@"ArrayList time is %@",[_timeArray valueForKeyPath:@"@avg.self"]); 105 | 106 | NSLog(@"********************************"); 107 | 108 | [_timeArray removeAllObjects]; 109 | 110 | for (int i = 0; i<10; i++) { 111 | CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent(); 112 | NSMutableArray *array = [NSMutableArray arrayWithCapacity:number]; 113 | for (int i = 0; i_timeArray addObject:@(duration)]; 120 | [NSThread sleepForTimeInterval:0.3f]; 121 | } 122 | 123 | NSLog(@"NSMutableArray time is %@",[_timeArray valueForKeyPath:@"@avg.self"]); 124 | 125 | 126 | 127 | } 128 | 129 | 130 | - (void)testPerformanceExample { 131 | // This is an example of a performance test case. 132 | [self measureBlock:^{ 133 | // Put the code you want to measure the time of here. 134 | }]; 135 | } 136 | 137 | @end 138 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructuresTests/TestStack.m: -------------------------------------------------------------------------------- 1 | // 2 | // TestStack.m 3 | // iOS-DataStructuresTests 4 | // 5 | // Created by dzb on 2018/8/7. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "LinkedListStack.h" 11 | #import "ArrayStack.h" 12 | #import "Person.h" 13 | 14 | @interface TestStack : XCTestCase 15 | { 16 | NSMutableArray *_timeArray; 17 | } 18 | @end 19 | 20 | @implementation TestStack 21 | 22 | - (void)setUp { 23 | // Put setup code here. This method is called before the invocation of each test method in the class. 24 | } 25 | 26 | - (void)tearDown { 27 | // Put teardown code here. This method is called after the invocation of each test method in the class. 28 | } 29 | 30 | - (void)testExample { 31 | // This is an example of a functional test case. 32 | // Use XCTAssert and related functions to verify your tests produce the correct results. 33 | } 34 | 35 | - (void) testStack { 36 | 37 | 38 | _timeArray = [NSMutableArray array]; 39 | 40 | ///10万次对比 NSMutableArray 和 ArrayList 41 | int number = 100000; 42 | Person *p = [Person new]; 43 | for (int i = 0; i<10; i++) { 44 | CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent(); 45 | LinkedListStack *linkStack = [LinkedListStack linkedListStack]; 46 | for (int i = 0; i_timeArray addObject:@(duration)]; 52 | [NSThread sleepForTimeInterval:0.3f]; 53 | } 54 | 55 | NSLog(@"LinkedListStack time is %@",[_timeArray valueForKeyPath:@"@avg.self"]); 56 | 57 | NSLog(@"********************************"); 58 | 59 | [_timeArray removeAllObjects]; 60 | 61 | for (int i = 0; i<10; i++) { 62 | CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent(); 63 | ArrayStack *arrayStack = [ArrayStack arrayStack]; 64 | for (int i = 0; i_timeArray addObject:@(duration)]; 70 | [NSThread sleepForTimeInterval:0.3f]; 71 | } 72 | 73 | NSLog(@"ArrayStack time is %@",[_timeArray valueForKeyPath:@"@avg.self"]); 74 | 75 | 76 | } 77 | 78 | - (void)testPerformanceExample { 79 | // This is an example of a performance test case. 80 | [self measureBlock:^{ 81 | // Put the code you want to measure the time of here. 82 | }]; 83 | } 84 | 85 | @end 86 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructuresTests/iOS_DataStructuresTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // iOS_DataStructuresTests.m 3 | // iOS-DataStructuresTests 4 | // 5 | // Created by dzb on 2018/8/5. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface iOS_DataStructuresTests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation iOS_DataStructuresTests 16 | 17 | - (void)setUp { 18 | [super setUp]; 19 | // Put setup code here. This method is called before the invocation of each test method in the class. 20 | } 21 | 22 | - (void)tearDown { 23 | // Put teardown code here. This method is called after the invocation of each test method in the class. 24 | [super tearDown]; 25 | } 26 | 27 | - (void)testExample { 28 | // This is an example of a functional test case. 29 | // Use XCTAssert and related functions to verify your tests produce the correct results. 30 | } 31 | 32 | - (void)testPerformanceExample { 33 | // This is an example of a performance test case. 34 | [self measureBlock:^{ 35 | // Put the code you want to measure the time of here. 36 | }]; 37 | } 38 | 39 | @end 40 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructuresUITests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /OC版本数据结构/iOS-DataStructures/iOS-DataStructuresUITests/iOS_DataStructuresUITests.m: -------------------------------------------------------------------------------- 1 | // 2 | // iOS_DataStructuresUITests.m 3 | // iOS-DataStructuresUITests 4 | // 5 | // Created by dzb on 2018/8/5. 6 | // Copyright © 2018 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface iOS_DataStructuresUITests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation iOS_DataStructuresUITests 16 | 17 | - (void)setUp { 18 | [super setUp]; 19 | 20 | // Put setup code here. This method is called before the invocation of each test method in the class. 21 | 22 | // In UI tests it is usually best to stop immediately when a failure occurs. 23 | self.continueAfterFailure = NO; 24 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. 25 | [[[XCUIApplication alloc] init] launch]; 26 | 27 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. 28 | } 29 | 30 | - (void)tearDown { 31 | // Put teardown code here. This method is called after the invocation of each test method in the class. 32 | [super tearDown]; 33 | } 34 | 35 | - (void)testExample { 36 | // Use recording to get started writing UI tests. 37 | // Use XCTAssert and related functions to verify your tests produce the correct results. 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /leetcode/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BestiOSDev/MJ_DataStructures/e2bab856549ab1d199b701c4f726cfc3689de201/leetcode/.DS_Store -------------------------------------------------------------------------------- /leetcode/栈/_20_有效的括号/_20_有效的括号.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /leetcode/栈/_20_有效的括号/_20_有效的括号/Stack/ArrayStack.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ArrayStack.hpp 3 | // 02-Array-Stack 4 | // 5 | // Created by dzb on 2019/3/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #ifndef ArrayStack_hpp 10 | #define ArrayStack_hpp 11 | 12 | #include 13 | #include "Stack.hpp" 14 | #include "ArrayList.hpp" 15 | 16 | 17 | template 18 | class ArrayStack : public Stack { 19 | private: 20 | ArrayList *array; 21 | public: 22 | ArrayStack(); 23 | ArrayStack(int capacity); 24 | ~ArrayStack(); 25 | int getSize(); 26 | int getCapacity(); 27 | bool isEmpty(); 28 | void push(E e); 29 | E pop(); 30 | E peek(); 31 | ///输出栈中所有元素 32 | void toString(); 33 | }; 34 | #pragma mark 构造 析构函数 35 | template 36 | ArrayStack::ArrayStack() : ArrayStack(10) { 37 | 38 | } 39 | 40 | template 41 | ArrayStack::ArrayStack(int capacity) { 42 | // cout<<"ArrayStack::ArrayStack(int capacity) "<(capacity); 44 | } 45 | 46 | template 47 | ArrayStack::~ArrayStack() { 48 | if (array != nullptr) { 49 | delete array; 50 | array = nullptr; 51 | } 52 | // cout << "ArrayStack::~ArrayStack()" << endl; 53 | } 54 | 55 | template 56 | int ArrayStack::getSize() { 57 | return array->getSize(); 58 | } 59 | template 60 | bool ArrayStack::isEmpty() { 61 | return array->isEmpty(); 62 | } 63 | 64 | template 65 | int ArrayStack::getCapacity() { 66 | return 0; 67 | } 68 | 69 | template 70 | void ArrayStack::push(E e) { 71 | array->addLast(e); 72 | } 73 | 74 | template 75 | E ArrayStack::pop() { 76 | return array->removeLast(); 77 | } 78 | 79 | template 80 | E ArrayStack::peek() { 81 | return array->getLast(); 82 | } 83 | 84 | template 85 | void ArrayStack::toString() { 86 | cout << "Stack: ["; 87 | for (int i = 0; i getSize(); ++i) { 88 | E t = array->objectAtIndex(i); 89 | if (i != 0) { 90 | cout << ", "; 91 | } 92 | std::cout << t; 93 | } 94 | cout << "] top" << endl; 95 | } 96 | 97 | #endif /* ArrayStack_hpp */ 98 | -------------------------------------------------------------------------------- /leetcode/栈/_20_有效的括号/_20_有效的括号/Stack/Stack.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stack.hpp 3 | // 02-Array-Stack 4 | // 5 | // Created by dzb on 2019/3/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #ifndef Stack_hpp 10 | #define Stack_hpp 11 | 12 | #include 13 | 14 | ///Stakc虚基类 15 | template 16 | class Stack { 17 | 18 | public: 19 | virtual int getSize() { return 0; } 20 | virtual bool isEmpty() { return false; } 21 | virtual void push(E e) { } 22 | virtual E pop() { return (E)(0);} 23 | virtual E peek() { return (E)0; } 24 | }; 25 | 26 | 27 | #endif /* Stack_hpp */ 28 | -------------------------------------------------------------------------------- /leetcode/栈/_20_有效的括号/_20_有效的括号/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // _20_有效的括号 4 | // 5 | // Created by dzb on 2019/4/28. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "Stack/ArrayStack.hpp" 11 | 12 | bool isValid(char *s) { 13 | size_t len = strlen(s); 14 | ArrayStack stack(10); 15 | for(int i = 0 ; i < len ; i ++){ 16 | char c = s[i]; 17 | if(c == '(' || c == '[' || c == '{') 18 | stack.push(c); 19 | else{ 20 | if(stack.isEmpty()) 21 | return false; 22 | 23 | char topChar = stack.pop(); 24 | if(c == ')' && topChar != '(') 25 | return false; 26 | if(c == ']' && topChar != '[') 27 | return false; 28 | if(c == '}' && topChar != '{') 29 | return false; 30 | } 31 | } 32 | return stack.isEmpty(); 33 | } 34 | 35 | int main(int argc, const char * argv[]) { 36 | std::cout << isValid("()[]{}") << std::endl; 37 | std::cout << isValid("([)]") << std::endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /leetcode/栈/_232_用栈实现队列/_232_用栈实现队列.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /leetcode/栈/_232_用栈实现队列/_232_用栈实现队列/Stack/ArrayStack.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // ArrayStack.hpp 3 | // 02-Array-Stack 4 | // 5 | // Created by dzb on 2019/3/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #ifndef ArrayStack_hpp 10 | #define ArrayStack_hpp 11 | 12 | #include 13 | #include "Stack.hpp" 14 | #include "ArrayList.hpp" 15 | 16 | 17 | template 18 | class ArrayStack : public Stack { 19 | private: 20 | ArrayList *array; 21 | public: 22 | ArrayStack(); 23 | ArrayStack(int capacity); 24 | ~ArrayStack(); 25 | int getSize(); 26 | int getCapacity(); 27 | bool isEmpty(); 28 | void push(E e); 29 | E pop(); 30 | E peek(); 31 | ///输出栈中所有元素 32 | void toString(); 33 | }; 34 | #pragma mark 构造 析构函数 35 | template 36 | ArrayStack::ArrayStack() : ArrayStack(10) { 37 | 38 | } 39 | 40 | template 41 | ArrayStack::ArrayStack(int capacity) { 42 | // cout<<"ArrayStack::ArrayStack(int capacity) "<(capacity); 44 | } 45 | 46 | template 47 | ArrayStack::~ArrayStack() { 48 | if (array != nullptr) { 49 | delete array; 50 | array = nullptr; 51 | } 52 | } 53 | 54 | template 55 | int ArrayStack::getSize() { 56 | return array->getSize(); 57 | } 58 | template 59 | bool ArrayStack::isEmpty() { 60 | return array->isEmpty(); 61 | } 62 | 63 | template 64 | int ArrayStack::getCapacity() { 65 | return 0; 66 | } 67 | 68 | template 69 | void ArrayStack::push(E e) { 70 | array->addLast(e); 71 | } 72 | 73 | template 74 | E ArrayStack::pop() { 75 | return array->removeFirst(); 76 | } 77 | 78 | template 79 | E ArrayStack::peek() { 80 | return array->getLast(); 81 | } 82 | 83 | template 84 | void ArrayStack::toString() { 85 | cout << "Stack: ["; 86 | for (int i = 0; i getSize(); ++i) { 87 | E t = array->objectAtIndex(i); 88 | if (i != 0) { 89 | cout << ", "; 90 | } 91 | std::cout << t; 92 | } 93 | cout << "] top" << endl; 94 | } 95 | 96 | #endif /* ArrayStack_hpp */ 97 | -------------------------------------------------------------------------------- /leetcode/栈/_232_用栈实现队列/_232_用栈实现队列/Stack/Stack.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stack.hpp 3 | // 02-Array-Stack 4 | // 5 | // Created by dzb on 2019/3/3. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #ifndef Stack_hpp 10 | #define Stack_hpp 11 | 12 | #include 13 | 14 | ///Stakc虚基类 15 | template 16 | class Stack { 17 | 18 | public: 19 | virtual int getSize() { return 0; } 20 | virtual bool isEmpty() { return false; } 21 | virtual void push(E e) { } 22 | virtual E pop() { return (E)(0);} 23 | virtual E peek() { return (E)0; } 24 | }; 25 | 26 | 27 | #endif /* Stack_hpp */ 28 | -------------------------------------------------------------------------------- /leetcode/栈/_232_用栈实现队列/_232_用栈实现队列/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // _232_用栈实现队列 4 | // 5 | // Created by dzb on 2019/4/28. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "Stack/ArrayStack.hpp" 11 | 12 | int main(int argc, const char * argv[]) { 13 | 14 | ArrayStack *stack = new ArrayStack(); 15 | stack->push(11); 16 | stack->push(22); 17 | stack->push(33); 18 | stack->push(44); 19 | 20 | while (!stack->isEmpty()) { 21 | int a = stack->pop(); 22 | std::cout << a << std::endl; 23 | } 24 | 25 | stack->toString(); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/链表/203_linkedlistremove/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BestiOSDev/MJ_DataStructures/e2bab856549ab1d199b701c4f726cfc3689de201/leetcode/链表/203_linkedlistremove/.DS_Store -------------------------------------------------------------------------------- /leetcode/链表/203_linkedlistremove/203_linkedlistremove.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /leetcode/链表/203_linkedlistremove/203_linkedlistremove.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /leetcode/链表/203_linkedlistremove/203_linkedlistremove/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // 203_linkedlistremove 4 | // 5 | // Created by dzb on 2019/4/21. 6 | // Copyright © 2019年 dzb. All rights reserved. 7 | // 8 | ///leetcode 203. 移除链表元素 https://leetcode-cn.com/problems/remove-linked-list-elements/ 9 | 10 | 11 | #include 12 | 13 | class Solution { 14 | class ListNode { 15 | public: 16 | int val; 17 | ListNode *next; 18 | ListNode(int x) : val(x), next(NULL) {} 19 | }; 20 | public: 21 | ListNode *m_head; 22 | int m_size; 23 | Solution() { 24 | m_head = new Solution::ListNode(0); 25 | m_size = 0; 26 | } 27 | void add(int val) { 28 | add(val,m_size); 29 | } 30 | ListNode *node(int index) { 31 | Solution::ListNode *pFind = m_head->next; 32 | for (int i = 0; inext; 34 | } 35 | return pFind; 36 | } 37 | void add(int val,int index) { 38 | if (index == 0) { 39 | Solution::ListNode *pFind = node(index); 40 | Solution::ListNode *cur = new Solution::ListNode(val); 41 | cur->next = pFind; 42 | m_head->next = cur; 43 | } else { 44 | //待添加节点的前一个节点 45 | Solution::ListNode *pre = node(index-1); 46 | Solution::ListNode *cur = new Solution::ListNode(val); 47 | cur->next = pre->next; 48 | pre->next = cur; 49 | } 50 | m_size++; 51 | } 52 | 53 | int indexOf(int val) { 54 | Solution::ListNode *cur = m_head->next; 55 | for (int i = 0; ival) { 57 | return i; 58 | } 59 | cur = cur->next; 60 | } 61 | return -1; 62 | } 63 | 64 | void remove(int index) { 65 | Solution::ListNode *cur = NULL; //要删除的节点 66 | if (index == 0) { 67 | cur = node(index); 68 | m_head->next = cur->next; 69 | } else { 70 | ///待删除节点前一个节点 71 | Solution::ListNode *pre = node(index-1); 72 | cur = pre->next; 73 | pre->next = cur->next; 74 | } 75 | m_size--; 76 | delete cur; //删除节点 对于节点数据需要开发者自行管理内存 77 | } 78 | 79 | ListNode* removeElements(ListNode* head, int val) { 80 | 81 | int idx = indexOf(val); 82 | if (idx == -1) { 83 | return m_head; 84 | } else { 85 | remove(idx); 86 | } 87 | 88 | return removeElements(m_head,val); 89 | } 90 | }; 91 | 92 | int main(int argc, const char * argv[]) { 93 | 94 | Solution *list = new Solution(); 95 | list->add(1); 96 | list->add(2); 97 | list->add(6); 98 | list->add(3); 99 | list->add(4); 100 | list->add(5); 101 | list->add(6); 102 | 103 | list->removeElements(list->m_head,6); 104 | 105 | delete list; 106 | 107 | return 0; 108 | } 109 | -------------------------------------------------------------------------------- /leetcode/链表/83_删除排序链表中的重复元素/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BestiOSDev/MJ_DataStructures/e2bab856549ab1d199b701c4f726cfc3689de201/leetcode/链表/83_删除排序链表中的重复元素/.DS_Store -------------------------------------------------------------------------------- /leetcode/链表/83_删除排序链表中的重复元素/83_删除排序链表中的重复元素.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /leetcode/链表/83_删除排序链表中的重复元素/83_删除排序链表中的重复元素.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /leetcode/链表/83_删除排序链表中的重复元素/83_删除排序链表中的重复元素/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // 83_删除排序链表中的重复元素 4 | // 5 | // Created by dzb on 2019/4/21. 6 | // Copyright © 2019年 dzb. All rights reserved. 7 | // 8 | ///leetcode 83_删除排序链表中的重复元素 https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/ 9 | 10 | #include 11 | 12 | ///代码提交到leetcode 报错 这是完整代码实现 利用递归去删除元素 13 | class Solution { 14 | class ListNode { 15 | public: 16 | int val; 17 | ListNode *next; 18 | ~ListNode() { 19 | std::cout<<"~ListNode()"<next; 35 | for (int i = 0; inext; 37 | } 38 | return pFind; 39 | } 40 | void add(int val,int index) { 41 | if (index == 0) { 42 | Solution::ListNode *pFind = node(index); 43 | Solution::ListNode *cur = new Solution::ListNode(val); 44 | cur->next = pFind; 45 | m_head->next = cur; 46 | } else { 47 | //待添加节点的前一个节点 48 | Solution::ListNode *pre = node(index-1); 49 | Solution::ListNode *cur = new Solution::ListNode(val); 50 | cur->next = pre->next; 51 | pre->next = cur; 52 | } 53 | m_size++; 54 | } 55 | 56 | int indexOf(int val) { 57 | Solution::ListNode *cur = m_head->next; 58 | for (int i = 0; ival) { 60 | return i; 61 | } 62 | cur = cur->next; 63 | } 64 | return -1; 65 | } 66 | 67 | void remove(int index) { 68 | Solution::ListNode *cur = NULL; //要删除的节点 69 | if (index == 0) { 70 | cur = node(index); 71 | m_head->next = cur->next; 72 | } else { 73 | ///待删除节点前一个节点 74 | Solution::ListNode *pre = node(index-1); 75 | cur = pre->next; 76 | pre->next = cur->next; 77 | } 78 | m_size--; 79 | delete cur; //删除节点 对于节点数据需要开发者自行管理内存 80 | } 81 | 82 | ListNode* deleteDuplicates(ListNode* head) { 83 | 84 | if (head->next == NULL) { 85 | return m_head; 86 | } 87 | ListNode *pre = head; 88 | ListNode *cur = pre->next; 89 | if (cur == NULL || pre == NULL) { 90 | return m_head; 91 | } 92 | 93 | std::cout<<"pre = " << pre->val << "cur = " << cur->val << std::endl; 94 | 95 | if (pre->val == cur->val) { //元素重复 96 | pre->next = cur->next; 97 | delete cur; 98 | m_head = pre; 99 | } else { 100 | m_head = cur; 101 | } 102 | 103 | return deleteDuplicates(m_head); 104 | } 105 | 106 | 107 | 108 | }; 109 | 110 | int main(int argc, const char * argv[]) { 111 | Solution *list = new Solution(); 112 | list->add(1); 113 | list->add(2); 114 | list->add(2); 115 | list->add(3); 116 | list->add(4); 117 | list->add(4); 118 | list->add(5); 119 | list->add(6); 120 | list->add(6); 121 | list->deleteDuplicates(list->m_head); 122 | 123 | delete list; 124 | return 0; 125 | } 126 | -------------------------------------------------------------------------------- /leetcode/链表/876_链表的中间结点/876_链表的中间结点.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /leetcode/链表/876_链表的中间结点/876_链表的中间结点.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /leetcode/链表/876_链表的中间结点/876_链表的中间结点/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // 83_删除排序链表中的重复元素 4 | // 5 | // Created by dzb on 2019/4/21. 6 | // Copyright © 2019年 dzb. All rights reserved. 7 | // 8 | ///leetcode 876. 链表的中间结点 https://leetcode-cn.com/problems/middle-of-the-linked-list/ 9 | 10 | #include 11 | 12 | ///代码提交到leetcode 报错 这是完整代码实现 利用递归去删除元素 13 | class Solution { 14 | class ListNode { 15 | public: 16 | int val; 17 | ListNode *next; 18 | ~ListNode() { 19 | std::cout<<"~ListNode()"<next; 35 | for (int i = 0; inext; 37 | } 38 | return pFind; 39 | } 40 | void add(int val,int index) { 41 | if (index == 0) { 42 | Solution::ListNode *pFind = node(index); 43 | Solution::ListNode *cur = new Solution::ListNode(val); 44 | cur->next = pFind; 45 | m_head->next = cur; 46 | } else { 47 | //待添加节点的前一个节点 48 | Solution::ListNode *pre = node(index-1); 49 | Solution::ListNode *cur = new Solution::ListNode(val); 50 | cur->next = pre->next; 51 | pre->next = cur; 52 | } 53 | m_size++; 54 | } 55 | 56 | int indexOf(int val) { 57 | Solution::ListNode *cur = m_head->next; 58 | for (int i = 0; ival) { 60 | return i; 61 | } 62 | cur = cur->next; 63 | } 64 | return -1; 65 | } 66 | 67 | void remove(int index) { 68 | Solution::ListNode *cur = NULL; //要删除的节点 69 | if (index == 0) { 70 | cur = node(index); 71 | m_head->next = cur->next; 72 | } else { 73 | ///待删除节点前一个节点 74 | Solution::ListNode *pre = node(index-1); 75 | cur = pre->next; 76 | pre->next = cur->next; 77 | } 78 | m_size--; 79 | delete cur; //删除节点 对于节点数据需要开发者自行管理内存 80 | } 81 | 82 | int listlen(ListNode *head) { 83 | 84 | int length = 0; 85 | Solution::ListNode *cur = head->next; 86 | while (cur != NULL) { 87 | cur = cur->next; 88 | length++; 89 | } 90 | 91 | return length; 92 | } 93 | 94 | ListNode *node1(ListNode* head,int index) { 95 | ListNode *pFind = head->next; 96 | for (int i = 0; inext; 98 | } 99 | return pFind; 100 | } 101 | 102 | ListNode* middleNode(ListNode* head) { 103 | ListNode* slow = head; 104 | ListNode* fast = head; 105 | while (fast != NULL && fast->next != NULL) { 106 | slow = slow->next; 107 | fast = fast->next->next; 108 | } 109 | return slow; 110 | } 111 | 112 | 113 | ListNode* removeElements(ListNode* head, int val) { 114 | 115 | int idx = indexOf(val); 116 | if (idx == -1) { 117 | return m_head; 118 | } else { 119 | remove(idx); 120 | } 121 | 122 | return removeElements(m_head,val); 123 | } 124 | 125 | 126 | }; 127 | 128 | int main(int argc, const char * argv[]) { 129 | Solution *list = new Solution(); 130 | list->add(1); 131 | list->add(2); 132 | list->add(3); 133 | list->add(4); 134 | list->add(5); 135 | // list->add(6); 136 | // list->add(7); 137 | // list->add(8); 138 | // list->add(9); 139 | // list->add(10); 140 | list->middleNode(list->m_head); 141 | 142 | delete list; 143 | return 0; 144 | } 145 | -------------------------------------------------------------------------------- /leetcode/链表/_141_环形链表/_141_环形链表.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /leetcode/链表/_141_环形链表/_141_环形链表/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // _141_环形链表 4 | // 5 | // Created by dzb on 2019/4/28. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | struct ListNode { 12 | public : 13 | ListNode *next; 14 | int data; 15 | }; 16 | 17 | bool hasCycle(ListNode *head) { 18 | if (head == NULL || head->next == NULL) return false; 19 | 20 | ListNode *slow = head; 21 | ListNode *fast = head->next; 22 | while (fast != NULL && fast->next != NULL) { 23 | slow = slow->next; 24 | fast = fast->next->next; 25 | 26 | if (slow == fast) return true; 27 | } 28 | 29 | return false; 30 | } 31 | 32 | int main(int argc, const char * argv[]) { 33 | 34 | 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /leetcode/链表/_206_反转链表/_206_反转链表.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /leetcode/链表/_206_反转链表/_206_反转链表/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // _206_反转链表 4 | // 5 | // Created by dzb on 2019/4/28. 6 | // Copyright © 2019 大兵布莱恩特. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | struct ListNode { 12 | public: 13 | ListNode *next; 14 | int data; 15 | }; 16 | 17 | 18 | ListNode * reverseList(ListNode *head) { 19 | if (head == NULL || head->next == NULL) return head; 20 | 21 | ListNode *newHead = reverseList(head->next); 22 | head->next->next = head; 23 | head->next = NULL; 24 | return newHead; 25 | } 26 | 27 | ListNode * reverseList2(ListNode *head) { 28 | if (head == NULL || head->next == NULL) return head; 29 | 30 | ListNode *newHead = NULL; 31 | while (head != NULL) { 32 | ListNode *tmp = head->next; 33 | head->next = newHead; 34 | newHead = head; 35 | head = tmp; 36 | } 37 | 38 | return newHead; 39 | } 40 | 41 | int main(int argc, const char * argv[]) { 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## 课程源码目录 4 | 5 | | **第一章 线性表 ** | [无代码] | 6 | | :--- | :---: | 7 | | day-1 复杂度,静态数组 | [C++](MJ恋上数据结构/day1/02_ArrayList) 8 | | day-2 动态数组、ArrayList自实现及源码分析 | [C++](MJ恋上数据结构/day2/02_ArrayList) | 9 | | day-3 单向链表、双向链表、LinkedList自实现及源码分析 | [C++](MJ恋上数据结构/day3/LinkedList) 10 | | day-4 单向循环链表、双向循环链表、静态链表 | [C++](MJ恋上数据结构/day4/LinkedList) | 11 | | day-5 栈、Stack自实现及源码分析 | [C++](MJ恋上数据结构/day5/02-Array-Stack) | 12 | | day-6 队列、循环队列 | [C++](MJ恋上数据结构/day6/08-Queues-Comparison)| 13 | 14 | --- 15 | 16 | 代码在运行时候难免会出现 Bug ,如果学习过程中发现代码有问题,请及时联系作者 QQ:1060545231 17 | 18 | 小码哥恋上数据结构第一季 19 | https://ke.qq.com/course/385223#term_id=100459236 20 | 21 | 不懂C++语法的 可以学30小时快速精通C++语言 也可以QQ联系作者 22 | 23 | https://ke.qq.com/course/336509 24 | 25 | **大家加油!:)** 26 | --------------------------------------------------------------------------------