├── linearList-seqList ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ ├── LList.java │ │ └── seqList │ │ ├── Test.java │ │ └── SeqList.java └── pom.xml ├── tree-binaryTree ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── tree │ │ └── binaryTree │ │ ├── BinaryNode.java │ │ ├── Test.java │ │ └── BinaryTree.java └── pom.xml ├── linearList-interface ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ └── LList.java └── pom.xml ├── linearList-linkList ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ ├── linkList │ │ ├── Test.java │ │ ├── Node.java │ │ └── SinglyLinkedList.java │ │ └── LList.java └── pom.xml ├── linearList-queue-link ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ ├── Node.java │ │ └── queue │ │ ├── QQueue.java │ │ └── link │ │ ├── Test.java │ │ └── LinkedQueue.java └── pom.xml ├── linearList-stack-link ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ ├── Node.java │ │ └── stack │ │ ├── SStack.java │ │ └── link │ │ ├── Test.java │ │ └── LinkedStack.java └── pom.xml ├── linearList-linkList-circular ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ ├── linkList │ │ ├── Node.java │ │ ├── circular │ │ │ ├── Test.java │ │ │ └── CircularSinglyLinkedList.java │ │ ├── SinglyLinkedList.java │ │ └── head │ │ │ └── HeadSinglyLinkedList.java │ │ └── LList.java └── pom.xml ├── linearList-linkList-head ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ ├── linkList │ │ ├── Node.java │ │ └── head │ │ │ ├── Test.java │ │ │ └── HeadSinglyLinkedList.java │ │ └── LList.java └── pom.xml ├── linearList-linkList-increase ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ ├── linkList │ │ ├── Node.java │ │ ├── increase │ │ │ ├── Test.java │ │ │ └── IncreaseSinglyLinkedList.java │ │ └── SinglyLinkedList.java │ │ └── LList.java └── pom.xml ├── linearList-linkList-josephus ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ ├── linkList │ │ ├── Test.java │ │ ├── Node.java │ │ ├── Josephus.java │ │ └── SinglyLinkedList.java │ │ └── LList.java └── pom.xml ├── linearList-linkList-reverse ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ ├── linkList │ │ ├── Test.java │ │ ├── Node.java │ │ ├── reverse │ │ │ ├── Test.java │ │ │ └── ReverseSinglyLinkedList.java │ │ └── SinglyLinkedList.java │ │ └── LList.java └── pom.xml ├── linearList-queue-interface ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ └── queue │ │ └── QQueue.java └── pom.xml ├── linearList-queue-sequence ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ └── queue │ │ ├── QQueue.java │ │ └── sequence │ │ ├── Test.java │ │ └── SeqQueue.java └── pom.xml ├── linearList-seqList-iterator ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ ├── seqList │ │ └── iterable │ │ │ ├── Test.java │ │ │ └── IterableSeqList.java │ │ ├── LList.java │ │ └── AbstractLList.java └── pom.xml ├── linearList-seqList-josephus ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ ├── LList.java │ │ └── seqList │ │ ├── Test.java │ │ ├── Josephus.java │ │ └── SeqList.java └── pom.xml ├── linearList-stack-interface ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ └── stack │ │ └── SStack.java └── pom.xml ├── linearList-stack-sequence ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ └── stack │ │ ├── SStack.java │ │ └── sequence │ │ ├── Test.java │ │ └── SeqStack.java └── pom.xml ├── longjiazuo.jpg ├── linearList-linkList-doubleLink ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ ├── linkList │ │ └── doubleLink │ │ │ ├── Test.java │ │ │ ├── DoubleLinkNode.java │ │ │ └── DoubleLinkedList.java │ │ └── LList.java └── pom.xml ├── linearList-linkList-head-iterable ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ ├── linkList │ │ ├── Node.java │ │ └── head │ │ │ └── iterable │ │ │ ├── Test.java │ │ │ └── IterableHeadSinglyLinkedList.java │ │ ├── LList.java │ │ └── AbstractLList.java └── pom.xml ├── linearList-linkList-head-sorted ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ ├── linkList │ │ ├── Node.java │ │ └── head │ │ │ ├── sorted │ │ │ ├── Test.java │ │ │ └── SortedHeadSinglyLinkedList.java │ │ │ └── HeadSinglyLinkedList.java │ │ └── LList.java └── pom.xml ├── linearList-queue-sequence-cycle ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ └── queue │ │ ├── QQueue.java │ │ └── sequence │ │ └── cycle │ │ ├── Test.java │ │ └── SeqCycleQueue.java └── pom.xml ├── linearList-linkList-doubleLink-head-cycle ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── org │ │ └── light4j │ │ └── dataStructure │ │ └── linearList │ │ ├── linkList │ │ └── doubleLink │ │ │ ├── head │ │ │ └── cycle │ │ │ │ └── CycleHeadDoubleLinkedList.java │ │ │ └── DoubleLinkNode.java │ │ └── LList.java └── pom.xml ├── .gitignore ├── pom.xml ├── README.md └── LICENSE /linearList-seqList/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /tree-binaryTree/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.settings/ 3 | /.classpath 4 | /.project 5 | -------------------------------------------------------------------------------- /linearList-interface/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-linkList/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-queue-link/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-stack-link/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-linkList-circular/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-linkList-head/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-linkList-increase/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-linkList-josephus/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-linkList-reverse/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-queue-interface/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-queue-sequence/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-seqList-iterator/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-seqList-josephus/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-stack-interface/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-stack-sequence/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /longjiazuo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/longjiazuo/data-structure-project/HEAD/longjiazuo.jpg -------------------------------------------------------------------------------- /linearList-linkList-doubleLink/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-linkList-head-iterable/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-linkList-head-sorted/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-queue-sequence-cycle/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /linearList-linkList-doubleLink-head-cycle/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.project 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | /target/ 14 | /.externalToolBuilders/ 15 | /.settings/ 16 | /.classpath 17 | /.gitignore 18 | /.project 19 | -------------------------------------------------------------------------------- /linearList-linkList/src/main/java/org/light4j/dataStructure/linearList/linkList/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class Test { 6 | public static void main(String[] args) { 7 | LList linkedList = new SinglyLinkedList(); 8 | // 添加A,B,C三个元素 9 | linkedList.add("A"); 10 | linkedList.add("B"); 11 | linkedList.add("C"); 12 | // 输出元素个数 13 | System.out.println("元素个数是:"+linkedList.length()); 14 | } 15 | } -------------------------------------------------------------------------------- /linearList-linkList-josephus/src/main/java/org/light4j/dataStructure/linearList/linkList/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class Test { 6 | public static void main(String[] args) { 7 | LList linkedList = new SinglyLinkedList(); 8 | // 添加A,B,C三个元素 9 | linkedList.add("A"); 10 | linkedList.add("B"); 11 | linkedList.add("C"); 12 | // 输出元素个数 13 | System.out.println("元素个数是:"+linkedList.length()); 14 | } 15 | } -------------------------------------------------------------------------------- /linearList-linkList-reverse/src/main/java/org/light4j/dataStructure/linearList/linkList/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class Test { 6 | public static void main(String[] args) { 7 | LList linkedList = new SinglyLinkedList(); 8 | // 添加A,B,C三个元素 9 | linkedList.add("A"); 10 | linkedList.add("B"); 11 | linkedList.add("C"); 12 | // 输出元素个数 13 | System.out.println("元素个数是:"+linkedList.length()); 14 | } 15 | } -------------------------------------------------------------------------------- /linearList-queue-link/src/main/java/org/light4j/dataStructure/linearList/Node.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | /** 4 | * 链表节点类 5 | * 6 | * @author longjiazuo 7 | */ 8 | public class Node { 9 | public E data;// 链表数据域,保存数据元素 10 | public Node next;// 链表地址域,引用后继节点 11 | 12 | public Node(E data, Node next) {// 构造节点,指定数据元素和后继节点 13 | this.data = data; 14 | this.next = next; 15 | } 16 | 17 | public Node(E data) { 18 | this(data, null); 19 | } 20 | 21 | public Node() { 22 | this(null, null); 23 | } 24 | } -------------------------------------------------------------------------------- /linearList-stack-link/src/main/java/org/light4j/dataStructure/linearList/Node.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | /** 4 | * 链表节点类 5 | * 6 | * @author longjiazuo 7 | */ 8 | public class Node { 9 | public E data;// 链表数据域,保存数据元素 10 | public Node next;// 链表地址域,引用后继节点 11 | 12 | public Node(E data, Node next) {// 构造节点,指定数据元素和后继节点 13 | this.data = data; 14 | this.next = next; 15 | } 16 | 17 | public Node(E data) { 18 | this(data, null); 19 | } 20 | 21 | public Node() { 22 | this(null, null); 23 | } 24 | } -------------------------------------------------------------------------------- /linearList-linkList/src/main/java/org/light4j/dataStructure/linearList/linkList/Node.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | /** 4 | * 链表节点类 5 | * 6 | * @author longjiazuo 7 | */ 8 | public class Node { 9 | public E data;// 链表数据域,保存数据元素 10 | public Node next;// 链表地址域,引用后继节点 11 | 12 | public Node(E data, Node next) {// 构造节点,指定数据元素和后继节点 13 | this.data = data; 14 | this.next = next; 15 | } 16 | 17 | public Node(E data) { 18 | this(data, null); 19 | } 20 | 21 | public Node() { 22 | this(null, null); 23 | } 24 | } -------------------------------------------------------------------------------- /linearList-linkList-head/src/main/java/org/light4j/dataStructure/linearList/linkList/Node.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | /** 4 | * 链表节点类 5 | * 6 | * @author longjiazuo 7 | */ 8 | public class Node { 9 | public E data;// 链表数据域,保存数据元素 10 | public Node next;// 链表地址域,引用后继节点 11 | 12 | public Node(E data, Node next) {// 构造节点,指定数据元素和后继节点 13 | this.data = data; 14 | this.next = next; 15 | } 16 | 17 | public Node(E data) { 18 | this(data, null); 19 | } 20 | 21 | public Node() { 22 | this(null, null); 23 | } 24 | } -------------------------------------------------------------------------------- /linearList-linkList-circular/src/main/java/org/light4j/dataStructure/linearList/linkList/Node.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | /** 4 | * 链表节点类 5 | * 6 | * @author longjiazuo 7 | */ 8 | public class Node { 9 | public E data;// 链表数据域,保存数据元素 10 | public Node next;// 链表地址域,引用后继节点 11 | 12 | public Node(E data, Node next) {// 构造节点,指定数据元素和后继节点 13 | this.data = data; 14 | this.next = next; 15 | } 16 | 17 | public Node(E data) { 18 | this(data, null); 19 | } 20 | 21 | public Node() { 22 | this(null, null); 23 | } 24 | } -------------------------------------------------------------------------------- /linearList-linkList-increase/src/main/java/org/light4j/dataStructure/linearList/linkList/Node.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | /** 4 | * 链表节点类 5 | * 6 | * @author longjiazuo 7 | */ 8 | public class Node { 9 | public E data;// 链表数据域,保存数据元素 10 | public Node next;// 链表地址域,引用后继节点 11 | 12 | public Node(E data, Node next) {// 构造节点,指定数据元素和后继节点 13 | this.data = data; 14 | this.next = next; 15 | } 16 | 17 | public Node(E data) { 18 | this(data, null); 19 | } 20 | 21 | public Node() { 22 | this(null, null); 23 | } 24 | } -------------------------------------------------------------------------------- /linearList-linkList-josephus/src/main/java/org/light4j/dataStructure/linearList/linkList/Node.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | /** 4 | * 链表节点类 5 | * 6 | * @author longjiazuo 7 | */ 8 | public class Node { 9 | public E data;// 链表数据域,保存数据元素 10 | public Node next;// 链表地址域,引用后继节点 11 | 12 | public Node(E data, Node next) {// 构造节点,指定数据元素和后继节点 13 | this.data = data; 14 | this.next = next; 15 | } 16 | 17 | public Node(E data) { 18 | this(data, null); 19 | } 20 | 21 | public Node() { 22 | this(null, null); 23 | } 24 | } -------------------------------------------------------------------------------- /linearList-linkList-reverse/src/main/java/org/light4j/dataStructure/linearList/linkList/Node.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | /** 4 | * 链表节点类 5 | * 6 | * @author longjiazuo 7 | */ 8 | public class Node { 9 | public E data;// 链表数据域,保存数据元素 10 | public Node next;// 链表地址域,引用后继节点 11 | 12 | public Node(E data, Node next) {// 构造节点,指定数据元素和后继节点 13 | this.data = data; 14 | this.next = next; 15 | } 16 | 17 | public Node(E data) { 18 | this(data, null); 19 | } 20 | 21 | public Node() { 22 | this(null, null); 23 | } 24 | } -------------------------------------------------------------------------------- /linearList-linkList-head-iterable/src/main/java/org/light4j/dataStructure/linearList/linkList/Node.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | /** 4 | * 链表节点类 5 | * 6 | * @author longjiazuo 7 | */ 8 | public class Node { 9 | public E data;// 链表数据域,保存数据元素 10 | public Node next;// 链表地址域,引用后继节点 11 | 12 | public Node(E data, Node next) {// 构造节点,指定数据元素和后继节点 13 | this.data = data; 14 | this.next = next; 15 | } 16 | 17 | public Node(E data) { 18 | this(data, null); 19 | } 20 | 21 | public Node() { 22 | this(null, null); 23 | } 24 | } -------------------------------------------------------------------------------- /linearList-linkList-head-sorted/src/main/java/org/light4j/dataStructure/linearList/linkList/Node.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | /** 4 | * 链表节点类 5 | * 6 | * @author longjiazuo 7 | */ 8 | public class Node { 9 | public E data;// 链表数据域,保存数据元素 10 | public Node next;// 链表地址域,引用后继节点 11 | 12 | public Node(E data, Node next) {// 构造节点,指定数据元素和后继节点 13 | this.data = data; 14 | this.next = next; 15 | } 16 | 17 | public Node(E data) { 18 | this(data, null); 19 | } 20 | 21 | public Node() { 22 | this(null, null); 23 | } 24 | } -------------------------------------------------------------------------------- /linearList-linkList-head/src/main/java/org/light4j/dataStructure/linearList/linkList/head/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.head; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class Test { 6 | public static void main(String[] args) { 7 | LList list = new HeadSinglyLinkedList(); 8 | for (int i = 0; i < 10; i++) { 9 | list.add(0, new String((char) ('A' + i) + "")); 10 | } 11 | System.out.println(list.toString()); 12 | list.remove(0);//移除第一个元素 13 | System.out.println(list.toString()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /linearList-queue-link/src/main/java/org/light4j/dataStructure/linearList/queue/QQueue.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.queue; 2 | 3 | /** 4 | * 队列接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface QQueue { 9 | /** 10 | * 判断队列是否为空,若为空返回true 11 | * 12 | * @return 13 | */ 14 | boolean isEmpty(); 15 | 16 | /** 17 | * 元素入队,操作成功返回true 18 | * 19 | * @param element 20 | * @return 21 | */ 22 | boolean enqueue(E element); 23 | 24 | /** 25 | * 出队,返回当前对头元素,若队列为空则返回null 26 | * 27 | * @return 28 | */ 29 | E dequeue(); 30 | } 31 | -------------------------------------------------------------------------------- /linearList-queue-sequence/src/main/java/org/light4j/dataStructure/linearList/queue/QQueue.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.queue; 2 | 3 | /** 4 | * 队列接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface QQueue { 9 | /** 10 | * 判断队列是否为空,若为空返回true 11 | * 12 | * @return 13 | */ 14 | boolean isEmpty(); 15 | 16 | /** 17 | * 元素入队,操作成功返回true 18 | * 19 | * @param element 20 | * @return 21 | */ 22 | boolean enqueue(E element); 23 | 24 | /** 25 | * 出队,返回当前对头元素,若队列为空则返回null 26 | * 27 | * @return 28 | */ 29 | E dequeue(); 30 | } 31 | -------------------------------------------------------------------------------- /linearList-queue-interface/src/main/java/org/light4j/dataStructure/linearList/queue/QQueue.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.queue; 2 | 3 | /** 4 | * 队列接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface QQueue { 9 | /** 10 | * 判断队列是否为空,若为空返回true 11 | * 12 | * @return 13 | */ 14 | boolean isEmpty(); 15 | 16 | /** 17 | * 元素入队,操作成功返回true 18 | * 19 | * @param element 20 | * @return 21 | */ 22 | boolean enqueue(E element); 23 | 24 | /** 25 | * 出队,返回当前对头元素,若队列为空则返回null 26 | * 27 | * @return 28 | */ 29 | E dequeue(); 30 | } 31 | -------------------------------------------------------------------------------- /linearList-queue-sequence-cycle/src/main/java/org/light4j/dataStructure/linearList/queue/QQueue.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.queue; 2 | 3 | /** 4 | * 队列接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface QQueue { 9 | /** 10 | * 判断队列是否为空,若为空返回true 11 | * 12 | * @return 13 | */ 14 | boolean isEmpty(); 15 | 16 | /** 17 | * 元素入队,操作成功返回true 18 | * 19 | * @param element 20 | * @return 21 | */ 22 | boolean enqueue(E element); 23 | 24 | /** 25 | * 出队,返回当前对头元素,若队列为空则返回null 26 | * 27 | * @return 28 | */ 29 | E dequeue(); 30 | } 31 | -------------------------------------------------------------------------------- /linearList-linkList-reverse/src/main/java/org/light4j/dataStructure/linearList/linkList/reverse/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.reverse; 2 | 3 | public class Test { 4 | /** 5 | * 测试 6 | * 7 | * @param args 8 | */ 9 | public static void main(String[] args) { 10 | ReverseSinglyLinkedList rslk = new ReverseSinglyLinkedList(); 11 | for (int i = 1; i < 10; i++) { 12 | rslk.add(0, new Integer(i)); 13 | } 14 | System.out.println("单链表: " + rslk.toString()); 15 | rslk.reverse(); 16 | System.out.println("逆转后的单链表: " + rslk.toString()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linearList-linkList-head-sorted/src/main/java/org/light4j/dataStructure/linearList/linkList/head/sorted/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.head.sorted; 2 | 3 | public class Test { 4 | public static void main(String[] args) { 5 | SortedHeadSinglyLinkedList list = new SortedHeadSinglyLinkedList(); 6 | int n = 10; 7 | System.out.print("insert: "); 8 | for (int i = 0; i < n; i++) { 9 | int k = (int) (Math.random() * 100);// 产生随机数 10 | list.add(new Integer(k)); 11 | System.out.print(k + " "); 12 | } 13 | System.out.println("\nlist " + list.toString()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /linearList-seqList-iterator/src/main/java/org/light4j/dataStructure/linearList/seqList/iterable/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.seqList.iterable; 2 | 3 | import java.util.Iterator; 4 | 5 | public class Test { 6 | /** 7 | * 测试 8 | * 9 | * @param args 10 | */ 11 | public static void main(String[] args) { 12 | IterableSeqList list = new IterableSeqList(); 13 | list.add("A"); 14 | list.add("B"); 15 | list.add("C"); 16 | list.add("D"); 17 | Iterator it = list.iterator(); 18 | while (it.hasNext()) { 19 | System.out.println(it.next()); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /linearList-linkList-circular/src/main/java/org/light4j/dataStructure/linearList/linkList/circular/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.circular; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class Test { 6 | public static void main(String[] args) { 7 | LList list = new CircularSinglyLinkedList(); 8 | for (int i = 0; i < 10; i++) { 9 | list.add(0, new String((char) ('A' + i) + "")); 10 | } 11 | System.out.println(list.toString()); 12 | System.out.println(list.get(1));//获取第一个元素 13 | list.remove(0);// 移除第一个元素 14 | System.out.println(list.toString()); 15 | } 16 | } -------------------------------------------------------------------------------- /linearList-linkList-doubleLink/src/main/java/org/light4j/dataStructure/linearList/linkList/doubleLink/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.doubleLink; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class Test { 6 | public static void main(String[] args) { 7 | LList list = new DoubleLinkedList(); 8 | list.add("A"); 9 | list.add("B"); 10 | System.out.println(list.toString()); 11 | list.remove(2);// 移除元素 12 | System.out.println(list.toString()); 13 | list.add(5, "C");// 追加元素 14 | list.add(2, "D");//追加元素 15 | System.out.println(list.toString()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /linearList-linkList-doubleLink-head-cycle/src/main/java/org/light4j/dataStructure/linearList/linkList/doubleLink/head/cycle/CycleHeadDoubleLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.doubleLink.head.cycle; 2 | 3 | import org.light4j.dataStructure.linearList.linkList.doubleLink.DoubleLinkNode; 4 | 5 | /** 6 | * 带头结点的循环双链表类 7 | * 8 | * @author longjiazuo 9 | */ 10 | public class CycleHeadDoubleLinkedList { 11 | protected DoubleLinkNode head; 12 | 13 | public CycleHeadDoubleLinkedList() { 14 | this.head = new DoubleLinkNode(null);// 头指针 15 | this.head.prev = this.head; 16 | this.head.next = this.head; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linearList-queue-link/src/main/java/org/light4j/dataStructure/linearList/queue/link/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.queue.link; 2 | 3 | import org.light4j.dataStructure.linearList.queue.QQueue; 4 | 5 | public class Test { 6 | public static void main(String[] args) { 7 | QQueue queue = new LinkedQueue(); 8 | queue.dequeue();//出栈 9 | System.out.println(queue.toString()); 10 | queue.enqueue("A");// 元素在队尾入队 11 | queue.enqueue("B"); 12 | queue.enqueue("C"); 13 | queue.enqueue("D"); 14 | System.out.println(queue.toString()); 15 | queue.dequeue();// 对头出队 16 | System.out.println(queue.toString()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linearList-interface/src/main/java/org/light4j/dataStructure/linearList/LList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | /** 4 | * 线性表接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface LList { 9 | boolean isEmpty(); // 判断线性表是否为空,若为空则返回true 10 | 11 | int length(); // 返回线性表长度 12 | 13 | E get(int index); // 返回序号为index的对象,index初始值为0 14 | 15 | E set(int index, E element); // 设置序号为index对象为element,返回原对象 16 | 17 | boolean add(int index, E element); // 插入element对象,插入后对象序号为index 18 | 19 | boolean add(E element); // 插入element对象,插入位置没有约定 20 | 21 | E remove(int index); // 移去序号为index的对象,返回被移去对象 22 | 23 | void clear(); // 清空线性表 24 | } 25 | -------------------------------------------------------------------------------- /linearList-linkList-head-iterable/src/main/java/org/light4j/dataStructure/linearList/linkList/head/iterable/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.head.iterable; 2 | 3 | import java.util.Iterator; 4 | 5 | public class Test { 6 | /** 7 | * 测试 8 | * 9 | * @param args 10 | */ 11 | public static void main(String[] args) { 12 | IterableHeadSinglyLinkedList list = new IterableHeadSinglyLinkedList(); 13 | list.add("A"); 14 | list.add("B"); 15 | list.add("C"); 16 | list.add("D"); 17 | Iterator it = list.iterator(); 18 | while (it.hasNext()) { 19 | System.out.println(it.next()); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /linearList-linkList/src/main/java/org/light4j/dataStructure/linearList/LList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | /** 4 | * 线性表接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface LList { 9 | boolean isEmpty(); // 判断线性表是否为空,若为空则返回true 10 | 11 | int length(); // 返回线性表长度 12 | 13 | E get(int index); // 返回序号为index的对象,index初始值为0 14 | 15 | E set(int index, E element); // 设置序号为index对象为element,返回原对象 16 | 17 | boolean add(int index, E element); // 插入element对象,插入后对象序号为index 18 | 19 | boolean add(E element); // 插入element对象,插入位置没有约定 20 | 21 | E remove(int index); // 移去序号为index的对象,返回被移去对象 22 | 23 | void clear(); // 清空线性表 24 | } 25 | -------------------------------------------------------------------------------- /linearList-queue-sequence/src/main/java/org/light4j/dataStructure/linearList/queue/sequence/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.queue.sequence; 2 | 3 | import org.light4j.dataStructure.linearList.queue.QQueue; 4 | 5 | public class Test { 6 | public static void main(String[] args) { 7 | QQueue queue = new SeqQueue(); 8 | queue.dequeue();//出栈 9 | System.out.println(queue.toString()); 10 | queue.enqueue("A");// 元素在队尾入队 11 | queue.enqueue("B"); 12 | queue.enqueue("C"); 13 | queue.enqueue("D"); 14 | System.out.println(queue.toString()); 15 | queue.dequeue();// 对头出队 16 | System.out.println(queue.toString()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linearList-seqList/src/main/java/org/light4j/dataStructure/linearList/LList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | /** 4 | * 线性表接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface LList { 9 | boolean isEmpty(); // 判断线性表是否为空,若为空则返回true 10 | 11 | int length(); // 返回线性表长度 12 | 13 | E get(int index); // 返回序号为index的对象,index初始值为0 14 | 15 | E set(int index, E element); // 设置序号为index对象为element,返回原对象 16 | 17 | boolean add(int index, E element); // 插入element对象,插入后对象序号为index 18 | 19 | boolean add(E element); // 插入element对象,插入位置没有约定 20 | 21 | E remove(int index); // 移去序号为index的对象,返回被移去对象 22 | 23 | void clear(); // 清空线性表 24 | } 25 | -------------------------------------------------------------------------------- /linearList-linkList-head/src/main/java/org/light4j/dataStructure/linearList/LList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | /** 4 | * 线性表接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface LList { 9 | boolean isEmpty(); // 判断线性表是否为空,若为空则返回true 10 | 11 | int length(); // 返回线性表长度 12 | 13 | E get(int index); // 返回序号为index的对象,index初始值为0 14 | 15 | E set(int index, E element); // 设置序号为index对象为element,返回原对象 16 | 17 | boolean add(int index, E element); // 插入element对象,插入后对象序号为index 18 | 19 | boolean add(E element); // 插入element对象,插入位置没有约定 20 | 21 | E remove(int index); // 移去序号为index的对象,返回被移去对象 22 | 23 | void clear(); // 清空线性表 24 | } 25 | -------------------------------------------------------------------------------- /linearList-linkList-reverse/src/main/java/org/light4j/dataStructure/linearList/LList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | /** 4 | * 线性表接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface LList { 9 | boolean isEmpty(); // 判断线性表是否为空,若为空则返回true 10 | 11 | int length(); // 返回线性表长度 12 | 13 | E get(int index); // 返回序号为index的对象,index初始值为0 14 | 15 | E set(int index, E element); // 设置序号为index对象为element,返回原对象 16 | 17 | boolean add(int index, E element); // 插入element对象,插入后对象序号为index 18 | 19 | boolean add(E element); // 插入element对象,插入位置没有约定 20 | 21 | E remove(int index); // 移去序号为index的对象,返回被移去对象 22 | 23 | void clear(); // 清空线性表 24 | } 25 | -------------------------------------------------------------------------------- /linearList-seqList-iterator/src/main/java/org/light4j/dataStructure/linearList/LList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | /** 4 | * 线性表接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface LList { 9 | boolean isEmpty(); // 判断线性表是否为空,若为空则返回true 10 | 11 | int length(); // 返回线性表长度 12 | 13 | E get(int index); // 返回序号为index的对象,index初始值为0 14 | 15 | E set(int index, E element); // 设置序号为index对象为element,返回原对象 16 | 17 | boolean add(int index, E element); // 插入element对象,插入后对象序号为index 18 | 19 | boolean add(E element); // 插入element对象,插入位置没有约定 20 | 21 | E remove(int index); // 移去序号为index的对象,返回被移去对象 22 | 23 | void clear(); // 清空线性表 24 | } 25 | -------------------------------------------------------------------------------- /linearList-seqList-josephus/src/main/java/org/light4j/dataStructure/linearList/LList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | /** 4 | * 线性表接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface LList { 9 | boolean isEmpty(); // 判断线性表是否为空,若为空则返回true 10 | 11 | int length(); // 返回线性表长度 12 | 13 | E get(int index); // 返回序号为index的对象,index初始值为0 14 | 15 | E set(int index, E element); // 设置序号为index对象为element,返回原对象 16 | 17 | boolean add(int index, E element); // 插入element对象,插入后对象序号为index 18 | 19 | boolean add(E element); // 插入element对象,插入位置没有约定 20 | 21 | E remove(int index); // 移去序号为index的对象,返回被移去对象 22 | 23 | void clear(); // 清空线性表 24 | } 25 | -------------------------------------------------------------------------------- /linearList-linkList-circular/src/main/java/org/light4j/dataStructure/linearList/LList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | /** 4 | * 线性表接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface LList { 9 | boolean isEmpty(); // 判断线性表是否为空,若为空则返回true 10 | 11 | int length(); // 返回线性表长度 12 | 13 | E get(int index); // 返回序号为index的对象,index初始值为0 14 | 15 | E set(int index, E element); // 设置序号为index对象为element,返回原对象 16 | 17 | boolean add(int index, E element); // 插入element对象,插入后对象序号为index 18 | 19 | boolean add(E element); // 插入element对象,插入位置没有约定 20 | 21 | E remove(int index); // 移去序号为index的对象,返回被移去对象 22 | 23 | void clear(); // 清空线性表 24 | } 25 | -------------------------------------------------------------------------------- /linearList-linkList-doubleLink/src/main/java/org/light4j/dataStructure/linearList/LList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | /** 4 | * 线性表接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface LList { 9 | boolean isEmpty(); // 判断线性表是否为空,若为空则返回true 10 | 11 | int length(); // 返回线性表长度 12 | 13 | E get(int index); // 返回序号为index的对象,index初始值为0 14 | 15 | E set(int index, E element); // 设置序号为index对象为element,返回原对象 16 | 17 | boolean add(int index, E element); // 插入element对象,插入后对象序号为index 18 | 19 | boolean add(E element); // 插入element对象,插入位置没有约定 20 | 21 | E remove(int index); // 移去序号为index的对象,返回被移去对象 22 | 23 | void clear(); // 清空线性表 24 | } 25 | -------------------------------------------------------------------------------- /linearList-linkList-head-sorted/src/main/java/org/light4j/dataStructure/linearList/LList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | /** 4 | * 线性表接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface LList { 9 | boolean isEmpty(); // 判断线性表是否为空,若为空则返回true 10 | 11 | int length(); // 返回线性表长度 12 | 13 | E get(int index); // 返回序号为index的对象,index初始值为0 14 | 15 | E set(int index, E element); // 设置序号为index对象为element,返回原对象 16 | 17 | boolean add(int index, E element); // 插入element对象,插入后对象序号为index 18 | 19 | boolean add(E element); // 插入element对象,插入位置没有约定 20 | 21 | E remove(int index); // 移去序号为index的对象,返回被移去对象 22 | 23 | void clear(); // 清空线性表 24 | } 25 | -------------------------------------------------------------------------------- /linearList-linkList-increase/src/main/java/org/light4j/dataStructure/linearList/LList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | /** 4 | * 线性表接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface LList { 9 | boolean isEmpty(); // 判断线性表是否为空,若为空则返回true 10 | 11 | int length(); // 返回线性表长度 12 | 13 | E get(int index); // 返回序号为index的对象,index初始值为0 14 | 15 | E set(int index, E element); // 设置序号为index对象为element,返回原对象 16 | 17 | boolean add(int index, E element); // 插入element对象,插入后对象序号为index 18 | 19 | boolean add(E element); // 插入element对象,插入位置没有约定 20 | 21 | E remove(int index); // 移去序号为index的对象,返回被移去对象 22 | 23 | void clear(); // 清空线性表 24 | } 25 | -------------------------------------------------------------------------------- /linearList-linkList-josephus/src/main/java/org/light4j/dataStructure/linearList/LList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | /** 4 | * 线性表接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface LList { 9 | boolean isEmpty(); // 判断线性表是否为空,若为空则返回true 10 | 11 | int length(); // 返回线性表长度 12 | 13 | E get(int index); // 返回序号为index的对象,index初始值为0 14 | 15 | E set(int index, E element); // 设置序号为index对象为element,返回原对象 16 | 17 | boolean add(int index, E element); // 插入element对象,插入后对象序号为index 18 | 19 | boolean add(E element); // 插入element对象,插入位置没有约定 20 | 21 | E remove(int index); // 移去序号为index的对象,返回被移去对象 22 | 23 | void clear(); // 清空线性表 24 | } 25 | -------------------------------------------------------------------------------- /linearList-linkList-head-iterable/src/main/java/org/light4j/dataStructure/linearList/LList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | /** 4 | * 线性表接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface LList { 9 | boolean isEmpty(); // 判断线性表是否为空,若为空则返回true 10 | 11 | int length(); // 返回线性表长度 12 | 13 | E get(int index); // 返回序号为index的对象,index初始值为0 14 | 15 | E set(int index, E element); // 设置序号为index对象为element,返回原对象 16 | 17 | boolean add(int index, E element); // 插入element对象,插入后对象序号为index 18 | 19 | boolean add(E element); // 插入element对象,插入位置没有约定 20 | 21 | E remove(int index); // 移去序号为index的对象,返回被移去对象 22 | 23 | void clear(); // 清空线性表 24 | } 25 | -------------------------------------------------------------------------------- /linearList-queue-sequence-cycle/src/main/java/org/light4j/dataStructure/linearList/queue/sequence/cycle/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.queue.sequence.cycle; 2 | 3 | import org.light4j.dataStructure.linearList.queue.QQueue; 4 | 5 | public class Test { 6 | public static void main(String[] args) { 7 | QQueue queue = new SeqCycleQueue(); 8 | queue.dequeue();// 出栈 9 | System.out.println(queue.toString()); 10 | queue.enqueue("A");// 元素在队尾入队 11 | queue.enqueue("B"); 12 | queue.enqueue("C"); 13 | queue.enqueue("D"); 14 | System.out.println(queue.toString()); 15 | queue.dequeue();// 对头出队 16 | System.out.println(queue.toString()); 17 | } 18 | } -------------------------------------------------------------------------------- /linearList-stack-link/src/main/java/org/light4j/dataStructure/linearList/stack/SStack.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.stack; 2 | 3 | /** 4 | * 栈接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface SStack { 9 | /** 10 | * 判断栈是否为空,若为空则返回true 11 | * 12 | * @return 13 | */ 14 | boolean isEmpty(); 15 | 16 | /** 17 | * element元素入栈,若操作成功返回true 18 | * 19 | * @param element 20 | * @return 21 | */ 22 | boolean push(E element); 23 | 24 | /** 25 | * 出栈,返回当前栈顶元素,若栈空返回null 26 | * 27 | * @return 28 | */ 29 | E pop(); 30 | 31 | /** 32 | * 取栈顶元素值,未出栈,若栈空返回null 33 | * 34 | * @return 35 | */ 36 | E get(); 37 | } -------------------------------------------------------------------------------- /linearList-stack-link/src/main/java/org/light4j/dataStructure/linearList/stack/link/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.stack.link; 2 | 3 | import org.light4j.dataStructure.linearList.stack.SStack; 4 | 5 | public class Test { 6 | public static void main(String[] args) { 7 | SStack stack = new LinkedStack(); 8 | System.out.println(stack.isEmpty());//判空 9 | stack.push("A");//入栈 10 | stack.push("B"); 11 | stack.push("C"); 12 | System.out.println(stack.toString()); 13 | stack.pop();// 出栈 14 | stack.push("D"); 15 | stack.get();// 取栈顶元素 16 | System.out.println(stack.toString()); 17 | System.out.println(stack.isEmpty());//判空 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /linearList-stack-sequence/src/main/java/org/light4j/dataStructure/linearList/stack/SStack.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.stack; 2 | 3 | /** 4 | * 栈接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface SStack { 9 | /** 10 | * 判断栈是否为空,若为空则返回true 11 | * 12 | * @return 13 | */ 14 | boolean isEmpty(); 15 | 16 | /** 17 | * element元素入栈,若操作成功返回true 18 | * 19 | * @param element 20 | * @return 21 | */ 22 | boolean push(E element); 23 | 24 | /** 25 | * 出栈,返回当前栈顶元素,若栈空返回null 26 | * 27 | * @return 28 | */ 29 | E pop(); 30 | 31 | /** 32 | * 取栈顶元素值,未出栈,若栈空返回null 33 | * 34 | * @return 35 | */ 36 | E get(); 37 | } -------------------------------------------------------------------------------- /linearList-linkList-doubleLink-head-cycle/src/main/java/org/light4j/dataStructure/linearList/LList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | /** 4 | * 线性表接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface LList { 9 | boolean isEmpty(); // 判断线性表是否为空,若为空则返回true 10 | 11 | int length(); // 返回线性表长度 12 | 13 | E get(int index); // 返回序号为index的对象,index初始值为0 14 | 15 | E set(int index, E element); // 设置序号为index对象为element,返回原对象 16 | 17 | boolean add(int index, E element); // 插入element对象,插入后对象序号为index 18 | 19 | boolean add(E element); // 插入element对象,插入位置没有约定 20 | 21 | E remove(int index); // 移去序号为index的对象,返回被移去对象 22 | 23 | void clear(); // 清空线性表 24 | } 25 | -------------------------------------------------------------------------------- /linearList-stack-interface/src/main/java/org/light4j/dataStructure/linearList/stack/SStack.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.stack; 2 | 3 | /** 4 | * 栈接口 5 | * 6 | * @author longjiazuo 7 | */ 8 | public interface SStack { 9 | /** 10 | * 判断栈是否为空,若为空则返回true 11 | * 12 | * @return 13 | */ 14 | boolean isEmpty(); 15 | 16 | /** 17 | * element元素入栈,若操作成功返回true 18 | * 19 | * @param element 20 | * @return 21 | */ 22 | boolean push(E element); 23 | 24 | /** 25 | * 出栈,返回当前栈顶元素,若栈空返回null 26 | * 27 | * @return 28 | */ 29 | E pop(); 30 | 31 | /** 32 | * 取栈顶元素值,未出栈,若栈空返回null 33 | * 34 | * @return 35 | */ 36 | E get(); 37 | } 38 | -------------------------------------------------------------------------------- /linearList-stack-sequence/src/main/java/org/light4j/dataStructure/linearList/stack/sequence/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.stack.sequence; 2 | 3 | import org.light4j.dataStructure.linearList.stack.SStack; 4 | 5 | public class Test { 6 | public static void main(String[] args) { 7 | SStack stack = new SeqStack(); 8 | System.out.println(stack.isEmpty());//判空 9 | stack.push("A");//入栈 10 | stack.push("B"); 11 | stack.push("C"); 12 | System.out.println(stack.toString()); 13 | stack.pop();// 出栈 14 | stack.push("D"); 15 | stack.get();// 取栈顶元素 16 | System.out.println(stack.toString()); 17 | System.out.println(stack.isEmpty());//判空 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /linearList-linkList/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-linkList 12 | linearList-linkList 13 | http://blog.longjiazuo.com/ 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-seqList/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-seqList 12 | linearList-seqList 13 | http://blog.longjiazuo.com/ 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-interface/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-interface 12 | linearList-interface 13 | http://blog.longjiazuo.com/ 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-queue-link/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-queue-link 12 | linearList-queue-link 13 | http://blog.longjiazuo.com 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-stack-link/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-stack-link 12 | linearList-stack-link 13 | http://blog.longjiazuo.com 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-linkList-head/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-linkList-head 12 | linearList-linkList-head 13 | http://blog.longjiazuo.com/ 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-queue-interface/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-queue-interface 12 | linearList-queue-interface 13 | http://blog.longjiazuo.com 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-queue-sequence/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-queue-sequence 12 | linearList-queue-sequence 13 | http://blog.longjiazuo.com 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-stack-interface/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-stack-interface 12 | linearList-stack-interface 13 | http://blog.longjiazuo.com 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-stack-sequence/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-stack-sequence 12 | linearList-stack-sequence 13 | http://blog.longjiazuo.com 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-linkList-circular/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-linkList-circular 12 | linearList-linkList-circular 13 | http://blog.longjiazuo.com 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-linkList-increase/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-linkList-increase 12 | linearList-linkList-increase 13 | http://blog.longjiazuo.com 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-linkList-reverse/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-linkList-reverse 12 | linearList-linkList-reverse 13 | http://blog.longjiazuo.com/ 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-seqList-iterator/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-seqList-iterator 12 | linearList-seqList-iterator 13 | http://blog.longjiazuo.com/ 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-seqList-josephus/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-seqList-josephus 12 | linearList-seqList-josephus 13 | http://blog.longjiazuo.com/ 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-linkList-josephus/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-linkList-josephus 12 | linearList-linkList-josephus 13 | http://blog.longjiazuo.com/ 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-linkList-doubleLink/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-linkList-doubleLink 12 | linearList-linkList-doubleLink 13 | http://blog.longjiazuo.com 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-linkList-head-sorted/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-linkList-head-sorted 12 | linearList-linkList-head-sorted 13 | http://blog.longjiazuo.com/ 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-queue-sequence-cycle/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-queue-sequence-cycle 12 | linearList-queue-sequence-cycle 13 | http://blog.longjiazuo.com 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-linkList-head-iterable/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-linkList-head-iterable 12 | linearList-linkList-head-iterable 13 | http://blog.longjiazuo.com/ 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /linearList-linkList-doubleLink-head-cycle/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | linearList-linkList-doubleLink-head-cycle 12 | linearList-linkList-doubleLink-head-cycle 13 | http://blog.longjiazuo.com/ 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /tree-binaryTree/src/main/java/org/light4j/tree/binaryTree/BinaryNode.java: -------------------------------------------------------------------------------- 1 | package org.light4j.tree.binaryTree; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * 二叉链表结点类 7 | * 8 | * @author longjiazuo 9 | * 10 | * @param 11 | */ 12 | @Data 13 | public class BinaryNode { 14 | // 数据元素 15 | private E data; 16 | // 左孩子结点 17 | private BinaryNode left; 18 | // 右孩子结点 19 | private BinaryNode right; 20 | 21 | // 指定数据元素,左右孩子结点的构造函数 22 | public BinaryNode(E data, BinaryNode left, BinaryNode right) { 23 | this.data = data; 24 | this.left = left; 25 | this.right = right; 26 | } 27 | 28 | // 指定数据元素的构造函数 29 | public BinaryNode(E data) { 30 | this(data, null, null); 31 | } 32 | 33 | // 空构造函数 34 | public BinaryNode() { 35 | this(null, null, null); 36 | } 37 | } -------------------------------------------------------------------------------- /tree-binaryTree/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.light4j 8 | data-structure-project 9 | 0.0.1-SNAPSHOT 10 | 11 | tree-binaryTree 12 | tree-binaryTree 13 | http://blog.longjiazuo.com/ 14 | 15 | UTF-8 16 | 17 | 18 | 19 | org.projectlombok 20 | lombok 21 | 1.16.8 22 | 23 | 24 | -------------------------------------------------------------------------------- /linearList-seqList-josephus/src/main/java/org/light4j/dataStructure/linearList/seqList/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.seqList; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class Test { 6 | public static void main(String[] args) { 7 | // 初始化默认值容量的SeqList 8 | LList defalutValueList = new SeqList(); 9 | // 添加A,B,C三个元素 10 | defalutValueList.add("A"); 11 | defalutValueList.add("B"); 12 | defalutValueList.add("C"); 13 | // 输出元素个数 14 | System.out.println(defalutValueList.length()); 15 | // 初始化容量值为10的SeqList 16 | LList specifiedValueList = new SeqList(10); 17 | // 添加D,E,F,G四个元素 18 | specifiedValueList.add("D"); 19 | specifiedValueList.add("E"); 20 | specifiedValueList.add("F"); 21 | specifiedValueList.add("G"); 22 | // 输出元素个数 23 | System.out.println(specifiedValueList.length()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /linearList-linkList-doubleLink/src/main/java/org/light4j/dataStructure/linearList/linkList/doubleLink/DoubleLinkNode.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.doubleLink; 2 | 3 | /** 4 | * 双链表结点类 5 | * 6 | * @author longjiazuo 7 | */ 8 | public class DoubleLinkNode { 9 | public E data;// 数据元素 10 | public DoubleLinkNode prev;// 前驱结点 11 | public DoubleLinkNode next;// 后继结点 12 | 13 | /** 14 | * 指定数据元素,前驱结点和后继结点的构造函数 15 | * 16 | * @param data 17 | * @param prev 18 | * @param next 19 | */ 20 | public DoubleLinkNode(E data, DoubleLinkNode prev, DoubleLinkNode next) { 21 | this.data = data; 22 | this.prev = prev; 23 | this.next = next; 24 | } 25 | 26 | /** 27 | * 指定数据元素的构造函数 28 | * 29 | * @param data 30 | */ 31 | public DoubleLinkNode(E data) { 32 | this(data, null, null); 33 | } 34 | 35 | /** 36 | * 默认构造函数 37 | */ 38 | public DoubleLinkNode() { 39 | } 40 | } -------------------------------------------------------------------------------- /linearList-linkList-reverse/src/main/java/org/light4j/dataStructure/linearList/linkList/reverse/ReverseSinglyLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.reverse; 2 | 3 | import org.light4j.dataStructure.linearList.linkList.Node; 4 | import org.light4j.dataStructure.linearList.linkList.SinglyLinkedList; 5 | 6 | /** 7 | * 可逆转的单链表,继承自SinglyLinkedList 8 | * 9 | * @author longjiazuo 10 | */ 11 | public class ReverseSinglyLinkedList extends SinglyLinkedList { 12 | public ReverseSinglyLinkedList() { 13 | super();// 调用父类同参数的构造方法 14 | } 15 | 16 | /** 17 | * 实现单链表逆转的方法 18 | */ 19 | public void reverse() { 20 | Node p = this.head; 21 | Node front = null; 22 | Node q = null; 23 | while (p != null) { 24 | q = p.next;// 设置q结点是p的后继结点 25 | p.next = front;// 使p的后继结点指向其前驱节点 26 | front = p;// front结点向后移动 27 | p = q;// p结点向后移动 28 | } 29 | this.head = front;// 头结点指向front 30 | } 31 | } -------------------------------------------------------------------------------- /linearList-seqList/src/main/java/org/light4j/dataStructure/linearList/seqList/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.seqList; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class Test { 6 | public static void main(String[] args) { 7 | // 初始化默认值容量的SeqList 8 | LList defalutValueList = new SeqList(); 9 | // 添加A,B,C三个元素 10 | defalutValueList.add("A"); 11 | defalutValueList.add("B"); 12 | defalutValueList.add("C"); 13 | // 输出元素个数 14 | System.out.println("defalutValueList线性表元素个数:"+defalutValueList.length()); 15 | // 初始化容量值为10的SeqList 16 | LList specifiedValueList = new SeqList(10); 17 | // 添加D,E,F,G四个元素 18 | specifiedValueList.add("D"); 19 | specifiedValueList.add("E"); 20 | specifiedValueList.add("F"); 21 | specifiedValueList.add("G"); 22 | // 输出元素个数 23 | System.out.println("specifiedValueList线性表元素个数:"+specifiedValueList.length()); 24 | } 25 | } -------------------------------------------------------------------------------- /linearList-linkList-doubleLink-head-cycle/src/main/java/org/light4j/dataStructure/linearList/linkList/doubleLink/DoubleLinkNode.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.doubleLink; 2 | 3 | /** 4 | * 双链表结点类 5 | * 6 | * @author longjiazuo 7 | */ 8 | public class DoubleLinkNode { 9 | public E data;// 数据元素 10 | public DoubleLinkNode prev;// 前驱结点 11 | public DoubleLinkNode next;// 后继结点 12 | 13 | /** 14 | * 指定数据元素,前驱结点和后继结点的构造函数 15 | * 16 | * @param data 17 | * @param prev 18 | * @param next 19 | */ 20 | public DoubleLinkNode(E data, DoubleLinkNode prev, DoubleLinkNode next) { 21 | this.data = data; 22 | this.prev = prev; 23 | this.next = next; 24 | } 25 | 26 | /** 27 | * 指定数据元素的构造函数 28 | * 29 | * @param data 30 | */ 31 | public DoubleLinkNode(E data) { 32 | this(data, null, null); 33 | } 34 | 35 | /** 36 | * 默认构造函数 37 | */ 38 | public DoubleLinkNode() { 39 | } 40 | } -------------------------------------------------------------------------------- /tree-binaryTree/src/main/java/org/light4j/tree/binaryTree/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.tree.binaryTree; 2 | 3 | public class Test { 4 | public static void main(String[] args) { 5 | // D结点 6 | BinaryNode node_d = new BinaryNode("D", null, 7 | new BinaryNode("G")); 8 | // B结点 9 | BinaryNode node_b = new BinaryNode("B", node_d, null); 10 | 11 | // F结点 12 | BinaryNode node_f = new BinaryNode("F", 13 | new BinaryNode("H"), null); 14 | // C结点 15 | BinaryNode node_c = new BinaryNode("C", 16 | new BinaryNode("E"), node_f); 17 | 18 | // A结点 19 | BinaryNode node_a = new BinaryNode("A", node_b, node_c); 20 | // 创建二叉树 21 | BinaryTree binaryTree = new BinaryTree<>(node_a); 22 | // 先根遍历 23 | binaryTree.preOrder(); 24 | // 中根遍历 25 | binaryTree.inOrder(); 26 | // 后根遍历 27 | binaryTree.postOrder(); 28 | // 二叉树结点个数 29 | System.out.println("\n 结点个数:" + binaryTree.count()); 30 | // 二叉树的高度 31 | System.out.println("\n 树的高度:" + binaryTree.height()); 32 | } 33 | } -------------------------------------------------------------------------------- /linearList-seqList-iterator/src/main/java/org/light4j/dataStructure/linearList/AbstractLList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | import java.util.Iterator; 4 | 5 | /** 6 | * 抽象线性表类 7 | * 8 | * @author longjiazuo 9 | */ 10 | public abstract class AbstractLList implements Iterable { 11 | 12 | /** 13 | * 获得迭代器对象,抽象方法,由子类进行实现 14 | */ 15 | public abstract Iterator iterator(); 16 | 17 | /** 18 | * 返回线性表所有元素组成的字符串 19 | */ 20 | @Override 21 | public String toString() { 22 | String str = "("; 23 | Iterator it = iterator();// 获取一个迭代器对象 24 | while (it.hasNext()) {// 判断是否有后继元素 25 | E element = it.next();// 获取后继元素 26 | str += element.toString(); 27 | if (it.hasNext()) { 28 | str += ","; 29 | } 30 | } 31 | return str + ")"; 32 | } 33 | 34 | /** 35 | * 判断线性表是否包含指定的元素 36 | * 37 | * @param obj 38 | * @return 39 | */ 40 | public boolean contain(Object obj) { 41 | if (obj != null) { 42 | Iterator it = iterator(); 43 | while (it.hasNext()) { 44 | E elment = it.next(); 45 | if (obj.equals(elment)) {// 如果找到相等的则返回true 46 | return true; 47 | } 48 | } 49 | } 50 | return false; 51 | } 52 | } -------------------------------------------------------------------------------- /linearList-linkList-head-iterable/src/main/java/org/light4j/dataStructure/linearList/AbstractLList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList; 2 | 3 | import java.util.Iterator; 4 | 5 | /** 6 | * 抽象线性表类 7 | * 8 | * @author longjiazuo 9 | */ 10 | public abstract class AbstractLList implements Iterable { 11 | 12 | /** 13 | * 获得迭代器对象,抽象方法,由子类进行实现 14 | */ 15 | public abstract Iterator iterator(); 16 | 17 | /** 18 | * 返回线性表所有元素组成的字符串 19 | */ 20 | @Override 21 | public String toString() { 22 | String str = "("; 23 | Iterator it = iterator();// 获取一个迭代器对象 24 | while (it.hasNext()) {// 判断是否有后继元素 25 | E element = it.next();// 获取后继元素 26 | str += element.toString(); 27 | if (it.hasNext()) { 28 | str += ","; 29 | } 30 | } 31 | return str + ")"; 32 | } 33 | 34 | /** 35 | * 判断线性表是否包含指定的元素 36 | * 37 | * @param obj 38 | * @return 39 | */ 40 | public boolean contain(Object obj) { 41 | if (obj != null) { 42 | Iterator it = iterator(); 43 | while (it.hasNext()) { 44 | E elment = it.next(); 45 | if (obj.equals(elment)) {// 如果找到相等的则返回true 46 | return true; 47 | } 48 | } 49 | } 50 | return false; 51 | } 52 | } -------------------------------------------------------------------------------- /linearList-linkList-josephus/src/main/java/org/light4j/dataStructure/linearList/linkList/Josephus.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class Josephus { 6 | private LList seqList; 7 | private int distance; 8 | 9 | // 创建约瑟夫环并求解,参数指定环长度,起始位置,计数 10 | public Josephus(int number, int distance) { 11 | if(number<=0||distance<=0){ 12 | throw new RuntimeException("number或distance参数输入不合法"); 13 | } 14 | this.distance=distance; 15 | this.seqList = new SinglyLinkedList();// 单链表元素类型是字符串 16 | for (int i = 0; i < number; i++) { 17 | this.seqList.add((char) ('A' + i) + "");// 添加字符串对象 18 | } 19 | System.out.print("约瑟夫环(" + number + "," + distance + "), "); 20 | System.out.println(this.seqList.toString());// 显示单链表所有对象的字符串表示 21 | } 22 | 23 | public void sentence(int start) { 24 | int index=start-1; 25 | while (this.seqList.length() > 1) { 26 | index = (index + this.distance - 1) % this.seqList.length(); 27 | System.out.print("删除" + this.seqList.remove(index).toString() + ", ");// 删除指定位置元素对象 28 | System.out.println(this.seqList.toString()); 29 | } 30 | System.out.println("被赦免者是"+this.seqList.get(0).toString()); 31 | } 32 | 33 | public static void main(String[] args) { 34 | new Josephus(5,2).sentence(1); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /linearList-seqList-josephus/src/main/java/org/light4j/dataStructure/linearList/seqList/Josephus.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.seqList; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class Josephus { 6 | private LList seqList; 7 | private int distance; 8 | 9 | // 创建约瑟夫环并求解,参数指定环长度,起始位置,计数 10 | public Josephus(int number, int distance) { 11 | if(number<=0||distance<=0){ 12 | throw new RuntimeException("number或distance参数输入不合法"); 13 | } 14 | this.distance=distance; 15 | this.seqList = new SeqList(number);// 顺序表元素类型是字符串,指定容量 16 | for (int i = 0; i < number; i++) { 17 | this.seqList.add((char) ('A' + i) + "");// 添加字符串对象 18 | } 19 | System.out.print("约瑟夫环(" + number + "," + distance + "), "); 20 | System.out.println(this.seqList.toString());// 显示顺序表所有对象的字符串表示 21 | } 22 | 23 | public void sentence(int start) { 24 | int index=start-1; 25 | while (this.seqList.length() > 1) { 26 | index = (index + this.distance - 1) % this.seqList.length(); 27 | System.out.print("删除" + this.seqList.remove(index).toString() + ", ");// 删除指定位置元素对象 28 | System.out.println(this.seqList.toString()); 29 | } 30 | System.out.println("被赦免者是"+this.seqList.get(0).toString()); 31 | } 32 | 33 | public static void main(String[] args) { 34 | new Josephus(5,2).sentence(1); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /linearList-linkList-increase/src/main/java/org/light4j/dataStructure/linearList/linkList/increase/Test.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.increase; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Test { 7 | public static void main(String[] args) { 8 | String[] elements = { "A", "B", "C", "D", "E" }; 9 | IncreaseSinglyLinkedList list = new IncreaseSinglyLinkedList( 10 | elements); 11 | IncreaseSinglyLinkedList listByList = new IncreaseSinglyLinkedList( 12 | list); 13 | 14 | System.out.println(list.toString()); 15 | System.out.println(listByList.toString()); 16 | // listByList.concat(list);// 拼接 17 | System.out.println(listByList); 18 | // System.out.println(listByList.search("A"));// 查找 19 | // System.out.println(listByList.contain("A"));// 包含 20 | // System.out.println(listByList.remove("B"));// 移除 21 | // System.out.println(listByList.replace("A", "M"));// 替换 22 | List strlist = new ArrayList(); 23 | strlist.add("A"); 24 | strlist.add("B"); 25 | strlist.add("C"); 26 | strlist.add("D"); 27 | strlist.add("E"); 28 | System.out.println(strlist.toString()); 29 | System.out.println(listByList.equals(list));// 比较 30 | System.out.println(listByList.equals(strlist));// 比较 31 | System.out.println(listByList.toString()); 32 | 33 | } 34 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | org.light4j 5 | data-structure-project 6 | 0.0.1-SNAPSHOT 7 | pom 8 | 9 | linearList-interface 10 | linearList-seqList 11 | linearList-seqList-josephus 12 | linearList-linkList 13 | linearList-linkList-josephus 14 | linearList-linkList-reverse 15 | linearList-linkList-head 16 | linearList-linkList-doubleLink-cycle-head 17 | linearList-linkList-doubleLink-head-cycle 18 | linearList-linkList-head-iterable 19 | linearList-seqList-iterator 20 | linearList-linkList-head-sorted 21 | linearList-linkList-increase 22 | linearList-linkList-circular 23 | linearList-linkList-doubleLink 24 | linearList-stack-interface 25 | linearList-stack-sequence 26 | linearList-stack-link 27 | linearList-queue-interface 28 | linearList-queue-sequence 29 | linearList-queue-sequence-cycle 30 | linearList-queue-link 31 | tree-binaryTree 32 | 33 | -------------------------------------------------------------------------------- /linearList-stack-link/src/main/java/org/light4j/dataStructure/linearList/stack/link/LinkedStack.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.stack.link; 2 | 3 | import org.light4j.dataStructure.linearList.Node; 4 | import org.light4j.dataStructure.linearList.stack.SStack; 5 | 6 | /** 7 | * 链式栈 8 | * 9 | * @author longjiazuo 10 | * 11 | * @param 12 | */ 13 | public class LinkedStack implements SStack { 14 | private Node top;// 栈顶元素结点 15 | 16 | public LinkedStack() { 17 | this.top = null; 18 | } 19 | 20 | /** 21 | * 判断栈是否为空,若为空则返回true 22 | * 23 | * @return 24 | */ 25 | @Override 26 | public boolean isEmpty() { 27 | return this.top == null; 28 | } 29 | 30 | /** 31 | * 元素入栈,成为新的栈顶元素,若操作成功返回true 32 | */ 33 | @Override 34 | public boolean push(E element) { 35 | if (element == null) {// 空对象不能入栈 36 | return false; 37 | } 38 | this.top = new Node(element, this.top);// 新的元素作为栈顶元素,原来的栈顶元素作为其后继元素 39 | return true; 40 | } 41 | 42 | /** 43 | * 元素出栈,返回当前栈顶元素,栈顶元素改变,若栈为空则返回null 44 | */ 45 | @Override 46 | public E pop() { 47 | if (!isEmpty()) {// 判断是否为空空栈 48 | E temp = this.top.data;// 取栈顶元素 49 | this.top = this.top.next;// 删除原来的栈顶结点,改变栈顶元素 50 | return temp;// 返回原来的栈顶元素值 51 | } 52 | return null; 53 | } 54 | 55 | /** 56 | * 取栈顶元素值,元素未出栈,栈顶元素未改变 57 | */ 58 | @Override 59 | public E get() { 60 | if (!isEmpty()) {// 判断是否为空栈 61 | return this.top.data;// 返回栈顶元素值 62 | } 63 | return null; 64 | } 65 | 66 | /** 67 | * 返回栈中各元素的字符串表示 68 | */ 69 | @Override 70 | public String toString() { 71 | String str = "("; 72 | Node p = this.top;// 新建一个结点指向栈顶结点top 73 | while (p != null) { 74 | if (p.next == null) {// 判断是否有后继结点 75 | str += p.data; 76 | } else { 77 | str += p.data + ","; 78 | } 79 | p = p.next; 80 | } 81 | return str + ")"; 82 | } 83 | } -------------------------------------------------------------------------------- /linearList-queue-link/src/main/java/org/light4j/dataStructure/linearList/queue/link/LinkedQueue.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.queue.link; 2 | 3 | import org.light4j.dataStructure.linearList.Node; 4 | import org.light4j.dataStructure.linearList.queue.QQueue; 5 | 6 | /** 7 | * 链式队列 8 | * 9 | * @author longjiazuo 10 | * @param 11 | */ 12 | public class LinkedQueue implements QQueue { 13 | private Node front;// 队列头结点 14 | private Node rear;// 队列尾结点 15 | 16 | public LinkedQueue() {// 构造空队列 17 | this.front = null; 18 | this.rear = null; 19 | } 20 | 21 | /** 22 | * 判断队列是否为空,若为空返回true 23 | * 24 | * @return 25 | */ 26 | @Override 27 | public boolean isEmpty() { 28 | return this.front == null && this.rear == null; 29 | } 30 | 31 | /** 32 | * 元素入队,操作成功返回true 33 | * 34 | * @param element 35 | * @return 36 | */ 37 | @Override 38 | public boolean enqueue(E element) { 39 | if (element == null) {// 空元素不能入队 40 | return false; 41 | } 42 | Node q = new Node(element); 43 | if (!isEmpty()) {// 队列不为空,新的结点作为新的队尾结点 44 | this.rear.next = q; 45 | } else {// 队列为空,新的结点作为头结点 46 | this.front = q; 47 | } 48 | this.rear = q;// 队尾结点指向新结点q 49 | return true; 50 | } 51 | 52 | /** 53 | * 出队,返回当前对头元素,若队列为空则返回null 54 | * 55 | * @return 56 | */ 57 | @Override 58 | public E dequeue() { 59 | if (!isEmpty()) { 60 | E temp = this.front.data;// 取得对头元素 61 | this.front = this.front.next;// 删除对头结点 62 | if (this.front == null) {// 如果对头为空,则是空队列,队尾也置为空 63 | this.rear = null; 64 | } 65 | return temp; 66 | } 67 | return null; 68 | } 69 | 70 | /** 71 | * 返回队列中各元素的字符串表示 72 | */ 73 | @Override 74 | public String toString() { 75 | String str = "("; 76 | Node p = this.front; 77 | while (p != null) { 78 | if (p == this.rear) {// 尾结点 79 | str += p.data; 80 | } else { 81 | str += p.data + ","; 82 | } 83 | p = p.next; 84 | } 85 | return str + ")"; 86 | } 87 | } -------------------------------------------------------------------------------- /linearList-linkList-head-sorted/src/main/java/org/light4j/dataStructure/linearList/linkList/head/sorted/SortedHeadSinglyLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.head.sorted; 2 | 3 | import org.light4j.dataStructure.linearList.linkList.Node; 4 | import org.light4j.dataStructure.linearList.linkList.head.HeadSinglyLinkedList; 5 | 6 | /** 7 | * 排序的单链表,继承至带头结点的单链表类HeadSinglyLinkedList 8 | * 9 | * @author longjiazuo 10 | */ 11 | public class SortedHeadSinglyLinkedList extends HeadSinglyLinkedList { 12 | public SortedHeadSinglyLinkedList() { 13 | super(); 14 | } 15 | 16 | /** 17 | * 根据指定对象的大小把对象插入到合适的位置上,操作成功返回true 18 | */ 19 | @Override 20 | public boolean add(E element) { 21 | // 不能插入null或者非Comparable对象 22 | if (element == null || !(element instanceof Comparable)) { 23 | return false; 24 | } 25 | @SuppressWarnings("unchecked") 26 | Comparable cmp = (Comparable) element; 27 | Node front = this.head; 28 | Node p = front.next;// front结点是p的前驱结点 29 | while (p != null && (cmp.compareTo(p.data) > 0)) {// 比较对象大小 30 | front = p; 31 | p = p.next; 32 | } 33 | front.next = new Node(element, p);// 插入元素,插入位置在p结点之前 34 | if (p == null) { 35 | this.rear = front.next;// 尾插入则需要修改尾指针 36 | } 37 | this.n++; 38 | return true; 39 | } 40 | 41 | /** 42 | * 删除指定对象,操作成功则返回true 43 | * 44 | * @param element 45 | * @return 46 | */ 47 | public boolean remove(E element) { 48 | if (element == null || !(element instanceof Comparable)) { 49 | return false; 50 | } 51 | @SuppressWarnings("unchecked") 52 | Comparable cmp = (Comparable) element; 53 | Node front = this.head; 54 | Node p = front.next;// front结点是p的前驱结点 55 | while (p != null && (cmp.compareTo(p.data) > 0)) {// 比较对象大小 56 | front = p; 57 | p = p.next; 58 | } 59 | 60 | if (p == null || cmp.compareTo(p.data) == 0) {// 没有找到指定的结点,删除失败 61 | return false; 62 | } 63 | front.next = p.next;// 删除p结点 64 | if (p == this.rear) { 65 | this.rear = front; 66 | } 67 | this.n--; 68 | return true; 69 | } 70 | } -------------------------------------------------------------------------------- /linearList-stack-sequence/src/main/java/org/light4j/dataStructure/linearList/stack/sequence/SeqStack.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.stack.sequence; 2 | 3 | import org.light4j.dataStructure.linearList.stack.SStack; 4 | 5 | public class SeqStack implements SStack { 6 | private Object[] value;// 储存栈的数据元素的数组 7 | private int top;// 栈顶元素下标 8 | 9 | public SeqStack() {// 构造默认容量的空栈 10 | this(16);// 初始化栈的容量为16 11 | } 12 | 13 | public SeqStack(int capacity) {// 构造指定容量的空栈 14 | this.value = new Object[Math.abs(capacity)]; 15 | this.top = -1;// 栈顶下标初始化为-1 16 | } 17 | 18 | /** 19 | * 判断栈是否为空,若为空则返回true 20 | * 21 | * @return 22 | */ 23 | @Override 24 | public boolean isEmpty() { 25 | return this.top == -1; 26 | } 27 | 28 | /** 29 | * 元素入栈,成为新的栈顶元素,若操作成功返回true 30 | */ 31 | @Override 32 | public boolean push(E element) { 33 | if (element == null) {// 元素不允许为空 34 | return false; 35 | } 36 | if (this.top == value.length - 1) {// 栈满则需要扩充容量 37 | Object[] temp = this.value; 38 | this.value = new Object[temp.length * 2];// 扩充数组容量为原来的数组容量的两倍 39 | for (int i = 0; i < temp.length; i++) {// 复制元素到新的数组 40 | this.value[i] = temp[i]; 41 | } 42 | } 43 | this.top++;// 栈顶加1 44 | this.value[top] = element;// 把入栈的元素置于栈顶 45 | return true; 46 | } 47 | 48 | /** 49 | * 元素出栈,返回当前栈顶元素,栈顶元素改变,若栈为空则返回null 50 | */ 51 | @SuppressWarnings("unchecked") 52 | @Override 53 | public E pop() { 54 | if (isEmpty()) {// 如果是空栈则返回null 55 | return null; 56 | } 57 | return (E) this.value[top--];// 取出栈顶元素,栈顶元素改变,top减1 58 | } 59 | 60 | /** 61 | * 取栈顶元素值,元素未出栈,栈顶元素未改变 62 | */ 63 | @SuppressWarnings("unchecked") 64 | @Override 65 | public E get() {// 如果是空栈则返回null 66 | if (isEmpty()) { 67 | return null; 68 | } 69 | return (E) this.value[top];// 返回栈顶元素,栈顶元素未改变,top不变 70 | } 71 | 72 | /** 73 | * 返回栈中各元素的字符串表示 74 | */ 75 | @Override 76 | public String toString() { 77 | String str = "("; 78 | for (int i = top; i >= 0; i--) {// 从栈顶开始遍历 79 | if (this.value[i] != null) { 80 | if (i == 0) {// i为0则是栈底元素 81 | str += this.value[i]; 82 | // break;// 遍历完栈顶元素跳出循环 83 | } else { 84 | str += this.value[i] + ","; 85 | } 86 | } 87 | } 88 | return str + ")"; 89 | } 90 | } -------------------------------------------------------------------------------- /linearList-queue-sequence/src/main/java/org/light4j/dataStructure/linearList/queue/sequence/SeqQueue.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.queue.sequence; 2 | 3 | import org.light4j.dataStructure.linearList.queue.QQueue; 4 | 5 | /** 6 | * 顺序队列 7 | * 8 | * @author longjiazuo 9 | * @param 10 | */ 11 | public class SeqQueue implements QQueue { 12 | 13 | private Object[] value;// 存储队列数据元素的数组 14 | private int front;// 对头下标 15 | private int rear;// 队尾下标 16 | 17 | public SeqQueue(int capacity) {// 构造指定容量的空队列 18 | value = new Object[Math.abs(capacity)]; 19 | this.front = -1; 20 | this.rear = -1; 21 | } 22 | 23 | public SeqQueue() {// 构造默认空队列 24 | this(16); 25 | } 26 | 27 | /** 28 | * 判断队列是否为空,若为空返回true 29 | * 30 | * @return 31 | */ 32 | @Override 33 | public boolean isEmpty() { 34 | return this.front == -1 && this.rear == -1; 35 | } 36 | 37 | /** 38 | * 元素入队,操作成功返回true 39 | * 40 | * @param element 41 | * @return 42 | */ 43 | @Override 44 | public boolean enqueue(E element) { 45 | if (element == null) {// 空元素不允许入队 46 | return false; 47 | } 48 | if (isEmpty()) {// 空队列 49 | this.value[0] = element; 50 | this.front++; 51 | this.rear++; 52 | } else {// 非空队列,入队更改队尾结点指向 53 | if (this.rear == this.value.length - 1) {// 队列满则扩容 54 | Object[] temp = this.value; 55 | this.value = new Object[this.value.length * 2];// 扩容为当前队列的两倍 56 | for (int i = 0; i < temp.length; i++) { 57 | this.value[i] = temp[i]; 58 | } 59 | } 60 | this.value[++this.rear] = element;// 61 | } 62 | return true; 63 | } 64 | 65 | /** 66 | * 出队,返回当前对头元素,若队列为空则返回null 67 | * 68 | * @return 69 | */ 70 | @Override 71 | public E dequeue() { 72 | if (isEmpty()) {// 如果队列为空返回null 73 | return null; 74 | } 75 | @SuppressWarnings("unchecked") 76 | E temp = (E) this.value[this.front]; 77 | this.front++; 78 | return temp; 79 | } 80 | 81 | /** 82 | * 返回栈中各元素的字符串表示 83 | */ 84 | @Override 85 | public String toString() { 86 | String str = "("; 87 | if (!isEmpty()) {// 判断是否非空 88 | for (int i = this.front; i <= this.rear; i++) {// 从对头 到队尾 89 | if (i == this.rear) { 90 | str += this.value[i]; 91 | } else { 92 | str += this.value[i] + ","; 93 | } 94 | } 95 | } 96 | return str + ")"; 97 | } 98 | } -------------------------------------------------------------------------------- /linearList-queue-sequence-cycle/src/main/java/org/light4j/dataStructure/linearList/queue/sequence/cycle/SeqCycleQueue.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.queue.sequence.cycle; 2 | 3 | import org.light4j.dataStructure.linearList.queue.QQueue; 4 | 5 | /** 6 | * 顺序循环队列 7 | * 8 | * @author longjiazuo 9 | * @param 10 | */ 11 | public class SeqCycleQueue implements QQueue { 12 | 13 | private Object[] value;// 存储队列数据元素的数组 14 | private int front;// 对头下标 15 | private int rear;// 队尾下标 16 | 17 | public SeqCycleQueue(int capacity) {// 构造指定容量的空循环队列 18 | value = new Object[Math.abs(capacity)]; 19 | this.front = this.rear = 0; 20 | } 21 | 22 | public SeqCycleQueue() {// 构造默认空循环队列 23 | this(16); 24 | } 25 | 26 | /** 27 | * 判断队列是否为空,若为空返回true 28 | * 29 | * @return 30 | */ 31 | @Override 32 | public boolean isEmpty() { 33 | return this.front == this.rear; 34 | } 35 | 36 | /** 37 | * 元素入队,操作成功返回true 38 | * 39 | * @param element 40 | * @return 41 | */ 42 | @Override 43 | public boolean enqueue(E element) { 44 | if (element == null) {// 空元素不允许入队 45 | return false; 46 | } 47 | if (this.front == (this.rear + 1) % this.value.length) { 48 | Object[] temp = this.value; 49 | this.value = new Object[temp.length * 2];// 扩充数组容量,扩充为原来容量的2倍 50 | int i = this.front; 51 | int j = 0; 52 | while (i != this.rear) {// 循环拷贝元素到新的数组 53 | this.value[j] = temp[i];// 元素复制 54 | i = (i + 1) % temp.length; 55 | j++; 56 | } 57 | this.front = 0;// 新队列的front为0 58 | this.rear = j;// 新队列的rear从索引j开始 59 | } 60 | this.value[this.rear] = element; 61 | this.rear = (this.rear + 1) % this.value.length;// rear下标变化规律 62 | return true; 63 | } 64 | 65 | /** 66 | * 出队,返回当前对头元素,若队列为空则返回null 67 | * 68 | * @return 69 | */ 70 | @Override 71 | public E dequeue() { 72 | if (!isEmpty()) {// 队列不为空 73 | @SuppressWarnings("unchecked") 74 | E temp = (E) this.value[this.front];// 取对头元素 75 | this.front = (this.front + 1) % this.value.length;// front下标变化规律 76 | return temp; 77 | } 78 | return null; 79 | } 80 | 81 | /** 82 | * 返回栈中各元素的字符串表示 83 | */ 84 | @Override 85 | public String toString() { 86 | String str = "("; 87 | if (!isEmpty()) {// 判断是否非空 88 | for (int i = this.front; i <= this.rear - 1; i++) {// 从对头 到队尾 89 | if (i == this.rear - 1) { 90 | str += this.value[i]; 91 | } else { 92 | str += this.value[i] + ","; 93 | } 94 | } 95 | } 96 | return str + ")"; 97 | } 98 | } -------------------------------------------------------------------------------- /linearList-linkList-circular/src/main/java/org/light4j/dataStructure/linearList/linkList/circular/CircularSinglyLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.circular; 2 | 3 | import org.light4j.dataStructure.linearList.linkList.Node; 4 | import org.light4j.dataStructure.linearList.linkList.head.HeadSinglyLinkedList; 5 | 6 | /** 7 | *

8 | * 循环单链表类,基本操作和带头结点的单链表类似, 只是在尾添加和尾移除元素的时候需要更改尾结点的next域指向head 9 | *

10 | * 11 | * @author longjiazuo 12 | * 13 | * @param 14 | */ 15 | public class CircularSinglyLinkedList extends HeadSinglyLinkedList { 16 | /** 17 | * 构造空单链表 18 | */ 19 | public CircularSinglyLinkedList() { 20 | this.head = new Node(null); 21 | this.rear = this.head; 22 | this.head.next = this.head; 23 | this.n = 0; 24 | } 25 | 26 | /** 27 | * 在指定位置插入非空的指针对象 28 | */ 29 | @Override 30 | public boolean add(int index, E element) { 31 | if (element == null) {// 不允许插入非空元素 32 | return false; 33 | } 34 | if (index >= this.n) {// 尾插入,插入在最后 35 | this.add(element); 36 | } else { 37 | Node p = this.head; 38 | int i = 0; 39 | while (p.next != null && i < index) { 40 | i++; 41 | p = p.next; 42 | } 43 | // 下面操作可以包含头插入和中间插入 44 | Node q = new Node(element); 45 | q.next = p.next; 46 | p.next = q;// 将q结点插入到p结点之后 47 | this.n++; 48 | return true; 49 | } 50 | return false; 51 | } 52 | 53 | /** 54 | * 在单链表的最后插入元素对象,时间复杂度是O(1) 55 | */ 56 | @Override 57 | public boolean add(E element) { 58 | if (element == null) {// 不允许插入非空元素 59 | return false; 60 | } 61 | this.rear.next = new Node(element);// 尾插入 62 | this.rear = this.rear.next;// 移动尾指针 63 | this.rear.next = this.head;//把尾结点的next域指向head结点 64 | this.n++;// 链表长度增加 65 | return true; 66 | } 67 | 68 | /** 69 | * 移除索引index处的结点,操作成功返回被移除的对象,失败则返回null 70 | */ 71 | @Override 72 | public E remove(int index) { 73 | E old = null; 74 | if (index >= 0) {// 头删除,中间删除,尾删除 75 | Node p = this.head; 76 | int i = 0; 77 | while (p.next != null && i < index) {// 从头结点开始遍历,定位到待删除结点的前驱结点 78 | i++; 79 | p = p.next; 80 | } 81 | if (p.next != null) { 82 | old = p.next.data; 83 | if (p.next == this.rear) {// 如果p结点的后一个结点是尾结点,则移除之后尾结点指针前移 84 | this.rear = p; 85 | this.rear.next = this.head; 86 | } 87 | p.next = p.next.next;// 删除p结点的后继结点 88 | this.n--;// 链表长度减少 89 | return old; 90 | } 91 | } 92 | return old; 93 | } 94 | 95 | @Override 96 | public void clear() { 97 | this.head.next = this.head; 98 | this.n = 0; 99 | } 100 | 101 | @Override 102 | public String toString() {// 返回所有元素值对应的字符串 103 | String str = "("; 104 | Node p = this.head.next; 105 | while (p.data != null) { 106 | str += p.data.toString(); 107 | p = p.next; 108 | if (p != null) { 109 | str += ", "; 110 | } 111 | } 112 | return str + ")"; 113 | } 114 | } -------------------------------------------------------------------------------- /linearList-seqList/src/main/java/org/light4j/dataStructure/linearList/seqList/SeqList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.seqList; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class SeqList implements LList { // 顺序表类,实现线性表接口 6 | private Object[] table; // 对象数组,私有成员 7 | private int n; // 顺序表长度 8 | 9 | /** 10 | * 指定空表的默认容量 11 | */ 12 | public SeqList() { 13 | this(16); 14 | } 15 | 16 | /** 17 | * 构造方法,创建指定容量的空表 Math.abs(i)返回参数的绝对值 18 | */ 19 | public SeqList(int capacity) { 20 | this.table = new Object[Math.abs(capacity)]; 21 | this.n = 0; 22 | } 23 | 24 | /** 25 | * 判断顺序表是否为空,若空返回true 26 | */ 27 | @Override 28 | public boolean isEmpty() { 29 | return this.n == 0; 30 | } 31 | 32 | /** 33 | * 返回顺序表长度 34 | */ 35 | @Override 36 | public int length() { 37 | return this.n; 38 | } 39 | 40 | /** 41 | * 返回index(初始值為0)位置的对象,若序号无效,返回null 42 | */ 43 | @Override 44 | public E get(int index) { 45 | if (index >= 0 && index < this.n) { 46 | return (E) this.table[index]; 47 | } 48 | return null; 49 | } 50 | 51 | /** 52 | * 设置index位置的对象为element,若操作成功,返回原对象,否则返回null 53 | */ 54 | @Override 55 | public E set(int index, E element) { 56 | if (index >= 0 && index < this.n && element != null) { 57 | E old = (E) this.table[index]; 58 | this.table[index] = element; 59 | return old; 60 | } 61 | return null; 62 | } 63 | 64 | /** 65 | * 在index位置插入element对象,若操作成功返回true,不能插入null 66 | */ 67 | @Override 68 | public boolean add(int index, E element) { 69 | if (element == null) { 70 | return false; // 不能插入null 71 | } 72 | if (this.n == this.table.length) { // 若数组满,则需要扩充顺序表的容量 73 | Object[] temp = this.table; 74 | this.table = new Object[temp.length * 2]; // 重新申请一个容量更大的数组 75 | for (int i = 0; i < temp.length; i++) { 76 | this.table[i] = temp[i]; // 复制数组元素,O(n) 77 | } 78 | } 79 | 80 | if (index < 0) { 81 | index = 0; // 下标容错 82 | } 83 | if (index > this.n) { 84 | index = this.n; // 下标容错 85 | } 86 | 87 | for (int j = this.n - 1; j >= index; j--) { // 元素后移,平均移动n/2 88 | this.table[j + 1] = this.table[j]; 89 | } 90 | this.table[index] = element; 91 | this.n++; 92 | return true; 93 | } 94 | 95 | /** 96 | * 在顺序表最好插入element对象 97 | */ 98 | @Override 99 | public boolean add(E element) { 100 | return this.add(this.n, element); 101 | } 102 | 103 | /** 104 | * 移去index位置的对象,若操作成功,则返回被移去对象,否则返回null 105 | */ 106 | @Override 107 | public E remove(int index) { 108 | if (this.n != 0 && index >= 0 && index < this.n) { 109 | E old = (E) this.table[index]; 110 | for (int i = index; i < this.n - 1; i++) { // 元素前移,平均移动n/2 111 | this.table[i] = this.table[i + 1]; 112 | } 113 | this.table[this.n - 1] = null; 114 | this.n--; 115 | return old; // 若操作成功,则返回被移除对象 116 | } 117 | return null; // 未找到删除对象,操作不成功,返回null 118 | } 119 | 120 | /** 121 | * 清空顺序表 122 | */ 123 | @Override 124 | public void clear() { 125 | if (this.n != 0) { 126 | for (int i = 0; i < this.n; i++) { 127 | this.table[i] = null; 128 | } 129 | this.n = 0; 130 | } 131 | } 132 | 133 | /** 134 | * 重写toString()方法 135 | */ 136 | @Override 137 | public String toString() { 138 | String str = "("; 139 | if (this.n != 0) { 140 | for (int i = 0; i < this.n - 1; i++) { 141 | str += this.table[i].toString() + ","; 142 | } 143 | str += this.table[this.n - 1].toString(); 144 | } 145 | return str + ")"; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /linearList-seqList-josephus/src/main/java/org/light4j/dataStructure/linearList/seqList/SeqList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.seqList; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class SeqList implements LList { // 顺序表类,实现线性表接口 6 | private Object[] table; // 对象数组,私有成员 7 | private int n; // 顺序表长度 8 | 9 | /** 10 | * 指定空表的默认容量 11 | */ 12 | public SeqList() { 13 | this(16); 14 | } 15 | 16 | /** 17 | * 构造方法,创建指定容量的空表 Math.abs(i)返回参数的绝对值 18 | */ 19 | public SeqList(int capacity) { 20 | this.table = new Object[Math.abs(capacity)]; 21 | this.n = 0; 22 | } 23 | 24 | /** 25 | * 判断顺序表是否为空,若空返回true 26 | */ 27 | @Override 28 | public boolean isEmpty() { 29 | return this.n == 0; 30 | } 31 | 32 | /** 33 | * 返回顺序表长度 34 | */ 35 | @Override 36 | public int length() { 37 | return this.n; 38 | } 39 | 40 | /** 41 | * 返回index(初始值為0)位置的对象,若序号无效,返回null 42 | */ 43 | @Override 44 | public E get(int index) { 45 | if (index >= 0 && index < this.n) { 46 | return (E) this.table[index]; 47 | } 48 | return null; 49 | } 50 | 51 | /** 52 | * 设置index位置的对象为element,若操作成功,返回原对象,否则返回null 53 | */ 54 | @Override 55 | public E set(int index, E element) { 56 | if (index >= 0 && index < this.n && element != null) { 57 | E old = (E) this.table[index]; 58 | this.table[index] = element; 59 | return old; 60 | } 61 | return null; 62 | } 63 | 64 | /** 65 | * 在index位置插入element对象,若操作成功返回true,不能插入null 66 | */ 67 | @Override 68 | public boolean add(int index, E element) { 69 | if (element == null) { 70 | return false; // 不能插入null 71 | } 72 | if (this.n == this.table.length) { // 若数组满,则需要扩充顺序表的容量 73 | Object[] temp = this.table; 74 | this.table = new Object[temp.length * 2]; // 重新申请一个容量更大的数组 75 | for (int i = 0; i < temp.length; i++) { 76 | this.table[i] = temp[i]; // 复制数组元素,O(n) 77 | } 78 | } 79 | 80 | if (index < 0) { 81 | index = 0; // 下标容错 82 | } 83 | if (index > this.n) { 84 | index = this.n; // 下标容错 85 | } 86 | 87 | for (int j = this.n - 1; j >= index; j--) { // 元素后移,平均移动n/2 88 | this.table[j + 1] = this.table[j]; 89 | } 90 | this.table[index] = element; 91 | this.n++; 92 | return true; 93 | } 94 | 95 | /** 96 | * 在顺序表最好插入element对象 97 | */ 98 | @Override 99 | public boolean add(E element) { 100 | return this.add(this.n, element); 101 | } 102 | 103 | /** 104 | * 移去index位置的对象,若操作成功,则返回被移去对象,否则返回null 105 | */ 106 | @Override 107 | public E remove(int index) { 108 | if (this.n != 0 && index >= 0 && index < this.n) { 109 | E old = (E) this.table[index]; 110 | for (int i = index; i < this.n - 1; i++) { // 元素前移,平均移动n/2 111 | this.table[i] = this.table[i + 1]; 112 | } 113 | this.table[this.n - 1] = null; 114 | this.n--; 115 | return old; // 若操作成功,则返回被移除对象 116 | } 117 | return null; // 未找到删除对象,操作不成功,返回null 118 | } 119 | 120 | /** 121 | * 清空顺序表 122 | */ 123 | @Override 124 | public void clear() { 125 | if (this.n != 0) { 126 | for (int i = 0; i < this.n; i++) { 127 | this.table[i] = null; 128 | } 129 | this.n = 0; 130 | } 131 | } 132 | 133 | /** 134 | * 重写toString()方法 135 | */ 136 | @Override 137 | public String toString() { 138 | String str = "("; 139 | if (this.n != 0) { 140 | for (int i = 0; i < this.n - 1; i++) { 141 | str += this.table[i].toString() + ","; 142 | } 143 | str += this.table[this.n - 1].toString(); 144 | } 145 | return str + ")"; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /linearList-linkList/src/main/java/org/light4j/dataStructure/linearList/linkList/SinglyLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class SinglyLinkedList implements LList { 6 | protected Node head;// 单链表头结点,指向单链表第一个结点 7 | 8 | /** 9 | * 构造空单链表 10 | */ 11 | public SinglyLinkedList() { 12 | this.head = null; 13 | } 14 | 15 | /** 16 | * 构造指定头结点的单链表 17 | * 18 | * @param head 19 | */ 20 | public SinglyLinkedList(Node head) { 21 | this.head = head; 22 | } 23 | 24 | /** 25 | * 判断单链表是否为空 26 | */ 27 | @Override 28 | public boolean isEmpty() { 29 | return this.head == null; 30 | } 31 | 32 | /** 33 | * 遍历单链表返回单链表长度 34 | */ 35 | @Override 36 | public int length() { 37 | Node p = this.head;// p从head指向的节点开始 38 | int i = 0; 39 | while (p != null) {// 若单链表未结束 40 | i++; 41 | p = p.next;// p到达后继节点 42 | } 43 | return i; 44 | } 45 | 46 | /** 47 | * 返回序号为index的的对象,如果链表为空或者序号错误则返回null 48 | */ 49 | @Override 50 | public E get(int index) { 51 | Node p = this.head; 52 | int i = 0; 53 | while (p != null && i < index) { 54 | i++; 55 | p = p.next; 56 | } 57 | if (p != null) { 58 | return p.data; 59 | } 60 | return null; 61 | } 62 | 63 | /** 64 | * 设置序号为index的对象的值为element,如果操作成功则返回原对象,操作失败返回null 65 | */ 66 | @Override 67 | public E set(int index, E element) { 68 | if (this.head != null && index >= 0 && element != null) { 69 | Node p = this.head; 70 | int i = 0; 71 | while (p != null && i < index) { 72 | i++; 73 | p = p.next; 74 | } 75 | if (p != null) { 76 | E old = p.data; 77 | p.data = element; 78 | return old;// 操作成功返回原对象 79 | } 80 | } 81 | return null;// 操作失败则返回null 82 | } 83 | 84 | /** 85 | * 插入elment元素,插入后对象序号为index,如果操作成功则返回true 86 | */ 87 | @Override 88 | public boolean add(int index, E element) { 89 | if (element == null) { 90 | return false; 91 | } 92 | Node q = new Node(element);// 创建要插入的结点 93 | if (this.head == null || index <= 0) {// 在头结点后面插入 94 | q.next = this.head; 95 | this.head = q; 96 | } else {// 中间或者尾结点后面插入 97 | Node p = this.head; 98 | int i = 0; 99 | while (p.next != null && i < index - 1) {// 寻找插入位置 100 | i++; 101 | p = p.next; 102 | } 103 | q.next = p.next;// q插入在p结点之后 104 | p.next = q; 105 | } 106 | 107 | return true; 108 | } 109 | 110 | /** 111 | * 在单链表最后插入对象 112 | */ 113 | @Override 114 | public boolean add(E element) { 115 | return add(Integer.MAX_VALUE, element); 116 | } 117 | 118 | /** 119 | * 移除序号为index的对象,如果操作成功则返回被移除的对象,操作失败则返回null 120 | */ 121 | @Override 122 | public E remove(int index) { 123 | E old = null; 124 | if (this.head != null && index >= 0) { 125 | if (index == 0) {// 头删除 126 | old = this.head.data; 127 | this.head = this.head.next; 128 | return old; 129 | } else {// 中间删除或者尾删除 130 | Node p = this.head; 131 | int i = 0; 132 | while (p != null && i < index - 1) {// 定位到待删除节点的前驱节点 133 | i++; 134 | p = p.next; 135 | } 136 | if (p != null && p.next != null) { 137 | old = p.next.data;// 操作成功返回被移去对象 138 | p.next = (p.next).next;// 删除p的后继节点 139 | } 140 | } 141 | } 142 | return old; 143 | } 144 | 145 | @Override 146 | public void clear() {// 清空单链表 147 | this.head = null; 148 | } 149 | 150 | @Override 151 | public String toString() {// 返回所有元素值对应的字符串 152 | String str = "("; 153 | Node p = this.head; 154 | while (p != null) { 155 | str += p.data.toString(); 156 | p = p.next; 157 | if (p != null) { 158 | str += ", "; 159 | } 160 | } 161 | return str + ")"; 162 | } 163 | } -------------------------------------------------------------------------------- /linearList-linkList-reverse/src/main/java/org/light4j/dataStructure/linearList/linkList/SinglyLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class SinglyLinkedList implements LList { 6 | protected Node head;// 单链表头结点,指向单链表第一个结点 7 | 8 | /** 9 | * 构造空单链表 10 | */ 11 | public SinglyLinkedList() { 12 | this.head = null; 13 | } 14 | 15 | /** 16 | * 构造指定头结点的单链表 17 | * 18 | * @param head 19 | */ 20 | public SinglyLinkedList(Node head) { 21 | this.head = head; 22 | } 23 | 24 | /** 25 | * 判断单链表是否为空 26 | */ 27 | @Override 28 | public boolean isEmpty() { 29 | return this.head == null; 30 | } 31 | 32 | /** 33 | * 遍历单链表返回单链表长度 34 | */ 35 | @Override 36 | public int length() { 37 | Node p = this.head;// p从head指向的节点开始 38 | int i = 0; 39 | while (p != null) {// 若单链表未结束 40 | i++; 41 | p = p.next;// p到达后继节点 42 | } 43 | return i; 44 | } 45 | 46 | /** 47 | * 返回序号为index的的对象,如果链表为空或者序号错误则返回null 48 | */ 49 | @Override 50 | public E get(int index) { 51 | Node p = this.head; 52 | int i = 0; 53 | while (p != null && i < index) { 54 | i++; 55 | p = p.next; 56 | } 57 | if (p != null) { 58 | return p.data; 59 | } 60 | return null; 61 | } 62 | 63 | /** 64 | * 设置序号为index的对象的值为element,如果操作成功则返回原对象,操作失败返回null 65 | */ 66 | @Override 67 | public E set(int index, E element) { 68 | if (this.head != null && index >= 0 && element != null) { 69 | Node p = this.head; 70 | int i = 0; 71 | while (p != null && i < index) { 72 | i++; 73 | p = p.next; 74 | } 75 | if (p != null) { 76 | E old = p.data; 77 | p.data = element; 78 | return old;// 操作成功返回原对象 79 | } 80 | } 81 | return null;// 操作失败则返回null 82 | } 83 | 84 | /** 85 | * 插入elment元素,插入后对象序号为index,如果操作成功则返回true 86 | */ 87 | @Override 88 | public boolean add(int index, E element) { 89 | if (element == null) { 90 | return false; 91 | } 92 | Node q = new Node(element);// 创建要插入的结点 93 | if (this.head == null || index <= 0) {// 在头结点后面插入 94 | q.next = this.head; 95 | this.head = q; 96 | } else {// 中间或者尾结点后面插入 97 | Node p = this.head; 98 | int i = 0; 99 | while (p.next != null && i < index - 1) {// 寻找插入位置 100 | i++; 101 | p = p.next; 102 | } 103 | q.next = p.next;// q插入在p结点之后 104 | p.next = q; 105 | } 106 | 107 | return true; 108 | } 109 | 110 | /** 111 | * 在单链表最后插入对象 112 | */ 113 | @Override 114 | public boolean add(E element) { 115 | return add(Integer.MAX_VALUE, element); 116 | } 117 | 118 | /** 119 | * 移除序号为index的对象,如果操作成功则返回被移除的对象,操作失败则返回null 120 | */ 121 | @Override 122 | public E remove(int index) { 123 | E old = null; 124 | if (this.head != null && index >= 0) { 125 | if (index == 0) {// 头删除 126 | old = this.head.data; 127 | this.head = this.head.next; 128 | return old; 129 | } else {// 中间删除或者尾删除 130 | Node p = this.head; 131 | int i = 0; 132 | while (p != null && i < index - 1) {// 定位到待删除节点的前驱节点 133 | i++; 134 | p = p.next; 135 | } 136 | if (p != null && p.next != null) { 137 | old = p.next.data;// 操作成功返回被移去对象 138 | p.next = (p.next).next;// 删除p的后继节点 139 | } 140 | } 141 | } 142 | return old; 143 | } 144 | 145 | @Override 146 | public void clear() {// 清空单链表 147 | this.head = null; 148 | } 149 | 150 | @Override 151 | public String toString() {// 返回所有元素值对应的字符串 152 | String str = "("; 153 | Node p = this.head; 154 | while (p != null) { 155 | str += p.data.toString(); 156 | p = p.next; 157 | if (p != null) { 158 | str += ", "; 159 | } 160 | } 161 | return str + ")"; 162 | } 163 | } -------------------------------------------------------------------------------- /linearList-linkList-circular/src/main/java/org/light4j/dataStructure/linearList/linkList/SinglyLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class SinglyLinkedList implements LList { 6 | protected Node head;// 单链表头结点,指向单链表第一个结点 7 | 8 | /** 9 | * 构造空单链表 10 | */ 11 | public SinglyLinkedList() { 12 | this.head = null; 13 | } 14 | 15 | /** 16 | * 构造指定头结点的单链表 17 | * 18 | * @param head 19 | */ 20 | public SinglyLinkedList(Node head) { 21 | this.head = head; 22 | } 23 | 24 | /** 25 | * 判断单链表是否为空 26 | */ 27 | @Override 28 | public boolean isEmpty() { 29 | return this.head == null; 30 | } 31 | 32 | /** 33 | * 遍历单链表返回单链表长度 34 | */ 35 | @Override 36 | public int length() { 37 | Node p = this.head;// p从head指向的节点开始 38 | int i = 0; 39 | while (p != null) {// 若单链表未结束 40 | i++; 41 | p = p.next;// p到达后继节点 42 | } 43 | return i; 44 | } 45 | 46 | /** 47 | * 返回序号为index的的对象,如果链表为空或者序号错误则返回null 48 | */ 49 | @Override 50 | public E get(int index) { 51 | Node p = this.head; 52 | int i = 0; 53 | while (p != null && i < index) { 54 | i++; 55 | p = p.next; 56 | } 57 | if (p != null) { 58 | return p.data; 59 | } 60 | return null; 61 | } 62 | 63 | /** 64 | * 设置序号为index的对象的值为element,如果操作成功则返回原对象,操作失败返回null 65 | */ 66 | @Override 67 | public E set(int index, E element) { 68 | if (this.head != null && index >= 0 && element != null) { 69 | Node p = this.head; 70 | int i = 0; 71 | while (p != null && i < index) { 72 | i++; 73 | p = p.next; 74 | } 75 | if (p != null) { 76 | E old = p.data; 77 | p.data = element; 78 | return old;// 操作成功返回原对象 79 | } 80 | } 81 | return null;// 操作失败则返回null 82 | } 83 | 84 | /** 85 | * 插入elment元素,插入后对象序号为index,如果操作成功则返回true 86 | */ 87 | @Override 88 | public boolean add(int index, E element) { 89 | if (element == null) { 90 | return false; 91 | } 92 | Node q = new Node(element);// 创建要插入的结点 93 | if (this.head == null || index <= 0) {// 在头结点后面插入 94 | q.next = this.head; 95 | this.head = q; 96 | } else {// 中间或者尾结点后面插入 97 | Node p = this.head; 98 | int i = 0; 99 | while (p.next != null && i < index - 1) {// 寻找插入位置 100 | i++; 101 | p = p.next; 102 | } 103 | q.next = p.next;// q插入在p结点之后 104 | p.next = q; 105 | } 106 | 107 | return true; 108 | } 109 | 110 | /** 111 | * 在单链表最后插入对象 112 | */ 113 | @Override 114 | public boolean add(E element) { 115 | return add(Integer.MAX_VALUE, element); 116 | } 117 | 118 | /** 119 | * 移除序号为index的对象,如果操作成功则返回被移除的对象,操作失败则返回null 120 | */ 121 | @Override 122 | public E remove(int index) { 123 | E old = null; 124 | if (this.head != null && index >= 0) { 125 | if (index == 0) {// 头删除 126 | old = this.head.data; 127 | this.head = this.head.next; 128 | return old; 129 | } else {// 中间删除或者尾删除 130 | Node p = this.head; 131 | int i = 0; 132 | while (p != null && i < index - 1) {// 定位到待删除节点的前驱节点 133 | i++; 134 | p = p.next; 135 | } 136 | if (p != null && p.next != null) { 137 | old = p.next.data;// 操作成功返回被移去对象 138 | p.next = (p.next).next;// 删除p的后继节点 139 | } 140 | } 141 | } 142 | return old; 143 | } 144 | 145 | @Override 146 | public void clear() {// 清空单链表 147 | this.head = null; 148 | } 149 | 150 | @Override 151 | public String toString() {// 返回所有元素值对应的字符串 152 | String str = "("; 153 | Node p = this.head; 154 | while (p != null) { 155 | str += p.data.toString(); 156 | p = p.next; 157 | if (p != null) { 158 | str += ", "; 159 | } 160 | } 161 | return str + ")"; 162 | } 163 | } -------------------------------------------------------------------------------- /linearList-linkList-increase/src/main/java/org/light4j/dataStructure/linearList/linkList/SinglyLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class SinglyLinkedList implements LList { 6 | protected Node head;// 单链表头结点,指向单链表第一个结点 7 | 8 | /** 9 | * 构造空单链表 10 | */ 11 | public SinglyLinkedList() { 12 | this.head = null; 13 | } 14 | 15 | /** 16 | * 构造指定头结点的单链表 17 | * 18 | * @param head 19 | */ 20 | public SinglyLinkedList(Node head) { 21 | this.head = head; 22 | } 23 | 24 | /** 25 | * 判断单链表是否为空 26 | */ 27 | @Override 28 | public boolean isEmpty() { 29 | return this.head == null; 30 | } 31 | 32 | /** 33 | * 遍历单链表返回单链表长度 34 | */ 35 | @Override 36 | public int length() { 37 | Node p = this.head;// p从head指向的节点开始 38 | int i = 0; 39 | while (p != null) {// 若单链表未结束 40 | i++; 41 | p = p.next;// p到达后继节点 42 | } 43 | return i; 44 | } 45 | 46 | /** 47 | * 返回序号为index的的对象,如果链表为空或者序号错误则返回null 48 | */ 49 | @Override 50 | public E get(int index) { 51 | Node p = this.head; 52 | int i = 0; 53 | while (p != null && i < index) { 54 | i++; 55 | p = p.next; 56 | } 57 | if (p != null) { 58 | return p.data; 59 | } 60 | return null; 61 | } 62 | 63 | /** 64 | * 设置序号为index的对象的值为element,如果操作成功则返回原对象,操作失败返回null 65 | */ 66 | @Override 67 | public E set(int index, E element) { 68 | if (this.head != null && index >= 0 && element != null) { 69 | Node p = this.head; 70 | int i = 0; 71 | while (p != null && i < index) { 72 | i++; 73 | p = p.next; 74 | } 75 | if (p != null) { 76 | E old = p.data; 77 | p.data = element; 78 | return old;// 操作成功返回原对象 79 | } 80 | } 81 | return null;// 操作失败则返回null 82 | } 83 | 84 | /** 85 | * 插入elment元素,插入后对象序号为index,如果操作成功则返回true 86 | */ 87 | @Override 88 | public boolean add(int index, E element) { 89 | if (element == null) { 90 | return false; 91 | } 92 | Node q = new Node(element);// 创建要插入的结点 93 | if (this.head == null || index <= 0) {// 在头结点后面插入 94 | q.next = this.head; 95 | this.head = q; 96 | } else {// 中间或者尾结点后面插入 97 | Node p = this.head; 98 | int i = 0; 99 | while (p.next != null && i < index - 1) {// 寻找插入位置 100 | i++; 101 | p = p.next; 102 | } 103 | q.next = p.next;// q插入在p结点之后 104 | p.next = q; 105 | } 106 | 107 | return true; 108 | } 109 | 110 | /** 111 | * 在单链表最后插入对象 112 | */ 113 | @Override 114 | public boolean add(E element) { 115 | return add(Integer.MAX_VALUE, element); 116 | } 117 | 118 | /** 119 | * 移除序号为index的对象,如果操作成功则返回被移除的对象,操作失败则返回null 120 | */ 121 | @Override 122 | public E remove(int index) { 123 | E old = null; 124 | if (this.head != null && index >= 0) { 125 | if (index == 0) {// 头删除 126 | old = this.head.data; 127 | this.head = this.head.next; 128 | return old; 129 | } else {// 中间删除或者尾删除 130 | Node p = this.head; 131 | int i = 0; 132 | while (p != null && i < index - 1) {// 定位到待删除节点的前驱节点 133 | i++; 134 | p = p.next; 135 | } 136 | if (p != null && p.next != null) { 137 | old = p.next.data;// 操作成功返回被移去对象 138 | p.next = (p.next).next;// 删除p的后继节点 139 | } 140 | } 141 | } 142 | return old; 143 | } 144 | 145 | @Override 146 | public void clear() {// 清空单链表 147 | this.head = null; 148 | } 149 | 150 | @Override 151 | public String toString() {// 返回所有元素值对应的字符串 152 | String str = "("; 153 | Node p = this.head; 154 | while (p != null) { 155 | str += p.data.toString(); 156 | p = p.next; 157 | if (p != null) { 158 | str += ", "; 159 | } 160 | } 161 | return str + ")"; 162 | } 163 | } -------------------------------------------------------------------------------- /linearList-linkList-josephus/src/main/java/org/light4j/dataStructure/linearList/linkList/SinglyLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | public class SinglyLinkedList implements LList { 6 | protected Node head;// 单链表头结点,指向单链表第一个结点 7 | 8 | /** 9 | * 构造空单链表 10 | */ 11 | public SinglyLinkedList() { 12 | this.head = null; 13 | } 14 | 15 | /** 16 | * 构造指定头结点的单链表 17 | * 18 | * @param head 19 | */ 20 | public SinglyLinkedList(Node head) { 21 | this.head = head; 22 | } 23 | 24 | /** 25 | * 判断单链表是否为空 26 | */ 27 | @Override 28 | public boolean isEmpty() { 29 | return this.head == null; 30 | } 31 | 32 | /** 33 | * 遍历单链表返回单链表长度 34 | */ 35 | @Override 36 | public int length() { 37 | Node p = this.head;// p从head指向的节点开始 38 | int i = 0; 39 | while (p != null) {// 若单链表未结束 40 | i++; 41 | p = p.next;// p到达后继节点 42 | } 43 | return i; 44 | } 45 | 46 | /** 47 | * 返回序号为index的的对象,如果链表为空或者序号错误则返回null 48 | */ 49 | @Override 50 | public E get(int index) { 51 | Node p = this.head; 52 | int i = 0; 53 | while (p != null && i < index) { 54 | i++; 55 | p = p.next; 56 | } 57 | if (p != null) { 58 | return p.data; 59 | } 60 | return null; 61 | } 62 | 63 | /** 64 | * 设置序号为index的对象的值为element,如果操作成功则返回原对象,操作失败返回null 65 | */ 66 | @Override 67 | public E set(int index, E element) { 68 | if (this.head != null && index >= 0 && element != null) { 69 | Node p = this.head; 70 | int i = 0; 71 | while (p != null && i < index) { 72 | i++; 73 | p = p.next; 74 | } 75 | if (p != null) { 76 | E old = p.data; 77 | p.data = element; 78 | return old;// 操作成功返回原对象 79 | } 80 | } 81 | return null;// 操作失败则返回null 82 | } 83 | 84 | /** 85 | * 插入elment元素,插入后对象序号为index,如果操作成功则返回true 86 | */ 87 | @Override 88 | public boolean add(int index, E element) { 89 | if (element == null) { 90 | return false; 91 | } 92 | Node q = new Node(element);// 创建要插入的结点 93 | if (this.head == null || index <= 0) {// 在头结点后面插入 94 | q.next = this.head; 95 | this.head = q; 96 | } else {// 中间或者尾结点后面插入 97 | Node p = this.head; 98 | int i = 0; 99 | while (p.next != null && i < index - 1) {// 寻找插入位置 100 | i++; 101 | p = p.next; 102 | } 103 | q.next = p.next;// q插入在p结点之后 104 | p.next = q; 105 | } 106 | 107 | return true; 108 | } 109 | 110 | /** 111 | * 在单链表最后插入对象 112 | */ 113 | @Override 114 | public boolean add(E element) { 115 | return add(Integer.MAX_VALUE, element); 116 | } 117 | 118 | /** 119 | * 移除序号为index的对象,如果操作成功则返回被移除的对象,操作失败则返回null 120 | */ 121 | @Override 122 | public E remove(int index) { 123 | E old = null; 124 | if (this.head != null && index >= 0) { 125 | if (index == 0) {// 头删除 126 | old = this.head.data; 127 | this.head = this.head.next; 128 | return old; 129 | } else {// 中间删除或者尾删除 130 | Node p = this.head; 131 | int i = 0; 132 | while (p != null && i < index - 1) {// 定位到待删除节点的前驱节点 133 | i++; 134 | p = p.next; 135 | } 136 | if (p != null && p.next != null) { 137 | old = p.next.data;// 操作成功返回被移去对象 138 | p.next = (p.next).next;// 删除p的后继节点 139 | } 140 | } 141 | } 142 | return old; 143 | } 144 | 145 | @Override 146 | public void clear() {// 清空单链表 147 | this.head = null; 148 | } 149 | 150 | @Override 151 | public String toString() {// 返回所有元素值对应的字符串 152 | String str = "("; 153 | Node p = this.head; 154 | while (p != null) { 155 | str += p.data.toString(); 156 | p = p.next; 157 | if (p != null) { 158 | str += ", "; 159 | } 160 | } 161 | return str + ")"; 162 | } 163 | } -------------------------------------------------------------------------------- /linearList-linkList-circular/src/main/java/org/light4j/dataStructure/linearList/linkList/head/HeadSinglyLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.head; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | import org.light4j.dataStructure.linearList.linkList.Node; 5 | /** 6 | * 带头结点的单链表类 7 | * 8 | * @author longjiazuo 9 | */ 10 | public class HeadSinglyLinkedList implements LList { 11 | protected Node head;// 单链表的头结点,指向单链表的头结点 12 | protected Node rear;// 单链表的尾结点,指向单链表的最后一个结点 13 | protected int n;// 单链表的长度 14 | 15 | public HeadSinglyLinkedList() {// 构造空单链表 16 | this.head = new Node(null);// 构造头结点,元素值为空 17 | this.rear = this.head;// 构造尾结点,初始化的时候头结点和尾结点都指向头结点 18 | this.n = 0;// 初始化链表长度为0 19 | } 20 | 21 | /** 22 | * 判断带头结点的单链表是否为空 23 | */ 24 | @Override 25 | public boolean isEmpty() { 26 | return this.head.next == null; 27 | } 28 | 29 | /** 30 | * 返回带头结点的单链表长度,时间复杂度为O(1) 31 | */ 32 | @Override 33 | public int length() { 34 | return this.n; 35 | } 36 | 37 | /** 38 | * 返回序号为index的的对象,如果链表为空或者序号错误则返回null 39 | */ 40 | @Override 41 | public E get(int index) { 42 | Node p = this.head; 43 | int i = 0; 44 | while (p != null && i < index) { 45 | i++; 46 | p = p.next; 47 | } 48 | if (p != null) { 49 | return p.data; 50 | } 51 | return null; 52 | } 53 | 54 | /** 55 | * 设置序号为index的对象的值为element,如果操作成功则返回原对象,操作失败返回null 56 | */ 57 | @Override 58 | public E set(int index, E element) { 59 | if (this.head != null && index >= 0 && element != null) { 60 | Node p = this.head; 61 | int i = 0; 62 | while (p != null && i < index) { 63 | i++; 64 | p = p.next; 65 | } 66 | if (p != null) { 67 | E old = p.data; 68 | p.data = element; 69 | return old;// 操作成功返回原对象 70 | } 71 | } 72 | return null;// 操作失败则返回null 73 | } 74 | 75 | /** 76 | * 在指定位置插入非空的指针对象 77 | */ 78 | @Override 79 | public boolean add(int index, E element) { 80 | if (element == null) {// 不允许插入非空元素 81 | return false; 82 | } 83 | if (index >= this.n) {// 尾插入,插入在最后 84 | this.add(element); 85 | } else { 86 | Node p = this.head; 87 | int i = 0; 88 | while (p.next != null && i < index) { 89 | i++; 90 | p = p.next; 91 | } 92 | // 下面操作可以包含头插入和中间插入 93 | Node q = new Node(element); 94 | q.next = p.next; 95 | p.next = q;// 将q结点插入到p结点之后 96 | this.n++; 97 | return true; 98 | } 99 | return false; 100 | } 101 | 102 | /** 103 | * 在单链表的最后插入元素对象,时间复杂度是O(1) 104 | */ 105 | @Override 106 | public boolean add(E element) { 107 | if (element == null) {// 不允许插入非空元素 108 | return false; 109 | } 110 | this.rear.next = new Node(element);// 尾插入 111 | this.rear = this.rear.next;// 移动尾指针 112 | this.n++;// 链表长度增加 113 | return true; 114 | } 115 | 116 | /** 117 | * 移除索引index处的结点,操作成功返回被移除的对象,失败则返回null 118 | */ 119 | @Override 120 | public E remove(int index) { 121 | E old = null; 122 | if (index >= 0) {// 头删除,中间删除,尾删除 123 | Node p = this.head; 124 | int i = 0; 125 | while (p.next != null && i < index) {// 从头结点开始遍历,定位到待删除结点的前驱结点 126 | i++; 127 | p = p.next; 128 | } 129 | if (p.next != null) { 130 | old = p.next.data; 131 | if (p.next == this.rear) {// 如果p结点的后一个结点是尾结点,则移除之后尾结点指针前移 132 | this.rear = p; 133 | } 134 | p.next = p.next.next;// 删除p结点的后继结点 135 | this.n--;// 链表长度减少 136 | return old; 137 | } 138 | } 139 | return old; 140 | } 141 | 142 | /** 143 | * 清空单链表 144 | */ 145 | @Override 146 | public void clear() { 147 | this.head.next = null; 148 | this.rear = this.head; 149 | this.n = 0; 150 | } 151 | 152 | @Override 153 | public String toString() {// 返回所有元素值对应的字符串 154 | String str = "("; 155 | Node p = this.head.next; 156 | while (p != null) { 157 | str += p.data.toString(); 158 | p = p.next; 159 | if (p != null) { 160 | str += ", "; 161 | } 162 | } 163 | return str + ")"; 164 | } 165 | } -------------------------------------------------------------------------------- /linearList-linkList-head/src/main/java/org/light4j/dataStructure/linearList/linkList/head/HeadSinglyLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.head; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | import org.light4j.dataStructure.linearList.linkList.Node; 5 | /** 6 | * 带头结点的单链表类 7 | * 8 | * @author longjiazuo 9 | */ 10 | public class HeadSinglyLinkedList implements LList { 11 | protected Node head;// 单链表的头结点,指向单链表的头结点 12 | protected Node rear;// 单链表的尾结点,指向单链表的最后一个结点 13 | protected int n;// 单链表的长度 14 | 15 | public HeadSinglyLinkedList() {// 构造空单链表 16 | this.head = new Node(null);// 构造头结点,元素值为空 17 | this.rear = this.head;// 构造尾结点,初始化的时候头结点和尾结点都指向头结点 18 | this.n = 0;// 初始化链表长度为0 19 | } 20 | 21 | /** 22 | * 判断带头结点的单链表是否为空 23 | */ 24 | @Override 25 | public boolean isEmpty() { 26 | return this.head.next == null; 27 | } 28 | 29 | /** 30 | * 返回带头结点的单链表长度,时间复杂度为O(1) 31 | */ 32 | @Override 33 | public int length() { 34 | return this.n; 35 | } 36 | 37 | /** 38 | * 返回序号为index的的对象,如果链表为空或者序号错误则返回null 39 | */ 40 | @Override 41 | public E get(int index) { 42 | Node p = this.head; 43 | int i = 0; 44 | while (p != null && i < index) { 45 | i++; 46 | p = p.next; 47 | } 48 | if (p != null) { 49 | return p.data; 50 | } 51 | return null; 52 | } 53 | 54 | /** 55 | * 设置序号为index的对象的值为element,如果操作成功则返回原对象,操作失败返回null 56 | */ 57 | @Override 58 | public E set(int index, E element) { 59 | if (this.head != null && index >= 0 && element != null) { 60 | Node p = this.head; 61 | int i = 0; 62 | while (p != null && i < index) { 63 | i++; 64 | p = p.next; 65 | } 66 | if (p != null) { 67 | E old = p.data; 68 | p.data = element; 69 | return old;// 操作成功返回原对象 70 | } 71 | } 72 | return null;// 操作失败则返回null 73 | } 74 | 75 | /** 76 | * 在指定位置插入非空的指针对象 77 | */ 78 | @Override 79 | public boolean add(int index, E element) { 80 | if (element == null) {// 不允许插入非空元素 81 | return false; 82 | } 83 | if (index >= this.n) {// 尾插入,插入在最后 84 | this.add(element); 85 | } else { 86 | Node p = this.head; 87 | int i = 0; 88 | while (p.next != null && i < index) { 89 | i++; 90 | p = p.next; 91 | } 92 | // 下面操作可以包含头插入和中间插入 93 | Node q = new Node(element); 94 | q.next = p.next; 95 | p.next = q;// 将q结点插入到p结点之后 96 | this.n++; 97 | return true; 98 | } 99 | return false; 100 | } 101 | 102 | /** 103 | * 在单链表的最后插入元素对象,时间复杂度是O(1) 104 | */ 105 | @Override 106 | public boolean add(E element) { 107 | if (element == null) {// 不允许插入非空元素 108 | return false; 109 | } 110 | this.rear.next = new Node(element);// 尾插入 111 | this.rear = this.rear.next;// 移动尾指针 112 | this.n++;// 链表长度增加 113 | return true; 114 | } 115 | 116 | /** 117 | * 移除索引index处的结点,操作成功返回被移除的对象,失败则返回null 118 | */ 119 | @Override 120 | public E remove(int index) { 121 | E old = null; 122 | if (index >= 0) {// 头删除,中间删除,尾删除 123 | Node p = this.head; 124 | int i = 0; 125 | while (p.next != null && i < index) {// 从头结点开始遍历,定位到待删除结点的前驱结点 126 | i++; 127 | p = p.next; 128 | } 129 | if (p.next != null) { 130 | old = p.next.data; 131 | if (p.next == this.rear) {// 如果p结点的后一个结点是尾结点,则移除之后尾结点指针前移 132 | this.rear = p; 133 | } 134 | p.next = p.next.next;// 删除p结点的后继结点 135 | this.n--;// 链表长度减少 136 | return old; 137 | } 138 | } 139 | return old; 140 | } 141 | 142 | /** 143 | * 清空单链表 144 | */ 145 | @Override 146 | public void clear() { 147 | this.head.next = null; 148 | this.rear = this.head; 149 | this.n = 0; 150 | } 151 | 152 | @Override 153 | public String toString() {// 返回所有元素值对应的字符串 154 | String str = "("; 155 | Node p = this.head.next; 156 | while (p != null) { 157 | str += p.data.toString(); 158 | p = p.next; 159 | if (p != null) { 160 | str += ", "; 161 | } 162 | } 163 | return str + ")"; 164 | } 165 | } -------------------------------------------------------------------------------- /linearList-linkList-head-sorted/src/main/java/org/light4j/dataStructure/linearList/linkList/head/HeadSinglyLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.head; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | import org.light4j.dataStructure.linearList.linkList.Node; 5 | /** 6 | * 带头结点的单链表类 7 | * 8 | * @author longjiazuo 9 | */ 10 | public class HeadSinglyLinkedList implements LList { 11 | protected Node head;// 单链表的头结点,指向单链表的头结点 12 | protected Node rear;// 单链表的尾结点,指向单链表的最后一个结点 13 | protected int n;// 单链表的长度 14 | 15 | public HeadSinglyLinkedList() {// 构造空单链表 16 | this.head = new Node(null);// 构造头结点,元素值为空 17 | this.rear = this.head;// 构造尾结点,初始化的时候头结点和尾结点都指向头结点 18 | this.n = 0;// 初始化链表长度为0 19 | } 20 | 21 | /** 22 | * 判断带头结点的单链表是否为空 23 | */ 24 | @Override 25 | public boolean isEmpty() { 26 | return this.head.next == null; 27 | } 28 | 29 | /** 30 | * 返回带头结点的单链表长度,时间复杂度为O(1) 31 | */ 32 | @Override 33 | public int length() { 34 | return this.n; 35 | } 36 | 37 | /** 38 | * 返回序号为index的的对象,如果链表为空或者序号错误则返回null 39 | */ 40 | @Override 41 | public E get(int index) { 42 | Node p = this.head; 43 | int i = 0; 44 | while (p != null && i < index) { 45 | i++; 46 | p = p.next; 47 | } 48 | if (p != null) { 49 | return p.data; 50 | } 51 | return null; 52 | } 53 | 54 | /** 55 | * 设置序号为index的对象的值为element,如果操作成功则返回原对象,操作失败返回null 56 | */ 57 | @Override 58 | public E set(int index, E element) { 59 | if (this.head != null && index >= 0 && element != null) { 60 | Node p = this.head; 61 | int i = 0; 62 | while (p != null && i < index) { 63 | i++; 64 | p = p.next; 65 | } 66 | if (p != null) { 67 | E old = p.data; 68 | p.data = element; 69 | return old;// 操作成功返回原对象 70 | } 71 | } 72 | return null;// 操作失败则返回null 73 | } 74 | 75 | /** 76 | * 在指定位置插入非空的指针对象 77 | */ 78 | @Override 79 | public boolean add(int index, E element) { 80 | if (element == null) {// 不允许插入非空元素 81 | return false; 82 | } 83 | if (index >= this.n) {// 尾插入,插入在最后 84 | this.add(element); 85 | } else { 86 | Node p = this.head; 87 | int i = 0; 88 | while (p.next != null && i < index) { 89 | i++; 90 | p = p.next; 91 | } 92 | // 下面操作可以包含头插入和中间插入 93 | Node q = new Node(element); 94 | q.next = p.next; 95 | p.next = q;// 将q结点插入到p结点之后 96 | this.n++; 97 | return true; 98 | } 99 | return false; 100 | } 101 | 102 | /** 103 | * 在单链表的最后插入元素对象,时间复杂度是O(1) 104 | */ 105 | @Override 106 | public boolean add(E element) { 107 | if (element == null) {// 不允许插入非空元素 108 | return false; 109 | } 110 | this.rear.next = new Node(element);// 尾插入 111 | this.rear = this.rear.next;// 移动尾指针 112 | this.n++;// 链表长度增加 113 | return true; 114 | } 115 | 116 | /** 117 | * 移除索引index处的结点,操作成功返回被移除的对象,失败则返回null 118 | */ 119 | @Override 120 | public E remove(int index) { 121 | E old = null; 122 | if (index >= 0) {// 头删除,中间删除,尾删除 123 | Node p = this.head; 124 | int i = 0; 125 | while (p.next != null && i < index) {// 从头结点开始遍历,定位到待删除结点的前驱结点 126 | i++; 127 | p = p.next; 128 | } 129 | if (p.next != null) { 130 | old = p.next.data; 131 | if (p.next == this.rear) {// 如果p结点的后一个结点是尾结点,则移除之后尾结点指针前移 132 | this.rear = p; 133 | } 134 | p.next = p.next.next;// 删除p结点的后继结点 135 | this.n--;// 链表长度减少 136 | return old; 137 | } 138 | } 139 | return old; 140 | } 141 | 142 | /** 143 | * 清空单链表 144 | */ 145 | @Override 146 | public void clear() { 147 | this.head.next = null; 148 | this.rear = this.head; 149 | this.n = 0; 150 | } 151 | 152 | @Override 153 | public String toString() {// 返回所有元素值对应的字符串 154 | String str = "("; 155 | Node p = this.head.next; 156 | while (p != null) { 157 | str += p.data.toString(); 158 | p = p.next; 159 | if (p != null) { 160 | str += ", "; 161 | } 162 | } 163 | return str + ")"; 164 | } 165 | } -------------------------------------------------------------------------------- /linearList-linkList-doubleLink/src/main/java/org/light4j/dataStructure/linearList/linkList/doubleLink/DoubleLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.doubleLink; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | 5 | /** 6 | * 双链表类 7 | * 8 | * @author longjiazuo 9 | */ 10 | public class DoubleLinkedList implements LList { 11 | protected DoubleLinkNode head;// 双链表的头结点 12 | protected int n;// 双链表的长度 13 | 14 | public DoubleLinkedList() {// 构造空双链表 15 | this.head = new DoubleLinkNode(null, null, null);// 头指针 16 | this.n = 0; 17 | } 18 | 19 | /** 20 | * 判断双链表是否为空 21 | */ 22 | @Override 23 | public boolean isEmpty() { 24 | return this.head.next == null; 25 | } 26 | 27 | /** 28 | * 返回双链表长度,时间复杂度为O(1) 29 | */ 30 | @Override 31 | public int length() { 32 | return n; 33 | } 34 | 35 | /** 36 | * 返回序号为index的的对象,如果链表为空或者序号错误则返回null 37 | */ 38 | @Override 39 | public E get(int index) { 40 | DoubleLinkNode p = this.head; 41 | int i = 0; 42 | while (p != null && i < index) {// 遍历双链表 43 | i++; 44 | p = p.next; 45 | } 46 | if (p != null) { 47 | return p.data; 48 | } 49 | return null; 50 | } 51 | 52 | /** 53 | * 设置序号为index的对象的值为element,如果操作成功则返回原对象,操作失败返回null 54 | */ 55 | @Override 56 | public E set(int index, E element) { 57 | if (index <= this.n && element != null) { 58 | DoubleLinkNode p = this.head; 59 | int i = 0; 60 | while (p != null && i < index) { 61 | i++; 62 | p = p.next; 63 | } 64 | if (p != null) { 65 | E old = p.data; 66 | p.data = element; 67 | return old; 68 | } 69 | } 70 | return null; 71 | } 72 | 73 | /** 74 | * 在指定位置插入非空的结点 75 | */ 76 | @Override 77 | public boolean add(int index, E element) { 78 | if (index < 0 || element == null) {// 如果插入序号不正确或者元素为空返回false 79 | return false; 80 | } 81 | if (index > this.n) {// 尾插入,把元素插入到最后面 82 | this.add(element); 83 | } else { 84 | DoubleLinkNode p = this.head;// p指向头结点 85 | int i = 0; 86 | while (p != null && i < index) {// 遍历双链表 87 | i++; 88 | p = p.next; 89 | } 90 | if (p != null) { 91 | DoubleLinkNode q = new DoubleLinkNode(element);// 新建q结点 92 | q.prev = p.prev; 93 | q.next = p; 94 | p.prev.next = q; 95 | p.prev = q; 96 | this.n++;// 链表长度加1 97 | } 98 | } 99 | return true; 100 | } 101 | 102 | /** 103 | * 在双链表的最后插入元素对象 104 | */ 105 | @Override 106 | public boolean add(E element) { 107 | if (element == null) {// 不允许插入空元素 108 | return false; 109 | } 110 | DoubleLinkNode p = this.head; 111 | while (p.next != null) {// 循环双链表 112 | p = p.next; 113 | } 114 | DoubleLinkNode q = new DoubleLinkNode(element);// 创建要插入的结点q 115 | p.next = q; 116 | q.prev = p; 117 | this.n++;// 链表长度加1 118 | return true; 119 | } 120 | 121 | /** 122 | * 移除索引index处的结点,操作成功返回被移除的对象,失败则返回null 123 | */ 124 | @Override 125 | public E remove(int index) { 126 | E old = null; 127 | if (index >= 0) {// 要删除元素的索引不能小于0 128 | if (index == 0) {// 等于0是头结点,不能移除头指针,如果值是0,赋值为1 129 | index = 1; 130 | } 131 | DoubleLinkNode p = this.head;// p结点指向头结点 132 | int i = 0; 133 | while (p != null && i < index) {// 遍历寻找元素 134 | i++; 135 | p = p.next; 136 | } 137 | if (p != null) { 138 | old = p.data; 139 | p.prev.next = p.next; 140 | if (p.next != null) { 141 | p.next.prev = p.prev; 142 | } 143 | this.n--;// 双链表长度减1 144 | } 145 | } 146 | return old; 147 | } 148 | 149 | /** 150 | * 清空双链表 151 | */ 152 | @Override 153 | public void clear() { 154 | this.head.next = null; 155 | this.n = 0; 156 | } 157 | 158 | @Override 159 | public String toString() {// 返回所有元素值对应的字符串 160 | String str = "("; 161 | DoubleLinkNode p = this.head.next; 162 | while (p != null) { 163 | str += p.data.toString(); 164 | p = p.next; 165 | if (p != null) { 166 | str += ", "; 167 | } 168 | } 169 | return str + ")"; 170 | } 171 | } -------------------------------------------------------------------------------- /tree-binaryTree/src/main/java/org/light4j/tree/binaryTree/BinaryTree.java: -------------------------------------------------------------------------------- 1 | package org.light4j.tree.binaryTree; 2 | 3 | /** 4 | * 二叉树类 5 | * 6 | * @author longjiazuo 7 | * 8 | * @param 9 | */ 10 | public class BinaryTree { 11 | // 根结点 12 | protected BinaryNode root; 13 | 14 | /** 15 | * 构造空二叉树 16 | */ 17 | public BinaryTree() { 18 | this.root = null; 19 | } 20 | 21 | /** 22 | * 构造指定结点的二叉树 23 | * 24 | * @param root 25 | */ 26 | public BinaryTree(BinaryNode root) { 27 | this.root = root; 28 | } 29 | 30 | /** 31 | * 判断二叉树是否为空 32 | * 33 | * @return 34 | */ 35 | public boolean isEmpty() { 36 | return this.root == null; 37 | } 38 | 39 | /** 40 | * 返回二叉树的根节点 41 | * 42 | * @return 43 | */ 44 | public BinaryNode getRoot() { 45 | return this.root; 46 | } 47 | 48 | /** 49 | * 返回node结点的父母结点 50 | * 51 | * @param node 52 | * @return 53 | */ 54 | public BinaryNode getParent(BinaryNode node) { 55 | return null; 56 | } 57 | 58 | /** 59 | * 先根遍历,从根结点root开始 60 | */ 61 | public void preOrder() { 62 | System.out.println("\n 先根序列:"); 63 | this.preOrder(root); 64 | } 65 | 66 | /** 67 | * 先根遍历,从指定节点开始 68 | */ 69 | public void preOrder(BinaryNode node) { 70 | if (node != null) { 71 | System.out.print(node.getData() + " "); 72 | preOrder(node.getLeft()); 73 | preOrder(node.getRight()); 74 | } 75 | } 76 | 77 | /** 78 | * 中根遍历,从根结点root开始 79 | */ 80 | public void inOrder() { 81 | System.out.println("\n 中根序列:"); 82 | this.inOrder(root); 83 | } 84 | 85 | /** 86 | * 中根遍历,从指定节点开始 87 | */ 88 | public void inOrder(BinaryNode node) { 89 | if (node != null) { 90 | inOrder(node.getLeft()); 91 | System.out.print(node.getData() + " "); 92 | inOrder(node.getRight()); 93 | } 94 | } 95 | 96 | /** 97 | * 后根遍历,从根结点root开始 98 | */ 99 | public void postOrder() { 100 | System.out.println("\n 后根序列:"); 101 | this.postOrder(root); 102 | } 103 | 104 | /** 105 | * 后根遍历,从指定节点开始 106 | */ 107 | public void postOrder(BinaryNode node) { 108 | if (node != null) { 109 | postOrder(node.getLeft()); 110 | postOrder(node.getRight()); 111 | System.out.print(node.getData() + " "); 112 | } 113 | } 114 | 115 | /** 116 | * 按层次遍历二叉树 117 | */ 118 | public void levelOrder() { 119 | 120 | } 121 | 122 | /** 123 | * 求二叉树结点个数 124 | * 125 | * @return 126 | */ 127 | public int count() { 128 | return this.count(this.root); 129 | } 130 | 131 | /** 132 | * 求从指定节点开始遍历的结点个数 133 | * 134 | * @param node 135 | * @return 136 | */ 137 | private int count(BinaryNode node) { 138 | if (node != null) { 139 | return 1 + count(node.getLeft()) + count(node.getRight()); 140 | } 141 | return 0; 142 | } 143 | 144 | /** 145 | * 求树的高度 146 | * 147 | * @return 148 | */ 149 | public int height() { 150 | return this.height(root); 151 | } 152 | 153 | /** 154 | * 求node子树的高度 155 | * 156 | * @param node 157 | * @return 158 | */ 159 | private int height(BinaryNode node) { 160 | if (node != null) { 161 | int ld = height(node.getLeft());// 返回左子树的高度 162 | int rd = height(node.getRight());// 返回右子树的高度 163 | return ld >= rd ? (ld + 1) : (rd + 1);// 当前子树高度为较高子树的高度加1 164 | } 165 | return 0; 166 | } 167 | 168 | /** 169 | * 查找并返回元素为element的结点,从根结点开始查找 170 | * 171 | * @param element 172 | * @return 173 | */ 174 | public BinaryNode search(E element) { 175 | return search(root, element); 176 | } 177 | 178 | /** 179 | * 查找并返回元素为element的结点,从指定结点开始查找 180 | * 181 | * @param p 182 | * @param element 183 | * @return 184 | */ 185 | public BinaryNode search(BinaryNode node, E element) { 186 | return null; 187 | } 188 | 189 | /** 190 | * 插入element元素作为node结点的左/右孩子结点 191 | * 192 | * @param node 193 | * @param data 194 | * @param leftChild 195 | */ 196 | public void insert(BinaryNode node, E data, boolean leftChild) { 197 | 198 | } 199 | 200 | /** 201 | * 删除p结点的左/右子树 202 | * 203 | * @param node 204 | * @param leftChild 205 | */ 206 | public void remove(BinaryNode node, boolean leftChild) { 207 | } 208 | 209 | /** 210 | * 清空二叉树 211 | */ 212 | public void clear() { 213 | this.root = null; 214 | } 215 | } -------------------------------------------------------------------------------- /linearList-seqList-iterator/src/main/java/org/light4j/dataStructure/linearList/seqList/iterable/IterableSeqList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.seqList.iterable; 2 | 3 | import java.util.Iterator; 4 | 5 | import org.light4j.dataStructure.linearList.AbstractLList; 6 | import org.light4j.dataStructure.linearList.LList; 7 | 8 | /** 9 | * 提供了迭代对象的线性表 10 | * 11 | * @author longjiazuo 12 | */ 13 | public class IterableSeqList extends AbstractLList implements LList { // 顺序表类,实现线性表接口 14 | private Object[] table; // 对象数组,私有成员 15 | private int n; // 顺序表长度 16 | 17 | /** 18 | * 指定空表的默认容量 19 | */ 20 | public IterableSeqList() { 21 | this(16); 22 | } 23 | 24 | /** 25 | * 构造方法,创建指定容量的空表 Math.abs(i)返回参数的绝对值 26 | */ 27 | public IterableSeqList(int capacity) { 28 | this.table = new Object[Math.abs(capacity)]; 29 | this.n = 0; 30 | } 31 | 32 | /** 33 | * 判断顺序表是否为空,若空返回true 34 | */ 35 | @Override 36 | public boolean isEmpty() { 37 | return this.n == 0; 38 | } 39 | 40 | /** 41 | * 返回顺序表长度 42 | */ 43 | @Override 44 | public int length() { 45 | return this.n; 46 | } 47 | 48 | /** 49 | * 返回index(初始值為0)位置的对象,若序号无效,返回null 50 | */ 51 | @SuppressWarnings("unchecked") 52 | @Override 53 | public E get(int index) { 54 | if (index >= 0 && index < this.n) { 55 | return (E) this.table[index]; 56 | } 57 | return null; 58 | } 59 | 60 | /** 61 | * 设置index位置的对象为element,若操作成功,返回原对象,否则返回null 62 | */ 63 | @Override 64 | public E set(int index, E element) { 65 | if (index >= 0 && index < this.n && element != null) { 66 | @SuppressWarnings("unchecked") 67 | E old = (E) this.table[index]; 68 | this.table[index] = element; 69 | return old; 70 | } 71 | return null; 72 | } 73 | 74 | /** 75 | * 在index位置插入element对象,若操作成功返回true,不能插入null 76 | */ 77 | @Override 78 | public boolean add(int index, E element) { 79 | if (element == null) { 80 | return false; // 不能插入null 81 | } 82 | if (this.n == this.table.length) { // 若数组满,则需要扩充顺序表的容量 83 | Object[] temp = this.table; 84 | this.table = new Object[temp.length * 2]; // 重新申请一个容量更大的数组 85 | for (int i = 0; i < temp.length; i++) { 86 | this.table[i] = temp[i]; // 复制数组元素,O(n) 87 | } 88 | } 89 | 90 | if (index < 0) { 91 | index = 0; // 下标容错 92 | } 93 | if (index > this.n) { 94 | index = this.n; // 下标容错 95 | } 96 | 97 | for (int j = this.n - 1; j >= index; j--) { // 元素后移,平均移动n/2 98 | this.table[j + 1] = this.table[j]; 99 | } 100 | this.table[index] = element; 101 | this.n++; 102 | return true; 103 | } 104 | 105 | /** 106 | * 在顺序表最好插入element对象 107 | */ 108 | @Override 109 | public boolean add(E element) { 110 | return this.add(this.n, element); 111 | } 112 | 113 | /** 114 | * 移去index位置的对象,若操作成功,则返回被移去对象,否则返回null 115 | */ 116 | @Override 117 | public E remove(int index) { 118 | if (this.n != 0 && index >= 0 && index < this.n) { 119 | @SuppressWarnings("unchecked") 120 | E old = (E) this.table[index]; 121 | for (int i = index; i < this.n - 1; i++) { // 元素前移,平均移动n/2 122 | this.table[i] = this.table[i + 1]; 123 | } 124 | this.table[this.n - 1] = null; 125 | this.n--; 126 | return old; // 若操作成功,则返回被移除对象 127 | } 128 | return null; // 未找到删除对象,操作不成功,返回null 129 | } 130 | 131 | /** 132 | * 清空顺序表 133 | */ 134 | @Override 135 | public void clear() { 136 | if (this.n != 0) { 137 | for (int i = 0; i < this.n; i++) { 138 | this.table[i] = null; 139 | } 140 | this.n = 0; 141 | } 142 | } 143 | 144 | /** 145 | * 返回迭代对象 146 | */ 147 | @Override 148 | public Iterator iterator() { 149 | return new SeqListIterator(); 150 | } 151 | 152 | @SuppressWarnings("hiding") 153 | private class SeqListIterator implements Iterator { 154 | int cursor = 0; 155 | 156 | @Override 157 | public boolean hasNext() { 158 | return cursor != n; 159 | } 160 | 161 | /** 162 | * 返回后继元素 163 | */ 164 | @Override 165 | public E next() { 166 | if (cursor != n) { 167 | @SuppressWarnings("unchecked") 168 | E next = (E) get(cursor); 169 | cursor++; 170 | return next; 171 | } 172 | return null; 173 | } 174 | 175 | /** 176 | * 移除元素 177 | */ 178 | @Override 179 | public void remove() { 180 | throw new UnsupportedOperationException();// 不支持该操作,抛出异常 181 | } 182 | } 183 | } -------------------------------------------------------------------------------- /linearList-linkList-head-iterable/src/main/java/org/light4j/dataStructure/linearList/linkList/head/iterable/IterableHeadSinglyLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.head.iterable; 2 | 3 | import java.util.Iterator; 4 | 5 | import org.light4j.dataStructure.linearList.AbstractLList; 6 | import org.light4j.dataStructure.linearList.LList; 7 | import org.light4j.dataStructure.linearList.linkList.Node; 8 | /** 9 | * 可迭代的带头结点的单链表类 10 | * 11 | * @author longjiazuo 12 | */ 13 | public class IterableHeadSinglyLinkedList extends AbstractLList implements LList { 14 | protected Node head;// 单链表的头结点,指向单链表的头结点 15 | protected Node rear;// 单链表的尾结点,指向单链表的最后一个结点 16 | protected int n;// 单链表的长度 17 | 18 | public IterableHeadSinglyLinkedList() {// 构造空单链表 19 | this.head = new Node(null);// 构造头结点,元素值为空 20 | this.rear = this.head;// 构造尾结点,初始化的时候头结点和尾结点都指向头结点 21 | this.n = 0;// 初始化链表长度为0 22 | } 23 | 24 | /** 25 | * 判断带头结点的单链表是否为空 26 | */ 27 | @Override 28 | public boolean isEmpty() { 29 | return this.head.next == null; 30 | } 31 | 32 | /** 33 | * 返回带头结点的单链表长度,时间复杂度为O(1) 34 | */ 35 | @Override 36 | public int length() { 37 | return this.n; 38 | } 39 | 40 | /** 41 | * 返回序号为index的的对象,如果链表为空或者序号错误则返回null 42 | */ 43 | @Override 44 | public E get(int index) { 45 | Node p = this.head; 46 | int i = 0; 47 | while (p != null && i < index) { 48 | i++; 49 | p = p.next; 50 | } 51 | if (p != null) { 52 | return p.data; 53 | } 54 | return null; 55 | } 56 | 57 | /** 58 | * 设置序号为index的对象的值为element,如果操作成功则返回原对象,操作失败返回null 59 | */ 60 | @Override 61 | public E set(int index, E element) { 62 | if (this.head != null && index >= 0 && element != null) { 63 | Node p = this.head; 64 | int i = 0; 65 | while (p != null && i < index) { 66 | i++; 67 | p = p.next; 68 | } 69 | if (p != null) { 70 | E old = p.data; 71 | p.data = element; 72 | return old;// 操作成功返回原对象 73 | } 74 | } 75 | return null;// 操作失败则返回null 76 | } 77 | 78 | /** 79 | * 在指定位置插入非空的指针对象 80 | */ 81 | @Override 82 | public boolean add(int index, E element) { 83 | if (element == null) {// 不允许插入非空元素 84 | return false; 85 | } 86 | if (index >= this.n) {// 尾插入,插入在最后 87 | this.add(element); 88 | } else { 89 | Node p = this.head; 90 | int i = 0; 91 | while (p.next != null && i < index) { 92 | i++; 93 | p = p.next; 94 | } 95 | // 下面操作可以包含头插入和中间插入 96 | Node q = new Node(element); 97 | q.next = p.next; 98 | p.next = q;// 将q结点插入到p结点之后 99 | this.n++; 100 | return true; 101 | } 102 | return false; 103 | } 104 | 105 | /** 106 | * 在单链表的最后插入元素对象,时间复杂度是O(1) 107 | */ 108 | @Override 109 | public boolean add(E element) { 110 | if (element == null) {// 不允许插入非空元素 111 | return false; 112 | } 113 | this.rear.next = new Node(element);// 尾插入 114 | this.rear = this.rear.next;// 移动尾指针 115 | this.n++;// 链表长度增加 116 | return true; 117 | } 118 | 119 | /** 120 | * 移除索引index处的结点,操作成功返回被移除的对象,失败则返回null 121 | */ 122 | @Override 123 | public E remove(int index) { 124 | E old = null; 125 | if (index >= 0) {// 头删除,中间删除,尾删除 126 | Node p = this.head; 127 | int i = 0; 128 | while (p.next != null && i < index) {// 从头结点开始遍历,定位到待删除结点的前驱结点 129 | i++; 130 | p = p.next; 131 | } 132 | if (p.next != null) { 133 | old = p.next.data; 134 | if (p.next == this.rear) {// 如果p结点的后一个结点是尾结点,则移除之后尾结点指针前移 135 | this.rear = p; 136 | } 137 | p.next = p.next.next;// 删除p结点的后继结点 138 | this.n--;// 链表长度减少 139 | return old; 140 | } 141 | } 142 | return old; 143 | } 144 | 145 | /** 146 | * 清空单链表 147 | */ 148 | @Override 149 | public void clear() { 150 | this.head.next = null; 151 | this.rear = this.head; 152 | this.n = 0; 153 | } 154 | 155 | /** 156 | * 返回迭代器对象 157 | */ 158 | @Override 159 | public Iterator iterator() { 160 | return new HeadSinglyLinkedListIterator(); 161 | } 162 | 163 | /** 164 | * 实现迭代器接口 165 | * 166 | * @author longjiazuo 167 | */ 168 | @SuppressWarnings("hiding") 169 | private class HeadSinglyLinkedListIterator implements Iterator { 170 | @SuppressWarnings("unchecked") 171 | private Node cursor = (Node) head;// 初始的时候指向头结点 172 | 173 | /** 174 | * 判断是否有后继元素 175 | */ 176 | @Override 177 | public boolean hasNext() { 178 | return cursor != null && cursor.next != null; 179 | } 180 | 181 | /** 182 | * 返回后继元素 183 | */ 184 | @Override 185 | public E next() { 186 | if (cursor != null && cursor.next != null) { 187 | E element = cursor.next.data; 188 | cursor = cursor.next; 189 | return element; 190 | } 191 | return null; 192 | } 193 | 194 | /** 195 | * 移除元素 196 | */ 197 | @Override 198 | public void remove() { 199 | throw new UnsupportedOperationException();// 不支持该操作,抛出异常 200 | } 201 | } 202 | } -------------------------------------------------------------------------------- /linearList-linkList-increase/src/main/java/org/light4j/dataStructure/linearList/linkList/increase/IncreaseSinglyLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.light4j.dataStructure.linearList.linkList.increase; 2 | 3 | import org.light4j.dataStructure.linearList.LList; 4 | import org.light4j.dataStructure.linearList.linkList.Node; 5 | import org.light4j.dataStructure.linearList.linkList.SinglyLinkedList; 6 | 7 | /** 8 | * 增强的单链表,比单链表类SinglyLinkedList增加了一些常用的方法 9 | * 10 | * @author longjiazuo 11 | * 12 | * @param 13 | */ 14 | public class IncreaseSinglyLinkedList extends SinglyLinkedList { 15 | 16 | /** 17 | * 构造空单链表 18 | */ 19 | public IncreaseSinglyLinkedList() { 20 | super(); 21 | } 22 | 23 | /** 24 | * 构造指定头结点的单链表 25 | * 26 | * @param head 27 | */ 28 | public IncreaseSinglyLinkedList(Node head) { 29 | super(head); 30 | } 31 | 32 | /** 33 | * 由指定数组中的多个对象构造单链表 34 | * 35 | * @param elements 36 | */ 37 | public IncreaseSinglyLinkedList(E[] elements) { 38 | if (elements == null) {// 数组元素不允许为空 39 | throw new RuntimeException("elements is null"); 40 | } 41 | Node p = null;// 记录当前节点,遍历 42 | for (int i = 0; i < elements.length; i++) { 43 | E element = elements[i]; 44 | if (element == null) {// 元素值不允许为空 45 | throw new RuntimeException("the value of elements is null"); 46 | } 47 | Node node = new Node(element);// 新建元素结点 48 | if (this.head == null) {// 如果头结点为空则头指针指向该结点 49 | this.head = node; 50 | } else { 51 | p.next = node;// 当前节点的next域指向该新建结点 52 | } 53 | p = node;// p指向当前节点 54 | } 55 | } 56 | 57 | /** 58 | * 以单链表list构造新的单链表,复制单链表 59 | * 60 | * @param list 61 | */ 62 | public IncreaseSinglyLinkedList(IncreaseSinglyLinkedList list) { 63 | if (list == null) {// list不允许为空 64 | throw new RuntimeException("list is null"); 65 | } 66 | Node p = list.head; 67 | Node q = null; 68 | while (p != null) { 69 | Node node = new Node(p.data);// 创建要新追加的结点; 70 | if (q == null) {// 头结点为空则头指针直接指向新追加的结点 71 | this.head = node;// 要新追加的结点; 72 | } else { 73 | q.next = node;// 把追加的结点添加到最后一个结点之后 74 | } 75 | q = node;// q结点指向node 76 | p = p.next; 77 | } 78 | } 79 | 80 | /** 81 | * 将指定单链表list链接在当前单链表之后 82 | * 83 | * @param list 84 | */ 85 | public void concat(IncreaseSinglyLinkedList list) { 86 | if (list == null) {// list不允许为空 87 | throw new RuntimeException("list is null"); 88 | } 89 | Node p = this.head; 90 | while (p.next != null) {// 循环找到当前链表的最后一个结点 91 | p = p.next; 92 | } 93 | if (this.head == null) {// 若当前链表的头结点为空,则当前链表的头结点指向被连接链表的头结点 94 | this.head = list.head; 95 | } else {// 若当前链表的头结点为空,则当前链表的最后一个结点的next域指向被连接链表的头结点 96 | p.next = list.head; 97 | } 98 | } 99 | 100 | /** 101 | * 查找指定对象,若找到则返回结点,否则返回null 102 | * 103 | * @param element 104 | * @return 105 | */ 106 | public Node search(E element) { 107 | if (element == null) { 108 | return null; 109 | } 110 | Node p = this.head; 111 | while (p != null) { 112 | E targetElement = p.data; 113 | if (element.equals(targetElement)) { 114 | return p; 115 | } 116 | p = p.next; 117 | } 118 | return null; 119 | } 120 | 121 | /** 122 | * 判断单链表是否包含指定对象,包含则返回true,不包含返回false 123 | * 124 | * @param element 125 | * @return 126 | */ 127 | public boolean contain(E element) { 128 | return search(element) != null; 129 | } 130 | 131 | /** 132 | * 移除首次出现的指定对象,移除成功返回true,移除失败返回false 133 | * 134 | * @param element 135 | * @return 136 | */ 137 | public boolean remove(E element) { 138 | if (element == null || this.head == null) {// 被移除元素和头结点不能为空 139 | return false; 140 | } 141 | 142 | E targetElement = this.head.data; 143 | if (element.equals(targetElement)) {// 头结点先特殊处理,判断头结点数据是否匹配 144 | this.head = this.head.next;// 头删除,头结点指针移动 145 | return true; 146 | } 147 | 148 | Node p = this.head; 149 | Node pre = null;// 记录当前结点的上一个结点 150 | while (p.next != null) {// 判断头结点之后的别的结点 151 | targetElement = p.next.data; 152 | if (element.equals(targetElement)) { 153 | pre = p;// 找到匹配元素,pre记录当前节点p 154 | } 155 | p = p.next; 156 | if (pre != null) {// pre不为空说明已经找到匹配的元素 157 | break;// 跳出循环 158 | } 159 | } 160 | if (p != null && pre != null) { 161 | pre.next = p.next;// 中间删除,头指针不移动 162 | return true; 163 | } 164 | return false; 165 | } 166 | 167 | /** 168 | * 将单链表中的obj对象替换为对象element,成功返回true,失败返回false 169 | * 170 | * @param obj 171 | * @param element 172 | * @return 173 | */ 174 | public boolean replace(Object obj, E element) { 175 | if (obj == null || element == null) {// 对象不能为空值 176 | return false; 177 | } 178 | if (this.head == null) {// 头结点不能为空 179 | return false; 180 | } 181 | Node p = this.head; 182 | int i = 0; 183 | while (p != null) { 184 | E targetElement = p.data;// 要进行匹配的目标元素 185 | if (obj.equals(targetElement)) {// 找到匹配元素 186 | i++;// 用i记录匹配的个数 187 | p.data = element;// 元素替换 188 | } 189 | p = p.next; 190 | } 191 | if (i != 0) {// i不等于0说明找到匹配的元素,返回true 192 | return true; 193 | } 194 | return false; 195 | } 196 | 197 | /** 198 | * 比较两条单链表是否相等,相等返回true,不相等返回false 199 | * 200 | * @param obj 201 | * @param element 202 | * @return 203 | */ 204 | public boolean equals(Object obj) { 205 | if (obj == null) {// 单链表不允许为空 206 | return false; 207 | } 208 | if (this == obj) {// 如果是同一个实例,返回true 209 | return true; 210 | } 211 | 212 | if ((obj instanceof LList)) {// 判断obj是否是LList的对象实例或者子类的实例 213 | @SuppressWarnings("unchecked") 214 | LList originalList = (LList) obj; 215 | if (this.length() != originalList.length()) {// 链表长度不相等返回false 216 | return false; 217 | } 218 | for (int i = 0; i < originalList.length(); i++) { 219 | E originalElement = originalList.get(i); 220 | E targetElement = this.get(i); 221 | if (originalElement == null || targetElement == null) {// 元素值不能为空 222 | return false; 223 | } 224 | if (!originalElement.equals(targetElement)) {// 找到不匹配的元素返回false 225 | return false; 226 | } 227 | } 228 | return true; 229 | } 230 | return false; 231 | } 232 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # data-structure-project【持续更新中...】 2 | 数据结构算法,自己实现集合框架系列整理总结样例,在每篇文章后面有对应源代码地址链接。
3 | 4 | #### 自己实现集合框架系列教程
5 | Github项目地址:https://github.com/longjiazuo/data-structure-project
6 | 码云项目地址:
http://git.oschina.net/longshiy/data-structure-project
7 | 文章地址:
自己实现集合框架系列教程
8 | 9 | #### 自己实现集合框架(一):定义线性表接口 10 | Github项目地址:linearList-interface
11 | 码云项目地址:linearList-interface
12 | 文章地址:自己实现集合框架(一):定义线性表接口
13 | 14 | #### 自己实现集合框架(二):顺序表的实现 15 | Github项目地址:linearList-seqList
16 | 码云项目地址:linearList-seqList
17 | 文章地址:自己实现集合框架(二):顺序表的实现
18 | 19 | #### 自己实现集合框架(三):利用顺序表解决约瑟夫环问题 20 | Github项目地址:linearList-seqList-josephus
21 | 码云项目地址:linearList-seqList-josephus
22 | 文章地址:自己实现集合框架(三):利用顺序表解决约瑟夫环问题
23 | 24 | #### 自己实现集合框架(四):单链表的实现 25 | Github项目地址:linearList-linkList
26 | 码云项目地址:linearList-linkList
27 | 文章地址:自己实现集合框架(四):单链表的实现
28 | 29 | #### 自己实现集合框架(五):利用单链表解决约瑟夫环问题 30 | Github项目地址:linearList-linkList-josephus
31 | 码云项目地址:linearList-linkList-josephus
32 | 文章地址:自己实现集合框架(五):利用单链表解决约瑟夫环问题
33 | 34 | #### 自己实现集合框架(六):实现单链表逆转 35 | Github项目地址:linearList-linkList-reverse
36 | 码云项目地址:linearList-linkList-reverse
37 | 文章地址:自己实现集合框架(六):实现单链表逆转
38 | 39 | #### 自己实现集合框架(七):带头结点单链表的实现 40 | Github项目地址:linearList-linkList-head
41 | 码云项目地址:linearList-linkList-head
42 | 文章地址:自己实现集合框架(七):带头结点单链表的实现
43 | 44 | #### 自己实现集合框架(八):可排序单链表的实现 45 | Github项目地址:linearList-linkList-head-sorted
46 | 码云项目地址:linearList-linkList-head-sorted
47 | 文章地址:自己实现集合框架(八):可排序单链表的实现
48 | 49 | #### 自己实现集合框架(九):循环单链表的实现 50 | Github项目地址:linearList-linkList-circular
51 | 码云项目地址:linearList-linkList-circular
52 | 文章地址:自己实现集合框架(九):循环单链表的实现
53 | 54 | #### 自己实现集合框架(十):双链表的实现 55 | Github项目地址:linearList-linkList-doubleLink
56 | 码云项目地址:linearList-linkList-doubleLink
57 | 文章地址:自己实现集合框架(十):双链表的实现
58 | 59 | #### 自己实现集合框架(十一):栈接口定义 60 | Github项目地址:linearList-stack-interface
61 | 码云项目地址:linearList-stack-interface
62 | 文章地址:自己实现集合框架(十一):栈接口定义
63 | 64 | #### 自己实现集合框架(十二):顺序栈的实现 65 | Github项目地址:linearList-stack-sequence
66 | 码云项目地址:linearList-stack-sequence
67 | 文章地址:自己实现集合框架(十二):顺序栈的实现
68 | 69 | #### 自己实现集合框架(十三):链式栈的实现 70 | Github项目地址:linearList-stack-link
71 | 码云项目地址:linearList-stack-link
72 | 文章地址:自己实现集合框架(十三):链式栈的实现
73 | 74 | #### 自己实现集合框架(十四):队列接口 75 | Github项目地址:linearList-queue-interface
76 | 码云项目地址:linearList-queue-interface
77 | 文章地址:自己实现集合框架(十四):队列接口
78 | 79 | #### 自己实现集合框架(十五):顺序队列的实现 80 | Github项目地址:linearList-queue-sequence
81 | 码云项目地址:linearList-queue-sequence
82 | 文章地址:自己实现集合框架(十五):顺序队列的实现
83 | 84 | #### 自己实现集合框架(十六):顺序循环队列的实现 85 | Github项目地址:linearList-queue-sequence-cycle
86 | 码云项目地址:linearList-queue-sequence-cycle
87 | 文章地址:自己实现集合框架(十六):顺序循环队列的实现
88 | 89 | #### 自己实现集合框架(十七):链式队列的实现 90 | Github项目地址:linearList-queue-link
91 | 码云项目地址:linearList-queue-link
92 | 文章地址:自己实现集合框架(十七):链式队列的实现
93 | 94 | ### 附录: 95 | 更多内容请阅读我的博客: 96 | http://blog.longjiazuo.com/ -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------