有关链表问题里面涉及的几个必要的类
在左程云_算法与数据结构下的链表问题中的代码中的需要引用的类,有一些print方法是根据题目需要或者测试中输出优美一些所以进行了一定的修改。
Node
package algorithm_zuochengyun;
public class Node {
public int value;
public Node next = null;
public Node(int value) {
this.value = value;
}
public static void Print(Node head) {
Node curNode = head;
if (curNode == null) {
System.out.print("null");
}
while (curNode != null) {
PrintNode(curNode);
curNode = curNode.next;
}
System.out.println();
}
public static void CirclePrint(Node head) {
if (head == null) {
System.out.println("null");
return;
}
Node current = head.next;
PrintNode(head);
while (current != head) {
PrintNode(current);
current = current.next;
}
System.out.println();
}
public static void PrintNode(Node head) {
if (head == null) {
System.out.print("null");
} else {
System.out.print(head.value + " ");
}
}
public static int getLen(Node head) {
if (head == null) {
return 0;
}
int n = 0;
while (head != null) {
head = head.next;
n++;
}
return n;
}
public static Node initCircleList(int n) {
Node head = new Node(1);
Node curNode = head;
for (int i = 2; i <= n; i++) {
curNode.next = new Node(i);
curNode = curNode.next;
if (i == n) {
curNode.next = head;
}
}
return head;
}
public static Node init() {
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(6);
head.next.next.next.next.next.next = new Node(7);
return head;
}
}
----------
DoubleNode
package algorithm_zuochengyun;
public class DoubleNode {
public int value;
public DoubleNode last;
public DoubleNode next;
public DoubleNode(int value) {
this.value = value;
}
public static void PrintNext(DoubleNode head) {
while (head != null) {
System.out.print(head.value + " ");
head = head.next;
}
System.out.println();
}
public static void PrintLast(DoubleNode head) {
while (head != null) {
System.out.print(head.value + " ");
head = head.last;
}
System.out.println();
}
public static DoubleNode init() {
DoubleNode head = new DoubleNode(1);
head.next = new DoubleNode(2);
head.next.next = new DoubleNode(3);
head.next.next.next = new DoubleNode(4);
head.next.next.next.next = new DoubleNode(5);
return head;
}
}
----------
RandomNode
package algorithm_zuochengyun;
public class RandomNode {
public int value;
public RandomNode next = null;
public RandomNode rand = null;
public RandomNode(int value) {
this.value = value;
}
public static void PrintNode(RandomNode randomNode) {
if (randomNode == null) {
System.out.print("null");
} else {
System.out.print(randomNode.value + " ");
}
}
public static void Print(RandomNode head) {
RandomNode curNode = head;
System.out.print("order: ");
if (curNode == null) {
System.out.print("null");
}
while (curNode != null) {
PrintNode(curNode);
curNode = curNode.next;
}
System.out.println();
curNode = head;
System.out.print("rand: ");
while (curNode != null) {
System.out.print(curNode.rand == null ? "- " : curNode.rand.value + " ");
curNode = curNode.next;
}
System.out.println();
}
}