理解Java是如何构造出链表的

1 单链表的基础与构造方法

学习所有数据结构的基础:创建 + 增删改查;我认为还需要会调用一些常用API;

1.1 链表的内部结构

链表由 数据 + 指针 构成,数据存放值,指针指向下一个节点,连接链表。

规范的链表定义:

public class ListNode {
    private int data;
    private ListNode next;
    public ListNode(int data) {
        this.data = data;
    }
    public int getData() {
        return data;
    }
    public void setData(int data) {
        this.data = data;
    }
    public ListNode getNext() {
        return next;
    }
    public void setNext(ListNode next) {
        this.next = next;
    }
}

leetcode中定义:

public class ListNode {
    public int val;
    public ListNode next;

    ListNode(int x) {
        val = x;
        //这个一般作用不大,写了会更加规范
        next = null;
    }
}
ListNode listnode=new ListNode(1);

1.2 单链表的遍历

链表的所有操作一定是从头逐个向后访问,再进行其他操作的。

public static int getListLength(Node head) {
        int length = 0;
       Node node = head;
        while (node != null) {
            length++;
            node = node.next;
        }
        return length;
    }

1.3 单链表的插入

链表的插入分为三种情况:首部、中部、尾部。
(1)头部:

(2)中部:

(3)尾部:

/**
     * 链表插入
     * @param head       链表头节点
     * @param nodeInsert 待插入节点
     * @param position   待插入位置,从1开始
     * @return 插入后得到的链表头节点
     */
    public static Node insertNode(Node head, Node nodeInsert, int position) {
        if (head == null) {
        //这里可以认为待插入的结点就是链表的头结点,也可以抛出不能插入的异常
            return nodeInsert;
        }
        //已经存放的元素个数
        int size = getLength(head);
        if (position > size+1  || position < 1) {
            System.out.println("位置参数越界");
            return head;
        }

        //表头插入
        if (position == 1) {
            nodeInsert.next = head;
            // 这里可以直接 return nodeInsert;还可以这么写:
            head = nodeInsert;
            return head;
        }

        Node pNode = head;
        int count = 1;
        //这里position被上面的size被限制住了,不用考虑pNode=null
        while (count < position - 1) {
            pNode = pNode.next;
            count++;
        } 
        nodeInsert.next = pNode.next;
        pNode.next = nodeInsert;

        return head;
    }

1.4 单链表的删除

同样分为头部、中部、尾部
(1)头部:
(2)中部:
(3)尾部:

/**
     * 删除节点
     * @param head     链表头节点
     * @param position 删除节点位置,取值从1开始
     * @return 删除后的链表头节点
     */
    public static Node deleteNode(Node head, int position) {
        if (head == null) {
            return null;
        }
        int size = getListLength(head);
        //思考一下,这里为什么是size,而不是size+1
        if (position > size || position <1) {
            System.out.println("输入的参数有误");
            return head;
        }
        if (position == 1) {
            //curNode就是链表的新head
            return head.next;
        } else {
            Node preNode = head;
            int count = 1;
            while (count < position - 1) {
                preNode = preNode.next;
                count++;
            }
            Node curNode = preNode.next;
            preNode.next = curNode.next;
        }
        return head;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Java中,可以使用三叉链表构造二叉树。下面是一个示例代码,演示了如何使用三叉链表实现二叉树的基本操作。 ```java // 定义二叉树节点类 class BTNode { int data; BTNode left; BTNode right; BTNode parent; public BTNode(int data) { this.data = data; this.left = null; this.right = null; this.parent = null; } } // 构造二叉树的类 class BinaryTree { BTNode root; public BinaryTree() { this.root = null; } // 插入节点 public void insert(int data) { BTNode newNode = new BTNode(data); if (root == null) { root = newNode; } else { BTNode current = root; BTNode parent; while (true) { parent = current; if (data < current.data) { current = current.left; if (current == null) { parent.left = newNode; newNode.parent = parent; return; } } else { current = current.right; if (current == null) { parent.right = newNode; newNode.parent = parent; return; } } } } } // 其他二叉树操作方法... // 复制二叉树 public void copyTree(BTNode dstTreeRoot, BTNode srcTreeRoot) { if (srcTreeRoot == null) { dstTreeRoot = null; } else { dstTreeRoot = new BTNode(srcTreeRoot.data); if (srcTreeRoot.left != null) { copyTree(dstTreeRoot.left, srcTreeRoot.left); dstTreeRoot.left.parent = dstTreeRoot; } if (srcTreeRoot.right != null) { copyTree(dstTreeRoot.right, srcTreeRoot.right); dstTreeRoot.right.parent = dstTreeRoot; } } } } // 示例代码 public class Main { public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.insert(5); tree.insert(3); tree.insert(7); tree.insert(2); tree.insert(4); tree.insert(6); tree.insert(8); // 复制二叉树 BinaryTree copiedTree = new BinaryTree(); copiedTree.copyTree(copiedTree.root, tree.root); // 其他操作... } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小C卷Java

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

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

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

打赏作者

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

抵扣说明:

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

余额充值