不带头节点的单链表的实现

1、静态的方法能不能访问实例成员变量?不能,静态的不依赖于对象
2、

  1. 顺序表中间/头部的插入删除,时间复杂度为O(N)
  2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。
  3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到200,我们再继续 插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。
    如何解决以上问题呢?下面给出了链表的结构来看看:
    3、顺序表:物理上连续逻辑上也连续的空间
    链表:物理上比一定连续,逻辑上一定连续
import java.util.List;
//不带头节点的单链表
class ListNode {
    public int data;
    public ListNode next;

    public ListNode (int data) {
        this.data = data;
        this.next = null;
    }
}
class SingleLest {
    public ListNode head;  //标志头

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

    //头插法
    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;
        }

    }
    //尾插法

    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;
        }
    }
    //打印单链表数据
    public void display(){

        if(this.head == null) {
            return;
        }
        ListNode cur = this.head;
        while (cur != null) {
            System.out.println(cur.data+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    private ListNode searchIndex(int index) {
        //prey-->index-1;
        ListNode prev = this.head;
        int count = 0;
        while(count < index-1) {
            prev = prev.next;
            count++;
        }
        return prev;
    }
    //插入到index位置
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        //下标不合法
        if(index<0||index>getLength()) {
            return false;
        }
        //头插法
        if(index == 0) {
            addFirst(data);
            return true;
        }
        ListNode prev = searchIndex(index);
        ListNode node = new ListNode(data);
        node.next = prev.next;
        prev.next = node;
        return false;
    }
    public int getLength() {
        int count = 0;
        ListNode cur = this.head;
        while(cur!= null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while(cur != null) {
            if(cur.data == key) {
                return true;
                //return cur;
            }
            cur = cur.next;
        }
        return  false;
        //return null;
    }
    //删除第一次出现关键字为key的节点
    private  ListNode searchPrev(int key) {

        ListNode prev = this.head;
        while(prev.next != null) {
            if(prev.next.data==key) {
                return prev;
            }
            prev = prev.next;
        }
        return null;

    }
    public void remove(int key){
        //删除的结点是头节点
        //找到删除的前驱,如果找不到,返回null
        //进行删除

        if(this.head.data == key){
            this.head = this.head.next;
            return;
        }
        ListNode prev = searchPrev(key);
        if(prev == null) {
            System.out.println("没有你要删除的节点");
            return;
        }
        ListNode del = prev.next;
        prev.next = del.next;
    }
    //删除所有值为key的节点
    //只遍历单列表一遍
    public void removeAllKey(int key) {
        ListNode cur = this.head.next;
        ListNode prev = this.head;
        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;
        }
    }
    public void clear(){
        //this.head = null;
        while (this.head.next != null) {
            ListNode cur = this.head.next;
            this.head.next = cur.next;
        }
        this.head = null;
    }
}

实现结果如下:

在这里插入图片描述

发布了43 篇原创文章 · 获赞 41 · 访问量 1796

猜你喜欢

转载自blog.csdn.net/weixin_45662626/article/details/102905240