无头单向非循环链表的解析

目录

1.概念及结构

2.创建一个单链表的类

3.头插法

4.尾插法

5.任意位置插入

6.删除节点

 7.链表长度

8.打印链表

 9.清空链表


1.概念及结构

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

无头单相飞循环链表的一个节点含有value(存放数值的) 、 next(下一个节点的地址)

2.创建一个单链表的类

创建的这个类用于下面的方法引用

class ListNode {
    public int val;
    public ListNode next;//节点的地址

    public ListNode(int val) {
        this.val = val;
    }
}

3.头插法

头插法就是在原来的链表前面增加一个新节点,然后再使新节点变为头节点

public void addFirst(int data) {
        ListNode listNode = new ListNode(data);//插一个新的节点
        listNode.next = this.head;//绑定位置先绑定后面的位置
        this.head = listNode;
    }

4.尾插法

先定义一个 cur 引用头结点,然后遍历一遍链表,直到找到尾节点就停止,尾节点就是next域为null。

如果链表为空的,则尾插的第一个就作为链表的头结点

public void addLast(int data) {
        ListNode listNode = new ListNode(data);
        if(this.head == null) {
            this.head = listNode;
        }else {
            ListNode cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = listNode;
        }
    }

5.任意位置插入

在任意位置插入节点时,我们先找到要插入节点位置前的一个节点,任意位置包括了头插、尾插和中间位置。

//找到index-1 位置节点的地址
    public ListNode findIndex(int index) {
        ListNode cur = this.head;
        while (index-1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) {
        if(index < 0 || index >size()) {
            System.out.println("位置不合法");
            return;
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

6.删除节点

删除节点要先找到要删除前的节点

//找到要删除关键字的前一个节点
    public ListNode searchPerv(int key) {
        ListNode cur = this.head;
        while (cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(this.head == null) {
            System.out.println("单链表为空,不用删除");
            return;
        }
        if(this.head.val == key) {
            this.head = this.head.next;
            return;
        }
        ListNode cur = searchPerv(key);
        if(cur == null) {
            System.out.println("没有你要删除的节点");
            return;
        }
        ListNode del = cur.next;//将删除的节点与上一个节点连接
        cur.next = del.next;//将删除的节点的 左右两边的 节点 连接,从而删除的节点没有连接就被去掉了
    }
    //删除所有值为key的节点
    public ListNode removeAllKey(int key) {
        if(this.head == null) return null;

        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while (cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        //最后处理头节点
        if(this.head.val == key) {
            this.head = this.head.next;
        }
        return this.head;
    }

 7.链表长度

public int size() {
        int count = 0;
        ListNode cur = this.head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

8.打印链表

 public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

 9.清空链表

public void clear() {
        while (this.head != null) {
            ListNode curNext = head.next;
            this.head.next = null;
            this.head =curNext;
        }
    }

猜你喜欢

转载自blog.csdn.net/m0_60494863/article/details/121723921