数据结构--手写链表

JDK1.7 HashMap 底层数据结构为 数组+链表(解决哈希冲突)

JDK1.8 HashMap 底层数据结构为 数组+红黑树(解决哈希冲突)

package JavaBasic.node;

/**
 * 自定义Node 链表
 * @author lzwen10
 * @version 1.0
 * @date 2020/3/25/0025 22:24
 */
public class Node {

    private Object content;

    private Node next;

    public Node(Object content, Node next) {
        this.content = content;
        this.next = next;
    }

    public static void main(String[] args) {
        Node header =  new Node(new Object(),null);
        //方式1
        header.next = new Node(new Object(),null);
        //方式2
        header = new Node(new Object(),header);
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,链表(也称为单链表)是一种基础数据结构,它由一系列节点组成,每个节点包含数据和指向下一个节点的引用。创建和操作链表的便利之处在于它的动态性和灵活性,可以在运行时添加或删除元素,而不需要像数组那样预先分配固定大小。 下面是一个简单的Java链表节点和链表类的例子: ```java // 链表节点定义 class Node { int data; Node next; public Node(int data) { this.data = data; this.next = null; } } // 链表类定义 public class LinkedList { private Node head; // 头节点 // 插入节点方法 public void insert(int value) { Node newNode = new Node(value); if (head == null) { head = newNode; } else { Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; } } // 查找节点方法 public boolean contains(int value) { Node current = head; while (current != null) { if (current.data == value) { return true; } current = current.next; } return false; } // 删除节点方法 public void remove(int value) { if (head == null) { return; } if (head.data == value) { head = head.next; return; } Node current = head; while (current.next != null && current.next.data != value) { current = current.next; } if (current.next != null) { current.next = current.next.next; } } // 显示链表元素(仅做演示,实际应用中可能有更优雅的方法) public void printList() { Node current = head; while (current != null) { System.out.print(current.data + " -> "); current = current.next; } System.out.println("null"); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值