单向链表
代码实现
package csdn.dreamzuora.list;
public class SingleNode {int id;String name;SingleNode next;public SingleNode(int id, String name) {this.id = id;this.name = name;}@Overridepublic String toString() {return "Node{" +"id=" + id +", name='" + name + '\'' +", next=" + next +'}';}
}
package csdn.dreamzuora.list;
public class SingleLinkedList {private SingleNode headNode = new SingleNode(0, "head");private SingleNode tailNode = headNode;public void tailAppend(SingleNode appendNode){tailNode.next = appendNode;tailNode = appendNode;}public void headAppend(SingleNode appendNode){SingleNode childNode = headNode.next;if (childNode == null){tailNode.next = appendNode;tailNode = appendNode;}else {headNode.next = appendNode;appendNode.next = childNode;}}public void show(){SingleNode childNode = headNode;while (childNode.next != null){SingleNode next = childNode.next;System.out.print("->" + next.name);childNode = next;}}}
单元测试
package csdn.dreamzuora.list;import org.junit.After;
import org.junit.Test;import static org.junit.Assert.*;
public class SingleLinkedListTest {SingleLinkedList list = new SingleLinkedList();@Testpublic void tailAppend() {for (int i = 0; i < 10; i++){SingleNode appendNode = new SingleNode(i, "node-" + i);list.tailAppend(appendNode);}}@Testpublic void headAppend() {for (int i = 0; i < 10; i++){SingleNode appendNode = new SingleNode(i, "node-" + i);list.headAppend(appendNode);}}@Afterpublic void showList(){list.show();}
}
控制台打印
头插法
尾插法
双向链表
代码实现
package csdn.dreamzuora.list;
public class DoubleNode {int id;String name;DoubleNode next;DoubleNode last;public DoubleNode(int id, String name) {this.id = id;this.name = name;}@Overridepublic String toString() {return "DoubleNode{" +"id=" + id +", name='" + name + '\'' +'}';}
}
package csdn.dreamzuora.list;import java.util.ArrayList;
import java.util.List;
public class DoubleLinkedList {private DoubleNode headNode = new DoubleNode(0, "head");private DoubleNode tailNode = headNode;public void tailAppend(DoubleNode appendNode){tailNode.next = appendNode;appendNode.last = tailNode;tailNode = appendNode;}public void headAppend(DoubleNode appendNode){DoubleNode childNode = headNode.next;if (childNode == null){headNode.next = appendNode;appendNode.last = headNode;}else {appendNode.next = childNode;appendNode.last = headNode;childNode.last = appendNode;headNode.next = appendNode;}}public void show(){DoubleNode childNode = headNode;List<String> nextList = new ArrayList<String>();List<String> lastList = new ArrayList<String>();while (childNode.next != null){DoubleNode node = childNode.next;childNode = node;nextList.add("->" + node.name);DoubleNode nodeLast = childNode.last;lastList.add("<-" + nodeLast.name);}System.out.print("打印后继节点: ");nextList.forEach(elem -> System.out.print(elem));System.out.println();System.out.print("打印前驱节点: ");lastList.forEach(elem -> System.out.print(elem));System.out.println();}}
单元测试
package csdn.dreamzuora.list;import org.junit.After;
import org.junit.Before;
import org.junit.Test;import static org.junit.Assert.*;
public class DoubleLinkedListTest {DoubleLinkedList list = new DoubleLinkedList();@Testpublic void tailAppend() {for (int i = 0; i < 10; i++){list.tailAppend(new DoubleNode(i, "node-" + i));}}@Testpublic void headAppend() {for (int i = 0; i < 10; i++){list.headAppend(new DoubleNode(i, "node-" + i));}}@Afterpublic void showList(){list.show();}
}
控制台打印
头插法
尾插法