数据结构(jav版本) 单链表 day(5)

一、单链表

1.1 概述

单链表存储数据的优点是数据存储的实际物理空间可以不连续。
单链表一般包括value区域和next区域,value区域是为了存值的,next区域存指向下一个节点的引用。

1.2 Node类

public class Node {
    Object value;
    Node next;
    }

1.3 添加节点

  • 如果当前节点的next区域为空,则将当前节点与下一个节点进行链接
  • 否则顺着当前节点往下查找,找到next、区域为空的节点,将节点连接到该节点的后面。
 public void append(Node element) {
        //如果next区域不为空,则找下一个next区域
        Node flag = this.next;
        Node before = this;
        while (flag != null) {
            before = before.next;
            flag = flag.next;
        }
        before.next = element;
    }

1.4 删除节点

删除当前节点的写一个节点,直接将当前节点与下下个节点相连即可。

 public void delete(){
        this.next = this.next.next;
    }

1.5 插入节点

在当前节点的下一个位置插入指定节点

 public void insert(Node node){
     Node now = this;
     Node next = this.next;
     this.next = node;
     node.next = next;
    }

二、测试

package Array;

public class demo3 {
    public static void main(String[] args) {
        Node no = new Node(3);
        Node no1 = new Node(4);
        Node no2 = new Node("你好");
        Node no3 = new Node(6);
        no.append(no1);
        no.append(no2);
        no.append(no3);
        System.out.println(no.next.value);
        System.out.println(no.next.next.value);
        System.out.println(no.next.next.next.value);
        no.delete();
        no.delete();
        System.out.println(no.next.value);
        no.delete();
        System.out.println(no.next);
        no.insert(no1);
        no.delete();
        System.out.println(no.next);
    }
}

结果:
4
你好
6
6
null
null

三、循环单链表

使最后一个节点的next引用指向第一个节点。

3.1 循环链表的类定义

public class Node {
    Object value;
    Node next = this;
}

3.2 当前节点后插入一个节点

点击此处,参考1.5

3.3 删除当前节点的下一个节点

点击此处,参考1.4

3.4 Node next = this解析

  • 作用
    使当前节点的next区域指向当前节点。
  • 举例
    a.next = this = a;
    b.next = this = b;
    a.next = b;
    b.next = b -> b.next -> a.next -> b.next -> a;
    以此类推…

四、测试

源代码:

public class Node {
    Object value;
    Node next = this;

    public Node(Object value) {
        this.value = value;
    }
    //删除下一个节点
    public void delete(){
        this.next = this.next.next;
    }
    //插入一个节点
    public void insert(Node node){
     Node now = this;
     Node next = this.next;
     this.next = node;
     node.next = next;
    }

    @Override
    public String toString() {
        return "value=" + value + " ";

    }
}

测试代码:

public class demo3 {
    public static void main(String[] args) {
        Node no = new Node(3);
        Node no1 = new Node(4);
        Node no2 = new Node("你好");
        Node no3 = new Node(6);
        no.insert(no1);
        no1.insert(no2);
        System.out.println(no1.next);
        System.out.println(no.next);
        System.out.println(no2.next);
    }
}

结果:
value=你好 
value=4 
value=3 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值