java链表1

本文介绍了链表的基本概念和结构,包括单向、带头、非循环等类型,并重点讲解了如何实现无头单向非循环链表的部分功能,如头插法、尾插法、任意位置插入、查找、删除操作。
摘要由CSDN通过智能技术生成

链表

链表的概念以及结构
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用连结次序实现。

链表的种类:

  • 单向、双向
    在这里插入图片描述
  • 带头、不带头
    在这里插入图片描述
  • 循环、非循环

在这里插入图片描述

链表的实现

无头单向非循环链表部分功能实现1:
  1. 头插法
public void addFirst(int data) {
    ListNode node = new ListNode(data);
    if(this.head == null) {
        this.head = node;
    }else {
        node.next = this.head;
        this.head = node;
    }
}
  1. 尾插法
public void addLast(int data){
    ListNode node = new ListNode(data);
    ListNode cur = this.head;
    if(this.head == null) {
        this.head = node;
    }else {
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }
}
  1. 任意位置插入,第一个数据节点为0号下标
//找到index的位置
private ListNode searchIndex(int index) {
    ListNode cur = this.head;
    //cur -> index-1
    int count = 0;
    while (count < index-1) {
        count++;
        cur = cur.next;
    }
    return cur;
}
//插入
public boolean addIndex(int index,int data){
    if(index <  0 | index > getLength()) {
        System.out.println("index不合法!");
        return false;
    }
    if(index == 0) {
        addFirst(data);
        return true;
    }
    //找到index-1的位置
    ListNode cur = searchIndex(index);
    ListNode node = new ListNode(data);
    node.next = cur.next;
    cur.next = node;
    return true;
}
  1. 查找是否包含关键字key是否在单链表当中
public boolean contains(int key){
    ListNode cur = this.head;
    while (cur != null) {
        if(cur.data == key) {
            return true;
        }
        cur = cur.next;
    }
    return false;
}
  1. 关键字为key的节点
private ListNode searchPrev(int key) {
    ListNode prev = this.head;
    //prev.next  头已经判断了
    while (prev.next != null) {
        if(prev.next.data == key) {
            return prev;
        }
        prev = prev.next;
    }
    return null;
}
  1. 删除第一次出现关键字为key的节点
public void remove(int key){
    if(this.head == null) {
        System.out.println("单链表为空");
        return;
    }
    //0、删除的节点是否是头结点
    if(this.head.data == key) {
        this.head = this.head.next;
        return;
    }

    //1、找到key的前驱  如果返回空
    ListNode prev = searchPrev(key);
    if(prev == null) {
        return;
    }
    //2、删除节点
    ListNode del = prev.next;
    prev.next = del.next;
}
  1. 删除所有值为key的节点
public void removeAllKey(int key){
    ListNode prev = this.head;
    ListNode cur = this.head.next;
    while (cur != null) {
        if(prev.next.data == key){
            prev.next = cur.next;
            cur = cur.next;
        }else {
            prev = cur;
            cur = cur.next;
        }
    }
    if(this.head.data == key) {
        this.head = this.head.next;
    }
}
  1. 得到单链表的长度
public int getLength(){
    int count = 0;
    ListNode cur = this.head;
    while (cur != null) {
        count++;
        cur = cur.next;
    }
    return count;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值