单链表(增删)

主代码

1、定义单链表类

public class MySignal2 {
    private ListNode2 head;

    MySignal2() {
        this.head = null;
    }

    public ListNode2 getHead() {
        return head;
    }

    public void setHead(ListNode2 head) {
        this.head = head;
    }

    public void display() {
        ListNode2 listNode2 = this.head;
        //MySignal2 mySignal21 = mySignal2;
        while (listNode2 != null) {
            System.out.print(listNode2.getDate() + " ");
            listNode2 = listNode2.next;
        }
        System.out.println();
    }
}
  
    /*节点类*/
    class ListNode2 {
        private double date;
        ListNode2 next;

        ListNode2(double date) {
            setDate(date);
            setNext(null);
        }

        public ListNode2 getNext() {
            return next;
        }

        public void setNext(ListNode2 next) {
            this.next = next;
        }

        public double getDate() {

            return date;
        }

        public void setDate(double date) {
            this.date = date;
        }


    }
}

插入

1、头插法
2、尾插法
3、任意节点插入

 /*头插法*/
    public void addFirst(double data) {
        ListNode2 listNode = new ListNode2(data);
        if (head == null) {
            this.head = listNode;
        } else {
            listNode.setNext(head);
            this.head = listNode;
        }
    }

    /*尾插法*/
    public void addLast(double data) {
        ListNode2 listNode2 = new ListNode2(data);
        ListNode2 node2 = head;
        if (head == null) {
            head.next = listNode2;
        }
        while (node2.next != null) {
            node2 = node2.next;
        }
        node2.next = listNode2;
    }

    /*求单链表长度*/
    public int getLength() {
        int count = 0;
        ListNode2 listNode2 = this.head;
        while (listNode2 != null) {
            listNode2 = listNode2.next;
            count++;
        }
        return count;
    }

    /*任意位置插入*/
    private ListNode2 initPos(int init) {
        ListNode2 listNode2 = this.head;
        for (int i = 1; i < init; i++) {
            listNode2 = listNode2.next;
        }
        return listNode2;
    }

    public void addInsert(int init, double data) {
        ListNode2 listNode2 = new ListNode2(data);
        if (init < 0 | init > this.getLength()) {
            System.out.println("插入位置不合法!");
            return;
        } else if (init == 0) {
            listNode2.next = this.head;
        } else {
            listNode2.next = this.initPos(init).next;
            this.initPos(init).next = listNode2;
        }
    }

删除

1、删除第一个key节点
2、删除所有key节点

  /*查找key节点是否在单链表内*/
    public boolean seachKey(double key) {
        ListNode2 listNode2 = this.head;
        while (listNode2 != null) {
            if (listNode2.getDate() == key) {
                return true;
            }
        }
        return false;
    }

    /*删除key节点*/
    public void remove(double key) {
        ListNode2 listNode2 = this.head;
        int count = 0;
        if (this.head == null) {
            System.out.println("单链表为空");
            return;
        }
        if (listNode2.getDate() == key) {
            this.head = this.head.next;
        }
        while (listNode2.getDate() != key) {
            listNode2 = listNode2.next;
            count++;
        }
        this.initPos(count).next = listNode2.next;
    }
    /*删除所有key值*/
    public void removeAllkey(double key) {

        /*ListNode2 listNode2 = this.head ;
        ListNode2 listNode3 = this.head.next;
        while (listNode3!=null){
            if (listNode2.next.getDate() == key){
                listNode2.next = listNode3.next;
                listNode3 = listNode3.next;
            }else {
                listNode2 = listNode3;
                listNode3 = listNode3.next;
            }
            if (this.head.getDate() == key){
                this.head = this.head.next;
            }
        }*/

        ListNode2 listNode2;
        ListNode2 listNode3;
        if (this.head == null) {
            System.out.println("单链表为空");
            return;
        }
        while (this.head.getDate() == key) {
            this.head = this.head.next;
        }

        listNode2 = this.head;
        listNode3 = this.head.next;
        while (listNode2.getDate() != key && listNode3 != null) {
            //System.out.println("11");
            //listNode3 = listNode2.next;
            if (listNode3.getDate() == key) {
                listNode2.next = listNode3.next;
                listNode3 = listNode3.next;
            } else {
                listNode2 = listNode3;
                listNode3 = listNode3.next;
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值