左程云_算法与数据结构 — 链表问题 — Node、DoubleNode、RandomNode类

有关链表问题里面涉及的几个必要的类

在左程云_算法与数据结构下的链表问题中的代码中的需要引用的类,有一些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();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值