├── .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