java链表怎么实现

worktile 其他 253

回复

共3条回复 我来回复
  • fiy的头像
    fiy
    Worktile&PingCode市场小伙伴
    评论

    链表是一种常见的数据结构,在Java中可以通过类来实现链表。以下是一种常见的实现方式:

    1. 定义链表节点类
    首先,我们需要定义链表的节点类,节点类中包含两个属性:数据(value)和指向下一个节点的指针(next)。

    “`java
    public class ListNode {
    int val;
    ListNode next;
    public ListNode(int val) {
    this.val = val;
    this.next = null;
    }
    }
    “`

    2. 创建链表
    在链表类中,我们可以定义一个头节点(head)来表示整个链表的起始位置。链表的操作包括插入节点、删除节点、查找节点等。

    “`java
    public class MyLinkedList {
    private ListNode head;

    public MyLinkedList() {
    this.head = null;
    }

    // 插入节点
    public void insert(int val) {
    ListNode newNode = new ListNode(val);
    if (head == null) {
    head = newNode;
    } else {
    ListNode current = head;
    while (current.next != null) {
    current = current.next;
    }
    current.next = newNode;
    }
    }

    // 删除节点
    public void delete(int val) {
    if (head == null) {
    return;
    }
    if (head.val == val) {
    head = head.next;
    } else {
    ListNode prev = head;
    ListNode current = head.next;
    while (current != null && current.val != val) {
    prev = current;
    current = current.next;
    }
    if (current != null) {
    prev.next = current.next;
    }
    }
    }

    // 查找节点
    public boolean search(int val) {
    ListNode current = head;
    while (current != null) {
    if (current.val == val) {
    return true;
    }
    current = current.next;
    }
    return false;
    }
    }
    “`

    3. 使用链表
    可以创建一个MyLinkedList对象,然后调用插入、删除、查找等方法来操作链表。

    “`java
    public class Main {
    public static void main(String[] args) {
    MyLinkedList list = new MyLinkedList();

    list.insert(1);
    list.insert(2);
    list.insert(3);

    System.out.println(“链表是否包含元素 2:” + list.search(2)); // 输出:true

    list.delete(2);

    System.out.println(“链表是否包含元素 2:” + list.search(2)); // 输出:false
    }
    }
    “`

    以上就是一种在Java中实现链表的方式。通过定义节点类和链表类,我们可以方便地操作链表的插入、删除、查找等操作。

    2年前 0条评论
  • worktile的头像
    worktile
    Worktile官方账号
    评论

    Java中链表可以通过自定义类来实现,也可以使用Java提供的LinkedList类来直接使用。

    1. 自定义链表类:
    自定义链表类需要定义一个节点类,节点类包含数据和指向下一个节点的指针。然后,在链表类中定义插入节点、删除节点、搜索节点等方法。

    “`java
    class Node {
    // 数据
    int data;
    // 下一个节点的指针
    Node next;

    public Node(int data) {
    this.data = data;
    this.next = null;
    }
    }

    class LinkedList {
    // 链表的头节点
    Node head;

    public LinkedList() {
    this.head = null;
    }

    // 在链表头部插入一个节点
    public void insert(int data) {
    Node newNode = new Node(data);
    newNode.next = head;
    head = newNode;
    }

    // 删除指定值的节点
    public void delete(int data) {
    Node temp = head;
    Node prev = null;

    // 如果头节点的值就是要删除的值
    if (temp != null && temp.data == data) {
    head = temp.next;
    return;
    }

    while (temp != null && temp.data != data) {
    prev = temp;
    temp = temp.next;
    }

    if (temp == null) {
    return;
    }

    prev.next = temp.next;
    }

    // 搜索指定值的节点
    public boolean search(int data) {
    Node temp = head;
    while (temp != null) {
    if (temp.data == data) {
    return true;
    }
    temp = temp.next;
    }
    return false;
    }
    }
    “`

    2. 使用LinkedList类:
    Java提供了一个LinkedList类,在java.util包下,可以直接使用该类来实现链表。

    “`java
    import java.util.LinkedList;

    LinkedList linkedList = new LinkedList<>();

    // 在链表头部插入一个元素
    linkedList.addFirst(1);

    // 在链表尾部插入一个元素
    linkedList.addLast(2);

    // 删除链表头部的元素
    linkedList.removeFirst();

    // 删除链表尾部的元素
    linkedList.removeLast();

    // 搜索链表中是否包含某个元素
    linkedList.contains(1);
    “`

    以上是两种常用的Java链表实现方式,根据实际需求选择合适的方式。

    2年前 0条评论
  • 不及物动词的头像
    不及物动词
    这个人很懒,什么都没有留下~
    评论

    实现链表的一种常见方式是使用Java语言编写链表类。下面将介绍如何用Java实现链表。

    一、链表的定义和节点的实现
    链表是一种线性数据结构,由一系列节点组成,每个节点包含了一个数据元素和一个指向下一个节点的指针。节点的定义如下:

    “`java
    class Node {
    int data;
    Node next;

    public Node(int data) {
    this.data = data;
    this.next = null;
    }
    }
    “`

    二、链表类的实现

    “`java
    class LinkedList {
    private Node head;

    public LinkedList() {
    this.head = null;
    }

    // 在链表末尾添加节点
    public void append(int data) {
    Node newNode = new Node(data);

    if (head == null) {
    head = newNode;
    } else {
    Node curr = head;
    while (curr.next != null) {
    curr = curr.next;
    }
    curr.next = newNode;
    }
    }

    // 在链表头部添加节点
    public void prepend(int data) {
    Node newNode = new Node(data);

    if (head == null) {
    head = newNode;
    } else {
    newNode.next = head;
    head = newNode;
    }
    }

    // 在指定位置插入节点
    public void insert(int data, int position) {
    Node newNode = new Node(data);

    if (position == 0) {
    newNode.next = head;
    head = newNode;
    } else {
    Node curr = head;
    for (int i = 0; i < position - 1 && curr.next != null; i++) { curr = curr.next; } newNode.next = curr.next; curr.next = newNode; } } // 从链表中删除指定值的节点 public void delete(int data) { if (head == null) { return; } if (head.data == data) { head = head.next; return; } Node curr = head; while (curr.next != null && curr.next.data != data) { curr = curr.next; } if (curr.next != null) { curr.next = curr.next.next; } } // 判断链表是否为空 public boolean isEmpty() { return head == null; } // 获取链表长度 public int length() { int count = 0; Node curr = head; while (curr != null) { count++; curr = curr.next; } return count; } // 打印链表元素 public void print() { Node curr = head; while (curr != null) { System.out.print(curr.data + " "); curr = curr.next; } System.out.println(); }}```三、使用链表类可以使用链表类进行链表的操作。示例代码如下:```javapublic class Main { public static void main(String[] args) { LinkedList linkedList = new LinkedList(); linkedList.append(1); linkedList.append(2); linkedList.append(3); linkedList.print(); // 输出:1 2 3 linkedList.prepend(0); linkedList.print(); // 输出:0 1 2 3 linkedList.insert(4, 3); linkedList.print(); // 输出:0 1 2 4 3 linkedList.delete(2); linkedList.print(); // 输出:0 1 4 3 System.out.println(linkedList.isEmpty()); // 输出:false System.out.println(linkedList.length()); // 输出:4 }}```通过以上代码,我们可以实现创建链表、在头部和尾部添加节点、在指定位置插入节点、删除指定值的节点、判断链表是否为空、获取链表的长度以及打印链表元素等操作。这样就完成了在Java中实现链表的过程。

    2年前 0条评论
注册PingCode 在线客服
站长微信
站长微信
电话联系

400-800-1024

工作日9:30-21:00在线

分享本页
返回顶部