【朗致面试】

题目要求

1. 手写一个四向链表结构带泛型

2. 然后写一个函数实现以下功能:

2.1 输入参数int n,生成一个多维四向链表
2.2 每个节点都有四个方向,如果有值就指向他,如果没有,就指向null

这个就是输出结果,可以看得到每个节点的上方向,如果没有就是空

public class ListNode<T> {
    T val;
    ListNode<T> up;
    ListNode<T> down;
    ListNode<T> left;
    ListNode<T> right;
    
    public T getVal() {
        return val;
    }

    public void setVal(T val) {
        this.val = val;
    }

    public ListNode<T> getUp() {
        return up;
    }

    public void setUp(ListNode<T> up) {
        this.up = up;
    }

    public ListNode<T> getDown() {
        return down;
    }

    public void setDown(ListNode<T> down) {
        this.down = down;
    }

    public ListNode<T> getLeft() {
        return left;
    }

    public void setLeft(ListNode<T> left) {
        this.left = left;
    }

    public ListNode<T> getRight() {
        return right;
    }

    public void setRight(ListNode<T> right) {
        this.right = right;
    }
}

public class Array {


    public static ListNode<Integer> generatorArr(int n) {
        if (n <= 0) {
            return null;
        }

        // 创建第一个节点
        ListNode<Integer> head = new ListNode<>();
        head.val = 1;
        // 创建第一维链表
        ListNode<Integer> current = head;
        for (int i = 2; i <= n; i++) {
            ListNode<Integer> newNode = new ListNode<>();
            newNode.setVal(i);
            current.setRight(newNode);
            newNode.setLeft(current);
            current = newNode;
        }
        // 创建剩余维度的链表
        ListNode<Integer> prevRowHead = head;
        ListNode<Integer> prevRowCurrent = prevRowHead;
        for (int row = 2; row <= n; row++) {
            ListNode<Integer> newRowHead = new ListNode<>();
            newRowHead.setVal(row * n + 1);
            prevRowCurrent.setDown(newRowHead);
            newRowHead.setUp(prevRowCurrent);
            current = newRowHead;
            for (int col = 2; col <= n; col++) {
                ListNode<Integer> newNode = new ListNode<>();
                newNode.setVal((row - 1) * n + col);
                current.setRight(newNode);
                newNode.setLeft(current);
                prevRowCurrent = prevRowCurrent.getRight();
                prevRowCurrent.setDown(newNode);
                newNode.setUp(prevRowCurrent);
                current = newNode;
            }

            prevRowHead = newRowHead;
            prevRowCurrent = prevRowHead;
        }

        return head;
    }

	//for test
    public static void main(String[] args) {
        int n = new Random().nextInt(1) + 10;
        System.err.println(n + "维");
        ListNode node = generatorArr(n);
        printListNode(node);
    }

    private static void printListNode(ListNode node) {
        ListNode row = node;
        while (row != null) {
            ListNode cur = row;
            while (cur != null) {
                ListNode down = cur.getDown();
                System.out.print(cur.val + " " + "up node:" + (cur.up == null ? null : cur.up.val) + "   ");
                cur = cur.right;
            }
            System.out.println();
            row = row.down;
        }

    }
}

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ctrl CV选手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值