单链表的增删改查

初识单链表

class Node{
       public  int data ;   // 0
       public  Node next ;   //  null

       public  Node(int data ){
           this.data = data;
           this.next = null;

       }
   }
public class MyLinkedList {
    public Node head; //保存单链表的头节点的引用   null

    public void addFirst(int data) {          //  头插法
        Node node = new Node(data);

        if (this.head == null) {          // 第一次插入节点
            this.head = node;
            return;
        }

        node.next = this.head;
        this.head = node;
    }

    public void addLast(int data) {            //尾插法
        Node node = new Node(data);
        if (head == null) {
            this.head = node;
            return;
        }
        Node cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }


    public void display() {         //  打印单链表
        Node cur = this.head;
        while (cur != null) {
            System.out.print(cur.data + "  ");
            cur = cur.next;
        }
    }

    public boolean contains(int key) {    //查找是否包含关键字key是否在单链表中
        Node cur = this.head;
        while (cur != null) {
            if (cur.data == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public int size() {                // 查询链表长度
        Node cur = this.head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void addIndex(int index, int data) {
        Node node = new Node(data);               //任意位置插入,第一个数据节点为0号下标
        if (index == 0) {                            //1、定义一个cur  走 index-1 步
            addFirst(data);                       //2、插入
            return;
        }
        if (index == this.size()) {
            addLast(data);
            return;
        }
        //先找到  index的前一个节点位置的引用
        searchIndex(index);
        //进行插入
        Node cur = searchIndex(index);
        node.next = cur.next;
        cur.next = node;
        return;
    }

    private Node searchIndex(int index) {
        //对index 进行合法性分析
        if (index < 0 || index > this.size()) {
            throw new RuntimeException("index 位置不合法!");

        }
        Node cur = this.head;  // 走  index-1
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }


    private Node searchPrev(int key) {          //找关键字的前面一个节点
        Node prev = this.head;
        while (prev.next != null) {
            if (prev.next.data == key) {
                return prev;
            } else {
                prev = prev.next;
            }
        }
        return null;
    }

    public void remove(int key) {          //删除第一次出现的关键字的节点
        if (this.head == null) {
            return;
        }
        if (this.head.data == key) {
            this.head = this.head.next;
            return;
        }
        Node prev = searchPrev(key);
        if (prev == null) {
            System.out.println("没有要删除的节点");
            return;
        }
        Node del = searchPrev(key).next;
        prev.next = del.next;

    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值