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

Java——模拟实现单链表

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

Java——模拟实现单链表

本文旨在对链表的各种操作自己进行模拟实现,以加深自己对链表操作的理解。

public class linkList {
    public static class Node{
        E value;
        Node next;

        public Node(E value){
            this.value = value;
        }
    }

    Node head; //指向链表中的第一个有效节点


    //头插法
    public void addFirst(E data){
        Node newNode = new Node<>(data);
        newNode.next = head;
        head = newNode;
    }

    //尾插法
    public void addLast(E data){
        Node newNode = new Node<>(data);
        if (null == head){
            head = newNode;
        }else{
            Node cur = head;
            while (null != cur.next)
                cur = cur.next;
            cur.next = newNode;
        }
    }

    //插入任意位置
    public boolean add(int index,E data){
        if(index < 0){
            throw new IllegalArgumentException("addIndex:position非法");
        }
        if(0 == index){
            addFirst(data);
            return true;
        }

        Node cur = head;
        Node pre = head.next;
        Node node = new Node<>(data);

        while(1 < index--){
            cur = cur.next;
            pre = pre.next;
        }
        cur.next = node;
        node.next = pre;
        return true;
    }

//    //删除任意位置的元素,没能写出
//    public boolean remove(int index){
//        if (null == head || index < 0 ){
//            return false;
//        }
//        Node cur = head;
//        Node pre = head.next;
//        while(0 < index--){
//            cur = cur.next;
//            pre = pre.next;
//        }
//    }

    //删除第一次出现的key元素
    public void remove(E e){
        Node cur = head;
        Node pre = null;

        while(null != cur){
            if (e.equals(cur.value)){
                cur.value = null;
                if (pre == null){
                    head = cur.next;
                }else{
                    pre.next = cur.next;
                }
                return;
            }
            pre = cur;
            cur = cur.next;

        }
    }

    //删除所有值为key的元素
    public void removeAll(E e){
        Node cur = head;
        Node pre = null;
        while(cur != null){
            if(e.equals(cur.value)){
                // 删除节点
                cur.value = null;
                if(pre == null){
                    head = cur.next;
                    cur = head;
                }else{
                    pre.next = cur.next;
                    cur = pre.next;
                }
            }else{
                pre = cur;
                cur = cur.next;
            }
        }
    }

    //输出最后一个结点
    public E getLastNode(){
        if(null == head)
            return head.value;
        Node cur = head;
        while(null != cur.next){
            cur = cur.next;
        }
        return cur.value;
    }

    //输出倒数第二个结点
    public E getPenultimateNode() {
        if (null == head || null == head.next)
            return null;
        Node cur = head;
        Node pre = cur.next;
        while(null != pre.next){
            cur = cur.next;
            pre = pre.next;
        }
        return cur.value;
    }

    //找到链表第n个结点
    public E getNNode(int n) {
        Node cur = head;
        if (n < 0){
            return null;
        }
        while(0 < n--){
            cur = cur.next;
        }

        return cur.value;
    }

    //计算元素个数
    public int size(){
        if (null == head.next)
            return 1;
        Node cur = head;
        int count = 0;
        while(null != cur){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //是否包含某个元素
    public boolean contain(E e){
        Node cur = head;
        while(null != cur){
            if (e.equals(cur.value))
                return true;
            cur = cur.next;
        }
        return false;
    }


    //打印链表中元素
    public void printlinkList(){
        Node cur = head;

        while (null != cur){
            System.out.print(cur.value);
            if (null != (cur.next)){
                System.out.print("-->");
            }
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public String toString() {
        Node cur = head;
        String str = "";
        while (null != cur){
            str += cur.value;
            if (null != (cur.next)){
                str += "-->";
            }
            cur = cur.next;
        }
        return str;
    }

    public static void testlinkList() {
        linkList linkList = new linkList<>();
        linkList.addLast(1);
        linkList.addLast(2);
        linkList.addLast(3);
        linkList.addLast(4);
        linkList.addFirst(5);
        linkList.addLast(1);
        linkList.addLast(2);
        linkList.addLast(3);
        linkList.addLast(4);
        linkList.addFirst(5);

        linkList.printlinkList();
        System.out.println(linkList);
        System.out.println("最后一个结点:"+linkList.getLastNode());
        System.out.println("倒数第二个结点:"+linkList.getPenultimateNode());
        System.out.println("位置3的元素为:" + linkList.getNNode(3));
        System.out.println("链表中元素的个数为:" + linkList.size());
        System.out.println("链表中是否包含4:" + linkList.contain(4));
        linkList.add(1,6); //像位置2插入元素6
        System.out.println(linkList);
        linkList.remove(3);
        System.out.println("删除第一个元素3后:" + linkList);
        linkList.removeAll(1);
        System.out.println("删除所有元素1后: " + linkList);

    }

    public static void main(String[] args) {
        testlinkList();
    }

}

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

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

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