栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java - 双向无头不循环 - 链表增删查改实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java - 双向无头不循环 - 链表增删查改实现

MylinkedList.java

class ListNode {
    public int val;
    public ListNode prev;
    public ListNode next;

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

public class MylinkedList {

    public ListNode head;//指向双向链表的头节点
    public ListNode last;//指向的是尾巴节点

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

    //得到单链表的长度
    public int size() {
        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.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return  false;
    }

    //头插法
    public void addFirst(int data) {
        //分为两种情况
        ListNode node = new ListNode(data);
        if (head == null) {
            this.head = node;
            this.last = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if (head == null) {
            this.head = node;
            this.last = node;
        }else {
            this.last.next = node;
            node.prev = this.last;
            this.last = node;
        }
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if (head == null) {
            return;
        }
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;
                    if (head != null) {             //如果只有一个node,被删掉了,可能会被空指针异常
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else if (cur == last){
                    cur.prev.next = cur.next;
                    last = last.prev;
                }else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                return;
            }else {
                cur = cur.next;
            }
        }
    }


    //删除所有值为key的节点
    public void removeAllKey(int key){
        if (head == null) {
            return;
        }
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;
                    if (head != null) {             //如果只有一个node,被删掉了,可能会被空指针异常
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else if (cur == last){
                    cur.prev.next = cur.next;
                    last = last.prev;
                }else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }else {
                cur = cur.next;
            }
        }
    }

    public ListNode searchIndex (int index) {
        if (index < 0 || index > size()) {
            System.out.println("index位置不合法!");
            return null;
        }
        ListNode cur = this.head;
        while (index != 0) {
            index--;
            cur = cur.next;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        ListNode node = new ListNode(data);
        ListNode cur;
        cur = searchIndex(index);
        if (index == 0) {
            addFirst(data);
        }else if (index == size()) {
            addLast(data);
        }else {
                node.next = cur;
                node.prev = cur.prev;
                cur.prev.next = node;
                cur.prev = node;
//            node.next = cur.prev.next;
//            cur.prev.next = node;            //两种都行
//            node.prev = cur.prev;
//            cur.prev = node;
        }
    }
    //暴力的做法是直接将head和last直接置为null
    //温柔的做法将所有节点置为null
    public void clear() {
        ListNode cur = this.head;
        while (cur != null) {
            ListNode curNext = head.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }
    //clear的第二种写法
    public void clear2() {
        while (head != null) {
            ListNode curNext = head.next;
            head.next = null;
            head.prev = null;
            head = curNext;
        }
        last = null;
    }

}

TestDemo.java

public class TestDemo {
    public static void main(String[] args) {
        MylinkedList mylinkedList = new MylinkedList();
        mylinkedList.addLast(1);
        mylinkedList.addLast(2);
        mylinkedList.addLast(3);
        mylinkedList.addLast(4);
        mylinkedList.display();
        mylinkedList.remove(2);
        mylinkedList.display();
//        mylinkedList.addIndex(7,77);
//        mylinkedList.display();
    }
}

//class Test1 {
//    public static int aaa;
//    public static void bbb() {
//        System.out.println("111");
//    }
//}

//public class TestDemo{
//    static boolean Paddy;
//    public static void ccc() {
//        System.out.println("111");
//    }
//    public static void main(String args[]){     //内部类的静态方法可以直接通过方法名进行访问吗?  可以
        System.out.println(test.aaa);
//        ccc();
        System.out.println(ccc());
//    }

//public class TestDemo{
//
//    private static int count;
//    private static void as() {
//        System.out.println("666");
//    }
//
//    public static void main(String[] args) {
//
//        TestDemo test=new TestDemo(88);
//        System.out.println(count);
//        test.as();
//        Test1 ass = new Test1();
//        ass.bbb();
//
        System.out.println(test.count);
//
//    }
//
//    TestDemo(int a) {
//
//        count=a;
//
//    }
//}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/462752.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号