├── .idea ├── .gitignore ├── description.html ├── dictionaries │ └── Shaoxiong.xml ├── encodings.xml ├── inspectionProfiles │ └── Project_Default.xml ├── misc.xml ├── modules.xml ├── project-template.xml ├── uiDesigner.xml └── vcs.xml ├── DataStructureForJava.iml ├── LICENSE ├── image └── 数据结构与算法Java实现版.jpg ├── out └── production │ └── DataStructureForJava │ └── cn │ └── shaoxiongdu │ ├── ArrayList.class │ ├── ArrayListTestMain.class │ ├── ArrayQueue.class │ ├── ArrayQueueTestMain.class │ ├── ArrayStack.class │ ├── ArrayStackTestMain.class │ ├── HashMap$Node.class │ ├── HashMap.class │ ├── HashMapTestMain.class │ ├── LinkedList$Node.class │ ├── LinkedList.class │ ├── LinkedListTestMain.class │ ├── LinkedQueue$Node.class │ ├── LinkedQueue.class │ ├── LinkedQueueTestMain.class │ ├── LinkedStack$Node.class │ ├── LinkedStack.class │ ├── LinkedStackTestMain.class │ ├── List.class │ ├── Queue.class │ ├── RandomIntArray.class │ ├── Stack.class │ ├── Student.class │ ├── TestMainFind.class │ ├── TestMainQueue.class │ ├── TestMainStack.class │ └── 冒泡排序 │ └── PopSort.class ├── readme.md ├── 常见排序算法 └── src │ └── cn │ └── shaoxiongdu │ ├── RandomIntArray.java │ ├── 冒泡排序 │ └── PopSort.java │ ├── 归并排序 │ └── MergeSortt.java │ └── 快速排序 │ ├── QuickSort.java │ └── readme.md ├── 常见查找算法 └── src │ └── cn │ └── shaoxiongdu │ ├── TestMainFind.java │ └── 顺序表查找 │ └── Find.java ├── 散列 └── src │ └── cn │ └── shaoxiongdu │ ├── HashMap.java │ └── HashMapTestMain.java ├── 栈 └── src │ └── cn │ └── shaoxiongdu │ ├── ArrayStack.java │ ├── ArrayStackTestMain.java │ ├── LinkedStack.java │ ├── LinkedStackTestMain.java │ ├── Stack.java │ └── TestMainStack.java ├── 线性表 └── src │ └── cn │ └── shaoxiongdu │ ├── ArrayList.java │ ├── ArrayListTestMain.java │ ├── LinkedList.java │ ├── LinkedListTestMain.java │ ├── List.java │ └── Student.java └── 队列 └── src └── cn └── shaoxiongdu ├── ArrayQueue.java ├── ArrayQueueTestMain.java ├── LinkedQueue.java ├── LinkedQueueTestMain.java ├── Queue.java └── TestMainQueue.java /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # 默认忽略的文件 2 | /shelf/ 3 | /workspace.xml 4 | # 数据源本地存储已忽略文件 5 | /../../../../:\JavaProject\DataStructureForJava\.idea/dataSources/ 6 | /dataSources.local.xml 7 | # 基于编辑器的 HTTP 客户端请求 8 | /httpRequests/ 9 | -------------------------------------------------------------------------------- /.idea/description.html: -------------------------------------------------------------------------------- 1 | Simple Java application that includes a class with main() method -------------------------------------------------------------------------------- /.idea/dictionaries/Shaoxiong.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | eckel 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 36 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/project-template.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 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 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /DataStructureForJava.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /image/数据结构与算法Java实现版.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/image/数据结构与算法Java实现版.jpg -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/ArrayList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/ArrayList.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/ArrayListTestMain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/ArrayListTestMain.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/ArrayQueue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/ArrayQueue.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/ArrayQueueTestMain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/ArrayQueueTestMain.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/ArrayStack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/ArrayStack.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/ArrayStackTestMain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/ArrayStackTestMain.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/HashMap$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/HashMap$Node.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/HashMap.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/HashMap.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/HashMapTestMain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/HashMapTestMain.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/LinkedList$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/LinkedList$Node.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/LinkedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/LinkedList.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/LinkedListTestMain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/LinkedListTestMain.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/LinkedQueue$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/LinkedQueue$Node.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/LinkedQueue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/LinkedQueue.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/LinkedQueueTestMain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/LinkedQueueTestMain.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/LinkedStack$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/LinkedStack$Node.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/LinkedStack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/LinkedStack.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/LinkedStackTestMain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/LinkedStackTestMain.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/List.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/List.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/Queue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/Queue.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/RandomIntArray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/RandomIntArray.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/Stack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/Stack.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/Student.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/Student.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/TestMainFind.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/TestMainFind.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/TestMainQueue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/TestMainQueue.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/TestMainStack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/TestMainStack.class -------------------------------------------------------------------------------- /out/production/DataStructureForJava/cn/shaoxiongdu/冒泡排序/PopSort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaoxiongdu/java-datastructure-algorithm/58ce99669218e65e35e181716a4aacd0366c936c/out/production/DataStructureForJava/cn/shaoxiongdu/冒泡排序/PopSort.class -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # DataStructureForJava 2 | 3 | ![数据结构与算法Java实现版](https://gitee.com/ShaoxiongDu/imageBed/raw/master/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95Java%E5%AE%9E%E7%8E%B0%E7%89%88.jpg) 4 | 5 | ## 主要内容 6 | 7 | ### 常用数据结构及其算法的Java实现,包括但不仅限于以下部分: 8 | 9 | >- ### 线性表 10 | > 11 | > - 顺序存储结构实现 12 | > - 链表存储结构实现 13 | > 14 | >- ### 栈 15 | > 16 | > - 顺序存储结果实现 17 | > - 链式存储结构实现 18 | > 19 | >- ### 队列 20 | > 21 | > - 链式存储结构实现 22 | > - 线性存储结构实现 23 | > 24 | >- ### 散列 25 | > - 使用数组和链表结合实现散列表HashMap 26 | > 27 | >- ### 查找算法 待开发... 28 | > - 顺序表查找 29 | > - 有序表查找 30 | > - 线性索引查找 31 | > - 二叉排序树 32 | > - 平衡二叉树 33 | > - 多路查找树 34 | > - 散列表(哈希表)查找 35 | > 36 | >- ### 排序算法 37 | > 38 | > - 冒泡排序 39 | > - 简单选择排序 40 | > - 直接插入排序 41 | > - 希尔排序 42 | > - 堆排序 43 | > - 归并排序 44 | > - 快速排序 45 | 46 | ## 参与贡献 47 | 1. Fork 本仓库 48 | 2. 新建 Feat_xxx 分支 49 | 3. 提交代码 50 | 4. 新建 Pull Request 51 | 52 | ## 参考书籍 53 | 54 | | 书名 | 作者 | 译 | 出版社 | ISBN | 55 | | ---------------------------------- | ---------------- | ------ | -------------- | ----------------- | 56 | | 《大话数据结构》 | 程杰 | | 清华大学出版社 | 978-7-302-25565-9 | 57 | | 《数据结构与算法分析Java语言描述》 | 马克思·艾伦·维斯 | 陈越 | 机械工业出版社 | 978-7-111-52839-5 | 58 | | 《Java编程思想》 | Bruce Eckel | 陈昊鹏 | 机械工业出版社 | 978-7-111-2138-6 | 59 | 60 | ## 说明 61 | 62 | 目前,项目并没有完成,仍在继续更新! 63 | -------------------------------------------------------------------------------- /常见排序算法/src/cn/shaoxiongdu/RandomIntArray.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | import java.util.Random; 4 | 5 | /** 6 | * ClassName : RandomIntArray 7 | * (c)CopyRight 2021/4/22 All rights reserved to ShaoxiongDu 8 | * 随机数序列类 9 | */ 10 | public class RandomIntArray { 11 | 12 | private int[] numbers; 13 | 14 | /** 15 | * 生成指定长度,最大max的随机数 数组对象 16 | * @param arrayLength 数组长度 17 | * @param max 随机数最大值 18 | */ 19 | public RandomIntArray(int arrayLength, int max) { 20 | Random random = new Random(); 21 | int [] numbers = new int[arrayLength]; 22 | for (int i = 0; i < arrayLength; i++) { 23 | numbers[i] = random.nextInt(max); 24 | } 25 | this.numbers = numbers; 26 | } 27 | 28 | /** 29 | * 无参构造 默认生成10个 最大值为100的随机数序列 30 | */ 31 | public RandomIntArray() { 32 | this(10,100); 33 | } 34 | 35 | /** 36 | * 迭代打印序列 37 | */ 38 | public void printArray(){ 39 | for (int i = 0; i < numbers.length; i++) { 40 | System.out.print(numbers[i] + "\t"); 41 | } 42 | System.out.println(); 43 | } 44 | 45 | /** 46 | * 返回序列 47 | * @return 48 | */ 49 | public int[] getNumbers(){ 50 | return numbers; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /常见排序算法/src/cn/shaoxiongdu/冒泡排序/PopSort.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu.冒泡排序; 2 | 3 | import cn.shaoxiongdu.RandomIntArray; 4 | 5 | /** 6 | * ClassName : Pop 7 | * (c)CopyRight 2021/4/22 All rights reserved to ShaoxiongDu 8 | * 冒泡排序 9 | */ 10 | public class PopSort{ 11 | 12 | 13 | public static void sort(int[] numbers) { 14 | 15 | for (int i = 0; i < numbers.length - 1; i++) { 16 | 17 | for (int j = 0; j < numbers.length - i - 1; j++) { 18 | 19 | if(numbers[j] > numbers[j+1]){ 20 | 21 | int temp = numbers[j]; 22 | numbers[j] = numbers[j+1]; 23 | numbers[j+1] = temp; 24 | 25 | } 26 | 27 | } 28 | 29 | } 30 | 31 | } 32 | 33 | public static void main(String[] args) { 34 | 35 | RandomIntArray randomIntArray = new RandomIntArray(20,100); 36 | 37 | randomIntArray.printArray(); 38 | 39 | sort(randomIntArray.getNumbers()); 40 | 41 | randomIntArray.printArray(); 42 | } 43 | 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /常见排序算法/src/cn/shaoxiongdu/归并排序/MergeSortt.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu.归并排序; 2 | 3 | import cn.shaoxiongdu.RandomIntArray; 4 | 5 | import java.util.Arrays; 6 | 7 | /** 8 | * @author zyc 9 | * @date 2022/10/8 10 | */ 11 | public class MergeSortt { 12 | 13 | public static void main(String[] args) { 14 | RandomIntArray in = new RandomIntArray(); 15 | int[] numbers = in.getNumbers(); 16 | MergeSortt.MergeSort(numbers); 17 | Arrays.stream(numbers).forEach(i -> System.out.print(i + " ")); 18 | } 19 | 20 | /** 21 | * 二路归并排序 22 | * 23 | * @param arr 待排序数组 24 | */ 25 | public static void MergeSort(int[] arr) { 26 | int[] work = new int[arr.length];//工作空间数组 27 | MergeSort(arr, 0, arr.length - 1, work); 28 | } 29 | 30 | //int[] arr = {2, 4, 7, 1, 6, 9, 0}; 31 | private static void MergeSort(int[] arr, int left, int right, int[] work) { 32 | if (left < right) { 33 | int mid = left + (right - left) / 2; 34 | MergeSort(arr, left, mid, work); 35 | MergeSort(arr, mid + 1, right, work); 36 | if (arr[mid] <= arr[mid + 1])//原本就是有序的 37 | return; 38 | MergeTwoArr(arr, left, mid, right, work); 39 | } 40 | } 41 | 42 | private static void MergeTwoArr(int[] arr, int left, int mid, int right, int[] work) { 43 | //拷贝原数组到工作数组 44 | System.arraycopy(arr, left, work, left, right - left + 1); 45 | int i = left, j = mid + 1, k; 46 | for (k = i; i <= mid && j <= right; ++k) { 47 | //进行归并 48 | if (work[i] <= work[j]) { 49 | arr[k] = work[i++]; 50 | } else { 51 | arr[k] = work[j++]; 52 | } 53 | } 54 | while (i <= mid) 55 | arr[k++] = work[i++]; 56 | while (j <= right) 57 | arr[k++] = work[j++]; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /常见排序算法/src/cn/shaoxiongdu/快速排序/QuickSort.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu.快速排序; 2 | 3 | import cn.shaoxiongdu.RandomIntArray; 4 | 5 | /** 6 | * ClassName : QuickSort 快速排序 7 | * (c)CopyRight 2021/5/7 All rights reserved to ShaoxiongDu 8 | */ 9 | public class QuickSort{ 10 | 11 | /** 12 | * 快速排序 13 | * @param number 需要排序的序列 14 | */ 15 | public static void sort(int[] number){ 16 | 17 | //递归调用快排 18 | quickSort(number,0, number.length-1); 19 | 20 | } 21 | 22 | /** 23 | * 递归快速排序 24 | * @param number 需要排序的数组 25 | * @param begin 开始部分 26 | * @param end 结束部分 27 | */ 28 | public static void quickSort(int [] number, int begin, int end){ 29 | 30 | //当左右下标重合时,结束递归 31 | if(begin >= end) return; 32 | 33 | //选出基准数字(第一个) 34 | int pivot = number[begin]; 35 | //左右双下标 36 | int left = begin, right = end; 37 | 38 | while (left < right){ 39 | 40 | //找出右部分中的第一个小于基准数字的值 41 | while (left < right && number[right] > pivot){ 42 | right--; 43 | } 44 | //到此处时,right处的数字要比基准数字小 45 | //则将right处的数字放到左边 46 | if(left < right){ 47 | number[left] = number[right]; 48 | //将左边下标++ 49 | left++; 50 | } 51 | 52 | //找出左部分中的第一个大于基准数字的值 53 | while (left < right && number[left] < pivot) { 54 | left++; 55 | } 56 | //到此处时,left处的数字要比基准数字大 57 | //则将left处的数字放到右边 58 | if (left < right) { 59 | number[right] = number[left]; 60 | //将右边下标-- 61 | right--; 62 | } 63 | 64 | } 65 | 66 | //此时,pivot左半部分的所有数字比pivot小,右边部分所有数字比pivot大 67 | //将pivot数字放入到左右下标相交的位置 68 | number[left] = pivot; 69 | //将pivot左部分序列递归调用进行排序 70 | quickSort(number, begin, left - 1); 71 | //将pivot右部分序列递归调用进行排序 72 | quickSort(number, left + 1, end); 73 | 74 | } 75 | 76 | /** 77 | * 测试 78 | * @param args 参数 79 | */ 80 | public static void main(String[] args) { 81 | 82 | //生成随机数字序列 83 | RandomIntArray randomIntArray = new RandomIntArray(20,100); 84 | //输入随机数字序列 85 | randomIntArray.printArray(); //86 48 39 41 2 97 97 22 77 5 86 66 63 34 59 3 53 87 94 92 86 | //调用快速排序 87 | sort(randomIntArray.getNumbers()); 88 | //测试结果 89 | randomIntArray.printArray(); //2 3 5 22 34 39 41 48 53 59 63 66 77 86 86 87 92 94 97 97 90 | 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /常见排序算法/src/cn/shaoxiongdu/快速排序/readme.md: -------------------------------------------------------------------------------- 1 | # 快速排序 2 | 3 | ## 基本思想: 4 | > 通过一趟排序将待排序的记录分成左右两部分,并且保证左半部分均比右半部分小, 5 | > 则可分别对这两个部分继续进行排序,直到达到整个序列有序。 6 | 7 | ## 原理: 8 | > 1. 在待排序的数列中,首先要找一个数字作为基准数pivot(一般为第一个数字) 9 | > 2. 接下来我们把这个待排序的数列中小于基准数的元素移动到待排序的数列的左边,把大于基准数的元素移动到待排序的数列的右边。 10 | > 3. 接着把两个分区的元素分别按照上面两种方法继续对每个分区找出基准数,然后移动,直到各个分区只有一个数时为止。 -------------------------------------------------------------------------------- /常见查找算法/src/cn/shaoxiongdu/TestMainFind.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | /** 4 | * ClassName : TestMain 5 | * (c)CopyRight 2021/4/12 All rights reserved to ShaoxiongDu 6 | */ 7 | public class TestMainFind { 8 | 9 | public static void main(String[] args) { 10 | 11 | System.out.println("Hello DataStructure"); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /常见查找算法/src/cn/shaoxiongdu/顺序表查找/Find.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu.顺序表查找; 2 | 3 | import cn.shaoxiongdu.RandomIntArray; 4 | 5 | import java.util.Arrays; 6 | 7 | /** 8 | * ClassName : Find 9 | * (c)CopyRight 2021/5/7 All rights reserved to ShaoxiongDu 10 | */ 11 | public class Find { 12 | 13 | /** 14 | * 顺序表查找 15 | * @param number 待查找的序列 16 | * @param target 目标数字 17 | * @return 返回目标数字的下标 若不存在 则返回-1 18 | */ 19 | public static int find(int [] number, int target){ 20 | 21 | for (int i = 0; i < number.length; i++) { 22 | 23 | if(target == number[i]){ 24 | return i; 25 | } 26 | 27 | } 28 | return -1; 29 | 30 | } 31 | 32 | public static void main(String[] args) { 33 | 34 | RandomIntArray randomIntArray = new RandomIntArray(30, 10); 35 | int [] number = randomIntArray.getNumbers(); 36 | System.out.println("序列为\n"); 37 | randomIntArray.printArray(); 38 | 39 | int target = 5; 40 | 41 | System.out.println("数字" + target + "所在下标为" + find(number, target)); 42 | 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /散列/src/cn/shaoxiongdu/HashMap.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | import java.io.Serializable; 4 | import java.util.Objects; 5 | 6 | /** 7 | * ClassName : HashMap 8 | * (c)CopyRight 2021/4/21 All rights reserved to ShaoxiongDu 9 | * @author Shaoxiong 10 | */ 11 | public class HashMap implements Serializable { 12 | 13 | /** 14 | * HashMap 内部节点类 15 | */ 16 | private class Node{ 17 | 18 | /** 19 | * 节点的哈希值 20 | */ 21 | private int hash; 22 | 23 | /** 24 | * 节点KEY 25 | */ 26 | private Object key; 27 | 28 | /** 29 | * 节点value 30 | */ 31 | private Object value; 32 | 33 | /** 34 | * 下一节点的引用 35 | */ 36 | private Node next; 37 | 38 | @Override 39 | public boolean equals(Object o) { 40 | if (this == o) { 41 | return true; 42 | } 43 | if (o == null || getClass() != o.getClass()) { 44 | return false; 45 | } 46 | Node node = (Node) o; 47 | return Objects.equals(key, node.key) && Objects.equals(value, node.value); 48 | } 49 | 50 | @Override 51 | public int hashCode() { 52 | return Objects.hashCode(key) ^ Objects.hashCode(value); 53 | } 54 | 55 | @Override 56 | public String toString() { 57 | return key + "=" + value; 58 | } 59 | 60 | public Object getKey() { 61 | return key; 62 | } 63 | 64 | 65 | public Object getValue() { 66 | return value; 67 | } 68 | 69 | public Object setValue(Object newValue) { 70 | Object oldValue = this.value; 71 | this.value = newValue; 72 | return oldValue; 73 | } 74 | 75 | public Node(int hash, Object key, Object value, Node next) { 76 | this.hash = hash; 77 | this.key = key; 78 | this.value = value; 79 | this.next = next; 80 | } 81 | 82 | 83 | 84 | } 85 | 86 | private static final long serialVersionUID = 1L; 87 | 88 | /** 89 | * 默认初始容量 16 90 | */ 91 | private static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; 92 | 93 | /** 94 | * 最大容量 2^30 = 1,073,741,824 95 | */ 96 | private static final int MAX_CAPACITY = 1 << 30; 97 | 98 | /** 99 | * 默认负载因子 当当前元素个数达到容量的此倍数时,进行扩容 100 | */ 101 | static final float DEFAULT_LOAD_FACTOR = 0.75f; 102 | 103 | /** 104 | * 默认扩大倍数 2 105 | */ 106 | static final int DEFAULT_EXPAND_MULTIPLE = 1 << 1; 107 | 108 | /** 109 | * 数组 110 | */ 111 | private Node [] table; 112 | 113 | /** 114 | * 容量 115 | */ 116 | private int capacity; 117 | 118 | /** 119 | * 加载因子 120 | */ 121 | private float loadFactor; 122 | 123 | /** 124 | * 已经占用的数组个数 125 | */ 126 | private int countOfOccupiedArrays = 0; 127 | 128 | /** 129 | * key-value 数目 130 | */ 131 | private int size; 132 | 133 | /** 134 | * 使用默认数据创建散列表 135 | */ 136 | public HashMap() throws Exception { 137 | this(DEFAULT_INITIAL_CAPACITY,DEFAULT_LOAD_FACTOR); 138 | } 139 | 140 | public HashMap(int capacity) throws Exception { 141 | this(capacity,DEFAULT_LOAD_FACTOR); 142 | } 143 | 144 | /** 145 | * 使用指定数据创建散列表 146 | * @param initialCapacity 初始化容量 147 | * @param loadFactor 加载因子 148 | * @throws Exception 如果参数不合法,抛出此异常 149 | */ 150 | public HashMap(int initialCapacity,float loadFactor) throws Exception { 151 | if( initialCapacity <= 0 ) { 152 | throw new Exception("初始化容量异常:" + initialCapacity); 153 | } 154 | if(loadFactor <= 0 || loadFactor >= 1 ) { 155 | throw new Exception("初始化负载因子异常" + loadFactor); 156 | } 157 | /** 158 | * 若初始化容量大于默认的最大容量 则置为最大容量 159 | */ 160 | if(initialCapacity >= MAX_CAPACITY) { 161 | initialCapacity = MAX_CAPACITY; 162 | } 163 | this.capacity = initialCapacity; 164 | this.loadFactor = loadFactor; 165 | table = new Node[initialCapacity]; 166 | } 167 | 168 | 169 | /** 170 | * 返回此Map中的Key-value映射条数 171 | * @return 172 | */ 173 | public int getSize() { 174 | return size; 175 | } 176 | 177 | /** 178 | * 判断是否为空 179 | * @return 180 | */ 181 | public boolean isEmpty() { 182 | return size == 0; 183 | } 184 | 185 | /** 186 | * 插入方法 187 | * @param key 188 | * @param value 189 | * @return 返回插入的value 如果key重复 则插入新值 返回旧值 190 | */ 191 | public Object put(Object key,Object value){ 192 | 193 | int hash = key.hashCode(); 194 | 195 | int index = getIndexForHash(hash); 196 | 197 | Node node = new Node(hash,key,value,null); 198 | 199 | /** 200 | * 当当前占用的数组个数达到数组长度的扩容因子时 扩大默认倍数 201 | */ 202 | if((countOfOccupiedArrays*1.0) / table.length > loadFactor){ 203 | 204 | /** 205 | * 扩大默认倍数 206 | */ 207 | expansion(DEFAULT_EXPAND_MULTIPLE); 208 | } 209 | /** 210 | * 如果该下标存在节点 211 | */ 212 | if(table[index] != null){ 213 | 214 | Node temp = table[index]; 215 | //遍历链表是否存在 216 | while (temp != null){ 217 | //如果Key相同 则替换旧值 218 | if(temp.key.equals(node.key)){ 219 | Object oldValue = temp.value; 220 | temp.value = node.value; 221 | size++; 222 | return oldValue; 223 | } 224 | temp =temp.next; 225 | } 226 | //如果该链表中没有相同的key则插入头中 227 | node.next = table[index]; 228 | table[index] = node; 229 | size++; 230 | countOfOccupiedArrays++; 231 | }else { 232 | //如果不存在 放入即可 233 | table[index] = node; 234 | size++; 235 | } 236 | 237 | return node.value; 238 | 239 | } 240 | 241 | /** 242 | * 通过Key获取Value 如果没有获取到 返回NULL 243 | * @param key 244 | * @return 245 | */ 246 | public Object get(Object key){ 247 | 248 | int hash = key.hashCode(); 249 | 250 | int index = getIndexForHash(hash); 251 | 252 | if(table[index] == null) { 253 | return null; 254 | } 255 | 256 | Node temp = table[index]; 257 | 258 | while (temp != null){ 259 | if(temp.key.equals(key)){ 260 | return temp.value; 261 | } 262 | temp = temp.next; 263 | } 264 | return null; 265 | } 266 | 267 | /** 268 | * 通过KEY删除value 269 | * @param key 270 | * @return 返回删除的value 如果不存在KEY 则返回NULL 271 | */ 272 | public Object remove(Object key){ 273 | 274 | int hash = key.hashCode(); 275 | 276 | int index = getIndexForHash(hash); 277 | 278 | if(table[index] == null) { 279 | return null; 280 | } 281 | 282 | Node temp = table[index]; 283 | 284 | while (temp.next != null){ 285 | if(temp.next.key.equals(key)){ 286 | Object value = temp.next.value; 287 | temp.next = temp.next.next; 288 | size--; 289 | return value; 290 | } 291 | temp = temp.next; 292 | } 293 | return null; 294 | } 295 | 296 | 297 | private int getIndexForHash(int hash){ 298 | return hash % table.length; 299 | } 300 | 301 | /** 302 | * 将当前散列表的数组扩大指定倍数 303 | * @param expansionMultiple 指定倍数 304 | */ 305 | private void expansion(int expansionMultiple){ 306 | Node [] newTable = new Node[this.table.length * expansionMultiple]; 307 | for (int i = 0; i < table.length; i++) { 308 | newTable[i] = table[i]; 309 | } 310 | table = newTable; 311 | } 312 | } 313 | -------------------------------------------------------------------------------- /散列/src/cn/shaoxiongdu/HashMapTestMain.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | /** 4 | * ClassName : HashMapTestMain 5 | * (c)CopyRight 2021/4/21 All rights reserved to ShaoxiongDu 6 | */ 7 | public class HashMapTestMain { 8 | 9 | public static void main(String[] args) { 10 | 11 | try { 12 | 13 | HashMap hashMap = new HashMap(); 14 | Student zhagnsan = new Student("张三11", 11); 15 | hashMap.put(new Student("张三11",11),11); 16 | hashMap.put(new Student("李四12",12),12); 17 | hashMap.put(new Student("王五13",13),13); 18 | hashMap.put(new Student("张三21",21),21); 19 | hashMap.put(new Student("李四22",22),22); 20 | hashMap.put(new Student("王五23",23),23); 21 | 22 | System.out.println(hashMap.getSize()); 23 | 24 | System.out.println(zhagnsan + "值为" + hashMap.get(zhagnsan)); 25 | 26 | hashMap.remove(zhagnsan); 27 | System.out.println(zhagnsan + "值为" + hashMap.get(zhagnsan)); 28 | 29 | 30 | 31 | } catch (Exception e) { 32 | e.printStackTrace(); 33 | } 34 | 35 | 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /栈/src/cn/shaoxiongdu/ArrayStack.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | /** 4 | * ClassName : ArrayStack 5 | * (c)CopyRight 2021/4/14 All rights reserved to ShaoxiongDu 6 | */ 7 | public class ArrayStack implements Stack{ 8 | 9 | /** 10 | * 默认容量 11 | */ 12 | private static final int DEFAULT_CAPACITY = 10; 13 | 14 | /** 15 | * 默认扩容倍数 16 | */ 17 | private static int DEFAULT_EXPAND_MULTIPLE = 2; 18 | 19 | private Object [] elementData = null; 20 | 21 | private int size; 22 | 23 | /** 24 | * 构造指定大小的栈 25 | * @param size 26 | */ 27 | public ArrayStack(int size) { 28 | this.size = 0; 29 | this.elementData = new Object[size]; 30 | } 31 | 32 | /** 33 | * 构造默认大小的栈 34 | */ 35 | public ArrayStack() { 36 | this(DEFAULT_CAPACITY); 37 | } 38 | 39 | /** 40 | * 销毁栈 41 | */ 42 | @Override 43 | public void destroy() { 44 | this.elementData = null; 45 | } 46 | 47 | /** 48 | * 清空栈 49 | */ 50 | @Override 51 | public void clear() { 52 | for (int i = 0; i < size; i++) { 53 | elementData[i] = null; 54 | } 55 | } 56 | 57 | /** 58 | * 判断是否为空 59 | * @return 60 | */ 61 | @Override 62 | public boolean empty() { 63 | return this.size == 0; 64 | } 65 | 66 | /** 67 | * 获取栈顶元素 68 | * @return 69 | * @throws Exception 若栈空 则抛出此异常 70 | */ 71 | @Override 72 | public Object getTop() throws Exception { 73 | if(size==0){ 74 | throw new Exception("栈空"); 75 | } 76 | return elementData[size-1]; 77 | } 78 | 79 | /** 80 | * 获取默认扩容倍数 81 | * @return 82 | */ 83 | public static int getDefaultExpandMultiple() { 84 | return DEFAULT_EXPAND_MULTIPLE; 85 | } 86 | 87 | /** 88 | * 设置默认扩容倍数 89 | * @param defaultExpandMultiple 90 | */ 91 | public static void setDefaultExpandMultiple(int defaultExpandMultiple) throws Exception { 92 | if(defaultExpandMultiple < 0) throw new Exception("默认扩容倍数参数错误"); 93 | DEFAULT_EXPAND_MULTIPLE = defaultExpandMultiple; 94 | } 95 | 96 | /** 97 | * 压栈指定元素 98 | * @param object 99 | */ 100 | @Override 101 | public void push(Object object){ 102 | 103 | /** 104 | * 若当前栈满 则扩容 105 | */ 106 | if(size == elementData.length){ 107 | //扩大默认倍数 108 | expansion(DEFAULT_EXPAND_MULTIPLE); 109 | } 110 | size++; 111 | elementData[size-1] = object; 112 | } 113 | 114 | /** 115 | * 扩容指定倍数 116 | */ 117 | public void expansion(int expandMultiple){ 118 | Object[] newElementData = new Object[elementData.length * expandMultiple]; 119 | for (int i = 0; i < elementData.length; i++) { 120 | newElementData [i] = elementData[i]; 121 | } 122 | elementData = newElementData; 123 | } 124 | 125 | @Override 126 | public Object pop() throws Exception { 127 | Object elementDatum = elementData[size - 1]; 128 | elementData[size-1] = null; 129 | size--; 130 | return elementDatum; 131 | } 132 | 133 | @Override 134 | public int size() { 135 | return this.size; 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /栈/src/cn/shaoxiongdu/ArrayStackTestMain.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | /** 4 | * ClassName : ArrayStackTestMain 5 | * (c)CopyRight 2021/4/14 All rights reserved to ShaoxiongDu 6 | */ 7 | public class ArrayStackTestMain { 8 | 9 | public static void main(String[] args) { 10 | 11 | ArrayStack arrayStack = new ArrayStack(2); 12 | System.out.println("添加之前"); 13 | System.out.println("元素个数为" + arrayStack.size()); 14 | System.out.println("是否为空" + arrayStack.empty()); 15 | System.out.println("添加之后"); 16 | 17 | try { 18 | arrayStack.push(new Student("张三",10)); 19 | arrayStack.push(new Student("李四",20)); 20 | arrayStack.push(new Student("王五",30)); 21 | 22 | System.out.println("元素个数为" + arrayStack.size()); 23 | System.out.println("是否为空" + arrayStack.empty()); 24 | 25 | while (!arrayStack.empty()){ 26 | System.out.println(arrayStack.pop()); 27 | } 28 | 29 | 30 | 31 | 32 | } catch (Exception e) { 33 | e.printStackTrace(); 34 | } 35 | 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /栈/src/cn/shaoxiongdu/LinkedStack.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | import java.util.Objects; 4 | 5 | /** 6 | * ClassName : LinkedStack 7 | * (c)CopyRight 2021/4/19 All rights reserved to ShaoxiongDu 8 | * 用非连续存储结构(链表)实现栈数据结构 9 | */ 10 | public class LinkedStack implements Stack{ 11 | 12 | /** 13 | * 内部节点类 14 | */ 15 | private class Node{ 16 | 17 | /** 18 | * 数据 19 | */ 20 | private Object elementData; 21 | 22 | /** 23 | * 下一个节点引用 24 | */ 25 | private Node next; 26 | 27 | public Node() { 28 | } 29 | 30 | public Node(Object elementData, Node next) { 31 | this.elementData = elementData; 32 | this.next = next; 33 | } 34 | 35 | public Object getElementData() { 36 | return elementData; 37 | } 38 | 39 | public void setElementData(Object elementData) { 40 | this.elementData = elementData; 41 | } 42 | 43 | public Node getNext() { 44 | return next; 45 | } 46 | 47 | public void setNext(Node next) { 48 | this.next = next; 49 | } 50 | 51 | @Override 52 | public boolean equals(Object o) { 53 | if (this == o) return true; 54 | if (o == null || getClass() != o.getClass()) return false; 55 | Node node = (Node) o; 56 | return Objects.equals(elementData, node.elementData) && Objects.equals(next, node.next); 57 | } 58 | 59 | @Override 60 | public int hashCode() { 61 | return Objects.hash(elementData, next); 62 | } 63 | } 64 | 65 | /** 66 | * 头节点 67 | */ 68 | private Node headNode; 69 | 70 | /** 71 | * 元素个数 72 | */ 73 | private int size; 74 | 75 | /** 76 | * 默认空省构造函数 77 | */ 78 | public LinkedStack() { 79 | headNode = new Node(null,null); 80 | size = 0; 81 | } 82 | 83 | /** 84 | * 销毁 85 | */ 86 | @Override 87 | public void destroy() { 88 | headNode = null; 89 | } 90 | 91 | /** 92 | * 清空栈 93 | */ 94 | @Override 95 | public void clear() { 96 | headNode.elementData = null; 97 | headNode.next = null; 98 | } 99 | 100 | /** 101 | * 是否为空 102 | * @return 103 | */ 104 | @Override 105 | public boolean empty() { 106 | return size == 0; 107 | } 108 | 109 | /** 110 | * 获取顶层元素 111 | * @return 若栈为空 则返回-1 112 | * @throws Exception 若栈被销毁,则抛出此异常 113 | */ 114 | @Override 115 | public Object getTop() throws Exception { 116 | if(headNode == null) throw new Exception("栈被销毁"); 117 | if(size == 0) return -1; 118 | Node temp = headNode; 119 | while (temp.next!=null){ 120 | temp = temp.next; 121 | } 122 | return temp.elementData; 123 | } 124 | 125 | @Override 126 | public void push(Object object) { 127 | Node node = new Node(object, null); 128 | Node temp = headNode; 129 | while (temp.next!=null){ 130 | temp = temp.next; 131 | } 132 | temp.next = node; 133 | size++; 134 | } 135 | 136 | /** 137 | * 弹出最顶层的元素 138 | * @return 若栈为空 则返回-1 139 | * @throws Exception 若栈被销毁,则抛出此异常 140 | */ 141 | @Override 142 | public Object pop() throws Exception { 143 | if(headNode == null) throw new Exception("栈被销毁"); 144 | if(size == 0) return -1; 145 | Node temp = headNode; 146 | 147 | while (temp.next.next!=null){ 148 | temp = temp.next; 149 | } 150 | Object elementData = temp.next.elementData; 151 | temp.next = null; 152 | size--; 153 | return elementData; 154 | 155 | } 156 | 157 | @Override 158 | public int size() { 159 | return size; 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /栈/src/cn/shaoxiongdu/LinkedStackTestMain.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | /** 4 | * ClassName : LinkedStackTestMain 5 | * (c)CopyRight 2021/4/19 All rights reserved to ShaoxiongDu 6 | */ 7 | public class LinkedStackTestMain { 8 | 9 | public static void main(String[] args) { 10 | 11 | LinkedStack linkedStack = new LinkedStack(); 12 | 13 | System.out.println("入栈前:元素个数为" + linkedStack.size() + " 是否为空 " + linkedStack.empty()); 14 | 15 | System.out.println("添加之后"); 16 | 17 | try { 18 | linkedStack.push(new Student("张三",10)); 19 | linkedStack.push(new Student("李四",20)); 20 | linkedStack.push(new Student("王五",30)); 21 | 22 | System.out.println("元素个数为" + linkedStack.size()); 23 | System.out.println("是否为空" + linkedStack.empty()); 24 | 25 | while (!linkedStack.empty()){ 26 | System.out.println(linkedStack.pop()); 27 | } 28 | 29 | linkedStack.pop(); 30 | } catch (Exception e) { 31 | e.printStackTrace(); 32 | } 33 | 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /栈/src/cn/shaoxiongdu/Stack.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | /** 4 | * InterfaceName : Stack 5 | * (c)CopyRight 2021/4/14 All rights reserved to ShaoxiongDu 6 | * 栈的接口 7 | */ 8 | public interface Stack { 9 | 10 | /** 11 | * 如果栈存在,则销毁栈 12 | */ 13 | public void destroy(); 14 | 15 | /** 16 | * 清空栈元素 17 | */ 18 | public void clear(); 19 | 20 | /** 21 | * 判断栈是否为空 22 | */ 23 | public boolean empty(); 24 | 25 | /** 26 | * 如果栈存在且非空,返回栈顶元素 27 | * @return 如果栈空,则返回NULL 28 | */ 29 | public Object getTop() throws Exception; 30 | 31 | /** 32 | * 插入新元素到栈顶 33 | * @param object 34 | * @throws Exception 若栈被销毁或者已满 抛出此异常 35 | */ 36 | public void push(Object object); 37 | 38 | /** 39 | * 弹出栈顶元素 40 | * @return 41 | * @throws Exception 若栈被销毁或者栈为空,则抛出此异常 42 | */ 43 | public Object pop() throws Exception; 44 | 45 | /** 46 | * 返回栈中元素个数 47 | * @return 48 | */ 49 | public int size(); 50 | 51 | 52 | 53 | } 54 | -------------------------------------------------------------------------------- /栈/src/cn/shaoxiongdu/TestMainStack.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | /** 4 | * ClassName : TestMain 5 | * (c)CopyRight 2021/4/12 All rights reserved to ShaoxiongDu 6 | */ 7 | public class TestMainStack { 8 | 9 | public static void main(String[] args) { 10 | 11 | System.out.println("Hello DataStructure"); 12 | 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /线性表/src/cn/shaoxiongdu/ArrayList.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | /** 4 | * ClassName : ArrayList 5 | * (c)CopyRight 2021/4/12 All rights reserved to ShaoxiongDu 6 | */ 7 | public class ArrayList implements List{ 8 | 9 | /** 10 | * 默认容量 11 | */ 12 | private static final int DEFAULT_CAPACITY = 10; 13 | 14 | private Object [] elementData; 15 | 16 | /** 17 | * 默认扩容倍数 18 | */ 19 | private static final int DEFAULT_EXPAND_MULTIPLE = 2; 20 | 21 | /** 22 | * 元素数量 23 | */ 24 | private int size; 25 | 26 | /** 27 | * 创建默认容量(10)的数组线性表 28 | */ 29 | public ArrayList() { 30 | elementData = new Object[DEFAULT_CAPACITY]; 31 | size = 0; 32 | } 33 | 34 | /** 35 | * 创建指定大小的数组线性表 36 | * @param size 指定大小 37 | * @throws Exception 若传入的容量大于9999或者小于0则抛出此异常 38 | */ 39 | public ArrayList(int size) throws Exception { 40 | if(size <= 0 || size > 9999) throw new Exception("容量数据错误!"); 41 | elementData = new Object[size]; 42 | } 43 | 44 | 45 | public void print() { 46 | System.out.println("=================================="); 47 | for (int i = 1; i <= this.size(); i++) { 48 | try { 49 | System.out.println(this.get(i)); 50 | } catch (Exception e) { 51 | e.printStackTrace(); 52 | } 53 | } 54 | System.out.println("=================================="); 55 | } 56 | 57 | /** 58 | * 判断是否为空 59 | * @return 60 | */ 61 | 62 | public boolean isEmpty() { 63 | return size==0; 64 | } 65 | 66 | 67 | public void clear() { 68 | for (int i = 0; i < elementData.length; i++) { 69 | elementData[i] = null; 70 | } 71 | size = 0; 72 | } 73 | 74 | /** 75 | * 返回指定位置处的下标 76 | * @param index 77 | * @return 78 | * @throws Exception 若下标<0或者大于线性表的长度 抛出此异常 79 | */ 80 | 81 | public Object get(int index) throws Exception { 82 | if(index < 0 || index > elementData.length) throw new Exception("下标错误"); 83 | return elementData[index-1]; 84 | } 85 | 86 | /** 87 | * 返回指定元素的下标 88 | * @param object 89 | * @return 如果线性表中没有,则返回-1 90 | */ 91 | 92 | public int findIndexByObject(Object object) { 93 | if(object == null) return -1; 94 | for (int i = 0; i < elementData.length; i++) { 95 | if(object.equals(elementData[i])) return i+1; 96 | } 97 | return -1; 98 | } 99 | 100 | /** 101 | * 向指定位置添加元素 102 | * @param index 指定位置 103 | * @param object 指定元素 104 | * @return 105 | * @throws Exception 如果下标小于0或者大于容量则抛出此异常 106 | */ 107 | 108 | public Object insert(int index, Object object) throws Exception { 109 | 110 | if( index < 0 || index > elementData.length) throw new Exception("下标错误!"); 111 | 112 | //如果容量已满 则自动扩容 113 | if(size == elementData.length){ 114 | expansion(); 115 | } 116 | elementData[index-1] = object; 117 | return elementData[index-1]; 118 | } 119 | 120 | /** 121 | * 添加元素 122 | * @param object 123 | * @return 返回添加好的元素 124 | */ 125 | 126 | public Object insert(Object object) throws Exception { 127 | //如果容量已满 则自动扩容 128 | if(size == elementData.length){ 129 | expansion(); 130 | } 131 | elementData[size] = object; 132 | size++; 133 | return elementData[size]; 134 | } 135 | 136 | /** 137 | * 删除指定位置的元素 138 | * @param index 指定位置 139 | * @return 140 | * @throws Exception 如果下标小于0或者大于元素个数则抛出此异常 141 | */ 142 | 143 | public Object delete(int index) throws Exception { 144 | if( index < 0 || index > size) throw new Exception("下标错误!"); 145 | Object object = elementData[index-1]; 146 | for (int i = index-1; i < elementData.length-1-1; i++) { 147 | elementData[i] = elementData[i+1]; 148 | } 149 | size--; 150 | return object; 151 | } 152 | 153 | 154 | 155 | 156 | public int size() { 157 | return size; 158 | } 159 | 160 | /** 161 | * 扩容 162 | */ 163 | private void expansion(){ 164 | Object[] newElementData = new Object[elementData.length * DEFAULT_EXPAND_MULTIPLE]; 165 | for (int i = 0; i < elementData.length; i++) { 166 | newElementData[i] = elementData[i]; 167 | } 168 | elementData = newElementData; 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /线性表/src/cn/shaoxiongdu/ArrayListTestMain.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | /** 4 | * ClassName : TestMain 5 | * (c)CopyRight 2021/4/12 All rights reserved to ShaoxiongDu 6 | */ 7 | public class ArrayListTestMain { 8 | 9 | public static void main(String[] args) { 10 | 11 | try { 12 | 13 | ArrayList arrayList = new ArrayList(20); 14 | 15 | arrayList.insert(new Student("张三",20)); 16 | arrayList.insert(new Student("李四",30)); 17 | arrayList.insert(new Student("王五",40)); 18 | 19 | System.out.println("元素个数为"+arrayList.size()); 20 | 21 | for (int i = 1; i <= arrayList.size(); i++) { 22 | System.out.println(arrayList.get(i)); 23 | } 24 | 25 | System.out.println("第一处添加元素之后"); 26 | arrayList.insert(1,new Student("赵6",50)); 27 | for (int i = 1; i <= arrayList.size(); i++) { 28 | System.out.println(arrayList.get(i)); 29 | } 30 | 31 | System.out.println("第二个元素为: "+ arrayList.get(2)); 32 | 33 | System.out.println("删除第一个元素之后" + arrayList.delete(1)); 34 | System.out.println("大小为" + arrayList.size()); 35 | for (int i = 1; i <= arrayList.size(); i++) { 36 | System.out.println(arrayList.get(i)); 37 | } 38 | 39 | Student wangwu = new Student("王五", 40); 40 | System.out.println("wangwu所在位置为"+arrayList.findIndexByObject(wangwu)); 41 | 42 | System.out.println("是否为空" + arrayList.isEmpty()); 43 | 44 | arrayList.clear(); 45 | 46 | System.out.println("清空之后是否为空" + arrayList.isEmpty()); 47 | 48 | System.out.println("arrayList的size = " + arrayList.size()); 49 | 50 | 51 | } catch (Exception e) { 52 | e.printStackTrace(); 53 | } 54 | 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /线性表/src/cn/shaoxiongdu/LinkedList.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | import java.io.NotActiveException; 4 | 5 | /** 6 | * ClassName : LinkedList 7 | * (c)CopyRight 2021/4/13 All rights reserved to ShaoxiongDu 8 | */ 9 | public class LinkedList implements List{ 10 | 11 | /** 12 | * 链表节点内部类 13 | */ 14 | private class Node{ 15 | 16 | /** 17 | * 当前索引 18 | */ 19 | private int index; 20 | 21 | /** 22 | * 数据元素 23 | */ 24 | private Object elementData; 25 | 26 | /** 27 | * 下一节点引用 28 | */ 29 | private Node nextNode; 30 | 31 | /** 32 | * 带参构造 33 | * @param index 当前索引 34 | * @param elementData 数据元素 35 | * @param nextNode 下一节点引用 36 | */ 37 | public Node(int index, Object elementData, Node nextNode) { 38 | this.index = index; 39 | this.elementData = elementData; 40 | this.nextNode = nextNode; 41 | } 42 | 43 | public Node() { 44 | this.nextNode = null; 45 | this.elementData = null; 46 | } 47 | 48 | public int getIndex() { 49 | return index; 50 | } 51 | 52 | public void setIndex(int index) { 53 | this.index = index; 54 | } 55 | 56 | public Object getElementData() { 57 | return elementData; 58 | } 59 | 60 | public void setElementData(Object elementData) { 61 | this.elementData = elementData; 62 | } 63 | 64 | public Node getNextNode() { 65 | return nextNode; 66 | } 67 | 68 | public void setNextNode(Node nextNode) { 69 | this.nextNode = nextNode; 70 | } 71 | } 72 | 73 | /** 74 | * 头节点 75 | */ 76 | private Node headNode; 77 | 78 | /** 79 | * 当前元素个数 80 | */ 81 | private int size; 82 | 83 | public LinkedList() { 84 | this.headNode = new Node(0,null,null); 85 | this.size = 0; 86 | } 87 | 88 | @Override 89 | public void print() { 90 | System.out.println("------------------------------"); 91 | for (Node temp = headNode;temp.nextNode!=null;temp=temp.nextNode){ 92 | System.out.println(temp.nextNode.getElementData()); 93 | } 94 | System.out.println("------------------------------"); 95 | } 96 | 97 | @Override 98 | public boolean isEmpty() { 99 | return this.size() == 0; 100 | } 101 | 102 | @Override 103 | public void clear() { 104 | headNode.nextNode = null; 105 | size=0; 106 | } 107 | 108 | /** 109 | * 通过下标获取元素 110 | * @param index 111 | * @return 如果下标错误 返回NULL 112 | * @throws Exception 下标小于0或者大于元素个数 抛出此异常 113 | */ 114 | @Override 115 | public Object get(int index) throws Exception { 116 | if(index <= 0 || index > size) throw new Exception("下标错误"); 117 | Node temp = headNode; 118 | while (true){ 119 | if(temp == null){ 120 | return null; 121 | } 122 | if(temp.index == index){ 123 | return temp.elementData; 124 | } 125 | temp = temp.nextNode; 126 | } 127 | } 128 | 129 | /** 130 | * 通过元素获取位置 131 | * @param object 132 | * @return 若元素为NULL或者未找到 则返回-1 133 | */ 134 | @Override 135 | public int findIndexByObject(Object object) { 136 | if(object == null) return -1; 137 | Node temp = headNode; 138 | while (true){ 139 | if(temp == null){ 140 | return -1; 141 | } 142 | if(object.equals(temp.elementData)){ 143 | return temp.index; 144 | } 145 | temp = temp.nextNode; 146 | } 147 | 148 | } 149 | 150 | /** 151 | * 将指定元素插入到指定位置之前 长度加一 152 | * @param index 指定位置 153 | * @param object 指定元素 154 | * @return 返回插入的元素 如果下标不存在,则返回NULL 155 | * @throws Exception 如果下标小于0 则抛出此异常 156 | */ 157 | @Override 158 | public Object insert(int index, Object object) throws Exception { 159 | if( index < 0 ) throw new Exception("下标错误"); 160 | Node temp = headNode; 161 | for (int i = 0; i < index-1 && temp.nextNode!=null; i++) { 162 | temp = temp.nextNode; 163 | } 164 | /** 165 | * 如果不存在该节点,则返回NULL 166 | */ 167 | if( temp == null) return null; 168 | 169 | Node node = new Node(index, object, temp.nextNode); 170 | temp.nextNode = node; 171 | for (Node node1 = node.nextNode;node1!=null;node1=node1.nextNode) { 172 | node1.setIndex(node1.getIndex()+1); 173 | } 174 | 175 | size++; 176 | return node.elementData; 177 | 178 | } 179 | 180 | /** 181 | * 通过下标删除元素 182 | * @param index 指定位置 183 | * @return 184 | * @throws Exception 下标错误抛出此异常 185 | */ 186 | @Override 187 | public Object delete(int index) throws Exception { 188 | if(index < 0 || index > size) throw new Exception("下标错误"); 189 | Node temp = headNode,deleteNode = null; 190 | while (true){ 191 | if(temp == null){ 192 | return null; 193 | } 194 | if(temp.nextNode.index == index){ 195 | deleteNode = temp.nextNode; 196 | temp.nextNode = temp.nextNode.nextNode; 197 | for(temp=temp.nextNode;temp!=null;temp=temp.nextNode){ 198 | temp.setIndex(temp.getIndex()-1); 199 | } 200 | 201 | size--; 202 | return deleteNode; 203 | } 204 | temp = temp.nextNode; 205 | } 206 | } 207 | 208 | @Override 209 | public Object insert(Object object) throws Exception { 210 | if(object == null) return null; 211 | Node temp = headNode; 212 | while (true){ 213 | if(temp.nextNode == null){ 214 | Node node = new Node(temp.index + 1, object, null); 215 | temp.nextNode = node; 216 | size++; 217 | return temp.nextNode.getElementData(); 218 | } 219 | temp = temp.nextNode; 220 | 221 | } 222 | } 223 | 224 | @Override 225 | public int size() { 226 | return this.size; 227 | } 228 | } 229 | -------------------------------------------------------------------------------- /线性表/src/cn/shaoxiongdu/LinkedListTestMain.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | /** 4 | * ClassName : LinkedListTestMain 5 | * (c)CopyRight 2021/4/13 All rights reserved to ShaoxiongDu 6 | */ 7 | public class LinkedListTestMain { 8 | 9 | public static void main(String[] args) { 10 | 11 | LinkedList linkedList = new LinkedList(); 12 | 13 | System.out.println("linkedList.size() = " + linkedList.size()); 14 | 15 | try { 16 | linkedList.insert(new Student("张三",10)); 17 | linkedList.insert(new Student("李四",20)); 18 | linkedList.insert(new Student("王五",30)); 19 | 20 | System.out.println("添加之后"); 21 | linkedList.print(); 22 | 23 | 24 | System.out.println("添加之后:linkedList.size() = " + linkedList.size()); 25 | 26 | System.out.println("位置3的元素是"+linkedList.get(3)); 27 | 28 | System.out.println("删除位置1的元素"); 29 | linkedList.delete(1); 30 | linkedList.print(); 31 | 32 | Student z6 = new Student("赵6", 40); 33 | System.out.println("位置1添加元素" + z6); 34 | linkedList.insert(1,z6); 35 | linkedList.print(); 36 | 37 | System.out.println("赵6 的位置"+linkedList.findIndexByObject(z6)); 38 | 39 | System.out.println("清除"); 40 | linkedList.clear(); 41 | System.out.println("linkedList.size = " + linkedList.size()); 42 | 43 | 44 | } catch (Exception e) { 45 | e.printStackTrace(); 46 | } 47 | 48 | 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /线性表/src/cn/shaoxiongdu/List.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | import java.util.ArrayList; 4 | 5 | /** 6 | * InterfaceName : List 7 | * (c)CopyRight 2021/4/12 All rights reserved to ShaoxiongDu 8 | */ 9 | public interface List { 10 | 11 | public void print(); 12 | 13 | /** 14 | * 判断线性表是否为空 15 | * @return 16 | */ 17 | public boolean isEmpty(); 18 | 19 | /** 20 | * 清空线性表 21 | */ 22 | public void clear(); 23 | 24 | /** 25 | * 返回线性表中下标为index的元素 26 | * @param index 27 | * @return 28 | */ 29 | public Object get(int index) throws Exception; 30 | 31 | /** 32 | * 返回指定元素的下标 33 | * @param object 34 | * @return 若线性表中无该元素,则返回-1 35 | */ 36 | public int findIndexByObject(Object object); 37 | 38 | /** 39 | * 在指定位置插入新元素 40 | * @param index 指定位置 41 | * @param object 指定元素 42 | * @return 新插入的元素 43 | */ 44 | public Object insert(int index,Object object) throws Exception; 45 | 46 | /** 47 | * 删除指定位置的元素 48 | * @param index 指定位置 49 | * @return 元素 若下标错误或者无指定元素 返回NULL 50 | */ 51 | public Object delete(int index) throws Exception; 52 | 53 | /** 54 | * 插入新元素 55 | * @param object 56 | * @return 新插入的元素 57 | */ 58 | public Object insert(Object object) throws Exception; 59 | 60 | /** 61 | * 返回线性表的元素个数 62 | * @return 63 | */ 64 | public int size(); 65 | 66 | } 67 | -------------------------------------------------------------------------------- /线性表/src/cn/shaoxiongdu/Student.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | import java.util.Objects; 4 | 5 | /** 6 | * ClassName : Student 7 | * (c)CopyRight 2021/4/12 All rights reserved to ShaoxiongDu 8 | */ 9 | public class Student { 10 | 11 | private String name; 12 | 13 | 14 | 15 | private int age; 16 | 17 | public Student(String name, int age) { 18 | this.name = name; 19 | this.age = age; 20 | } 21 | 22 | @Override 23 | public boolean equals(Object o) { 24 | if (this == o) return true; 25 | if (o == null || getClass() != o.getClass()) return false; 26 | Student student = (Student) o; 27 | return age == student.age && Objects.equals(name, student.name); 28 | } 29 | 30 | @Override 31 | public int hashCode() { 32 | return age % 10; 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return "Student{" + 38 | "name='" + name + '\'' + 39 | ", age=" + age + 40 | '}'; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /队列/src/cn/shaoxiongdu/ArrayQueue.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | /** 4 | * ClassName : ArrayQueue 5 | * (c)CopyRight 2021/4/20 All rights reserved to ShaoxiongDu 6 | */ 7 | public class ArrayQueue implements Queue{ 8 | 9 | 10 | /** 11 | * 队头元素 12 | */ 13 | private int front; 14 | 15 | /** 16 | * 队尾元素 17 | */ 18 | private int rear; 19 | 20 | /** 21 | * 队列数组 22 | */ 23 | private Object [] theArray; 24 | 25 | /** 26 | * 元素个数 27 | */ 28 | private int size; 29 | 30 | /** 31 | * 构造大小为10的默认环形队列 32 | * @throws Exception 容量错误返回此异常 33 | */ 34 | public ArrayQueue() throws Exception { 35 | this(10); 36 | } 37 | 38 | /** 39 | * 构造指定大小的环形队列 40 | * @param capacity 容量 41 | * @throws Exception 容量错误返回此异常 42 | */ 43 | public ArrayQueue(int capacity) throws Exception { 44 | 45 | if(capacity < 1 || capacity > 10000) throw new Exception("容量错误"); 46 | this.theArray = new Object[capacity]; 47 | // 头指针置0 48 | front = 0; 49 | // 头指针置0 50 | rear = 0; 51 | // 元素个数置0 52 | size = 0; 53 | } 54 | 55 | /** 56 | * 清空队列中全部元素 57 | */ 58 | @Override 59 | public void clear() { 60 | for (Object o : theArray) { 61 | o = null; 62 | } 63 | } 64 | 65 | /** 66 | * 判断是否为空 67 | * @return 68 | */ 69 | @Override 70 | public boolean empty() { 71 | return rear == front; 72 | } 73 | 74 | /** 75 | * 获取头元素 76 | * @return 77 | */ 78 | @Override 79 | public Object getHead() { 80 | return theArray[front]; 81 | } 82 | 83 | /** 84 | * 入队 85 | * @param object 86 | * @throws Exception 队列已满时抛出此异常 87 | */ 88 | @Override 89 | public void enterObject(Object object) throws Exception { 90 | if(( rear + 1) % this.theArray.length == front) throw new Exception("队列已满!"); 91 | /** 92 | * 入队 93 | */ 94 | theArray [rear] = object; 95 | 96 | /** 97 | * 尾指针后移 98 | */ 99 | rear = (rear + 1) % this.theArray.length; 100 | size++; 101 | } 102 | 103 | /** 104 | * 出队 105 | * @return 106 | * @throws Exception 队列为空时抛出此异常 107 | */ 108 | @Override 109 | public Object deleteObject() throws Exception{ 110 | if(empty()) throw new Exception("队列为空"); 111 | Object object = theArray[front]; 112 | front = (front + 1) % this.theArray.length; 113 | return object; 114 | } 115 | 116 | @Override 117 | public int size() { 118 | return size; 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /队列/src/cn/shaoxiongdu/ArrayQueueTestMain.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | /** 4 | * ClassName : ArrayQueueTestMain 5 | * (c)CopyRight 2021/4/20 All rights reserved to ShaoxiongDu 6 | */ 7 | public class ArrayQueueTestMain { 8 | 9 | public static void main(String[] args) { 10 | 11 | try { 12 | 13 | Queue queue = new ArrayQueue(10); 14 | System.out.println("入队之前大小 " + queue.size()); 15 | System.out.println("入队之前是否为空 " + queue.empty()); 16 | 17 | System.out.println("入队中!!!"); 18 | queue.enterObject(new Student("张3",10)); 19 | queue.enterObject(new Student("李4",20)); 20 | queue.enterObject(new Student("王5",30)); 21 | System.out.println("入队之后大小 " + queue.size()); 22 | System.out.println("入队之后是否为空 " + queue.empty()); 23 | 24 | System.out.println("头元素:"); 25 | System.out.println(queue.getHead()); 26 | 27 | System.out.println("出队"); 28 | System.out.println("出队--->" + queue.deleteObject()); 29 | System.out.println("出队--->" + queue.deleteObject()); 30 | System.out.println("出队--->" + queue.deleteObject()); 31 | 32 | System.out.println("出队之后大小 " + queue.size()); 33 | System.out.println("出队之后是否为空 " + queue.empty()); 34 | 35 | 36 | } catch (Exception e) { 37 | e.printStackTrace(); 38 | } 39 | 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /队列/src/cn/shaoxiongdu/LinkedQueue.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | /** 4 | * ClassName : LinkedQueue 5 | * (c)CopyRight 2021/4/20 All rights reserved to ShaoxiongDu 6 | */ 7 | public class LinkedQueue implements Queue{ 8 | 9 | /** 10 | * 节点内部类 11 | */ 12 | private class Node{ 13 | 14 | /** 15 | * 数据 16 | */ 17 | private Object elementData; 18 | 19 | /** 20 | * 下一节点地址 21 | */ 22 | private Node next; 23 | 24 | public Node() { 25 | } 26 | 27 | public Node(Object elementData, Node next) { 28 | this.elementData = elementData; 29 | this.next = next; 30 | } 31 | 32 | public Object getElementData() { 33 | return elementData; 34 | } 35 | 36 | public void setElementData(Object elementData) { 37 | this.elementData = elementData; 38 | } 39 | 40 | public Node getNext() { 41 | return next; 42 | } 43 | 44 | public void setNext(Node next) { 45 | this.next = next; 46 | } 47 | } 48 | 49 | /** 50 | * 头节点引用 51 | */ 52 | private Node front; 53 | 54 | /** 55 | * 尾节点引用 56 | */ 57 | private Node rear; 58 | 59 | private int size; 60 | 61 | /** 62 | * 构造链表队列 63 | */ 64 | public LinkedQueue() { 65 | front = new Node(null,null); 66 | rear = front; 67 | } 68 | 69 | @Override 70 | public void clear() { 71 | front.next = null; 72 | rear = front; 73 | } 74 | 75 | @Override 76 | public boolean empty() { 77 | return size == 0; 78 | } 79 | 80 | @Override 81 | public Object getHead() { 82 | return front.next.elementData; 83 | } 84 | 85 | @Override 86 | public void enterObject(Object object) throws Exception { 87 | Node node = new Node(object, null); 88 | rear.next = node; 89 | size++; 90 | rear = rear.next; 91 | } 92 | 93 | @Override 94 | public Object deleteObject() throws Exception { 95 | if(front == rear) throw new Exception("栈已空"); 96 | Node node = front.next; 97 | front.next = front.next.next; 98 | size--; 99 | if(node == rear){ 100 | rear = front; 101 | } 102 | return node.elementData; 103 | } 104 | 105 | @Override 106 | public int size() { 107 | return size; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /队列/src/cn/shaoxiongdu/LinkedQueueTestMain.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | /** 4 | * ClassName : ArrayQueueTestMain 5 | * (c)CopyRight 2021/4/20 All rights reserved to ShaoxiongDu 6 | */ 7 | public class LinkedQueueTestMain { 8 | 9 | public static void main(String[] args) { 10 | 11 | try { 12 | 13 | Queue queue = new LinkedQueue(); 14 | System.out.println("入队之前大小 " + queue.size()); 15 | System.out.println("入队之前是否为空 " + queue.empty()); 16 | 17 | System.out.println("入队中!!!"); 18 | queue.enterObject(new Student("张3",10)); 19 | queue.enterObject(new Student("李4",20)); 20 | queue.enterObject(new Student("王5",30)); 21 | System.out.println("入队之后大小 " + queue.size()); 22 | System.out.println("入队之后是否为空 " + queue.empty()); 23 | 24 | System.out.println("头元素:"); 25 | System.out.println(queue.getHead()); 26 | 27 | System.out.println("出队"); 28 | while (!queue.empty()){ 29 | System.out.println("出队--->" + queue.deleteObject()); 30 | } 31 | 32 | System.out.println("出队之后大小 " + queue.size()); 33 | System.out.println("出队之后是否为空 " + queue.empty()); 34 | 35 | 36 | } catch (Exception e) { 37 | e.printStackTrace(); 38 | } 39 | 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /队列/src/cn/shaoxiongdu/Queue.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | /** 4 | * InterfaceName : Queue 5 | * (c)CopyRight 2021/4/20 All rights reserved to ShaoxiongDu 6 | * 队列接口 7 | */ 8 | public interface Queue { 9 | 10 | /** 11 | * 清空队列 12 | */ 13 | public void clear(); 14 | 15 | /** 16 | * 是否为空 17 | */ 18 | public boolean empty(); 19 | 20 | /** 21 | * 获取头元素 22 | * @return 23 | */ 24 | public Object getHead(); 25 | 26 | /** 27 | * 堆尾插入新元素 28 | * @param object 29 | */ 30 | public void enterObject(Object object) throws Exception; 31 | 32 | /** 33 | * 删除队列中的队头元素 34 | * @return 35 | */ 36 | public Object deleteObject() throws Exception; 37 | 38 | /** 39 | * 返回队列元素个数 40 | * @return 41 | */ 42 | public int size(); 43 | } 44 | -------------------------------------------------------------------------------- /队列/src/cn/shaoxiongdu/TestMainQueue.java: -------------------------------------------------------------------------------- 1 | package cn.shaoxiongdu; 2 | 3 | import java.util.Queue; 4 | import java.util.concurrent.ArrayBlockingQueue; 5 | 6 | /** 7 | * ClassName : TestMain 8 | * (c)CopyRight 2021/4/12 All rights reserved to ShaoxiongDu 9 | */ 10 | public class TestMainQueue { 11 | 12 | public static void main(String[] args) { 13 | 14 | System.out.println("Hello DataStructure"); 15 | 16 | } 17 | 18 | } 19 | --------------------------------------------------------------------------------