zoukankan      html  css  js  c++  java
  • Java自己实现双向链表LinkList

    /**
     * <p>
     * Node 双向链表实体类
     * <p>
     * 
     * @author <a href="mailto:yangkj@corp.21cn.com">yangkj</a>
     * @version
     * @since 2016年8月15日
     */
    public class Node {
        
        // 双向链表-前一节点
        Node previous;
        // 双向链表-当前节点对象
        Object obj;
        // 双向链表-后一节点
        Node next;
    
        public Node() {
            super();
        }
    
        public Node(Node previous, Object obj, Node next) {
            super();
            this.previous = previous;
            this.obj = obj;
            this.next = next;
        }
    
        public Node getPrevious() {
            return previous;
        }
    
        public void setPrevious(Node previous) {
            this.previous = previous;
        }
    
        public Object getObj() {
            return obj;
        }
    
        public void setObj(Object obj) {
            this.obj = obj;
        }
    
        public Node getNext() {
            return next;
        }
    
        public void setNext(Node next) {
            this.next = next;
        }
    
    }
    /**
     * 
     * <p>
     * MyLinkList 双向列表
     * <p>
     * 
     * @author <a href="mailto:yangkj@corp.21cn.com">yangkj</a>
     * @version
     * @since 2016年8月15日
     */
    public class MyLinkList {
        /**
         * 首节点
         */
        private Node firstNode;
        /**
         * 尾节点
         */
        private Node lastNode;
        /**
         * 节点个数
         */
        private int size;
    
        public MyLinkList() {
            super();
        }
    
        /**
         * 添加节点
         * 
         * @param obj
         */
        public void add(Object obj) {
            if (firstNode == null) {
                // 如果链表为空,则设置当前对象为首节点,同时也是尾节点;且该节点的前一节点和后一节点都为null
                Node node = new Node(null, obj, null);
                firstNode = node;
                lastNode = node;
            } else {
                // 直接在lastNode后面添加新的节点
                Node node = new Node(lastNode, obj, null);
                // 旧的尾节点的下一个节点指向新加节点
                lastNode.setNext(node);
                // 链表的尾节点设成新加节点
                lastNode = node;
            }
            size++;
        }
    
        /**
         * 指定索引位置上插入节点
         * 
         * @param index
         * @param obj
         */
        public void add(int index, Object obj) {
            Node temp = node(index);
            if (temp != null) {
                Node up = temp.previous;
                Node newNode = new Node(up, obj, temp);
                up.setNext(newNode);
                temp.setPrevious(newNode);
                size++;
            }
        }
    
        /**
         * 获取指定节点对象
         * 
         * @param index
         * @return
         */
        public Object get(int index) {
            Node temp = node(index);
            if (temp != null) {
                return temp.obj;
            } else {
                return null;
            }
        }
    
        /**
         * 越界检测
         * 
         * @param index
         */
        private void rangCheck(int index) {
            if (index < 0 || index > size) {
                throw new ArrayIndexOutOfBoundsException(index);
            }
        }
    
        /**
         * 移除指定索引 的节点
         * 
         * @param index
         */
        public void remove(int index) {
            // 越界检测
            Node temp = node(index);
            if (temp != null) {
                Node up = temp.previous;
                Node down = temp.next;
                up.next = down;
                up.previous = up;
                temp = null;
                size--;
            }
    
        }
    
        /**
         * 获取指定索引下的节点
         * 
         * @param index
         * @return
         */
        private Node node(int index) {
            rangCheck(index);
            Node temp = null;
            if (firstNode != null) {
                temp = firstNode;
                for (int i = 0; i < index; i++) {
                    temp = temp.next;
                }
            }
            return temp;
        }
        /**
         * 设置指定索引的值
         * @param index
         * @param obj
         */
        public void set(int index,Object obj){
            rangCheck(index);
            Node temp = node(index);
            if(temp!=null){
                temp.setObj(obj);
            }
        }
        /**
         * 获得链表长度
         * 
         * @return
         */
        public int size() {
            return size;
        }
    
        public static void main(String[] args) {
            MyLinkList linkList = new MyLinkList();
            linkList.add("aaa");
            linkList.add("bbb");
            linkList.add("ccc");
            
            System.out.println(linkList.get(1));
            linkList.set(1, "fff");
            System.out.println(linkList.get(1));
            
        }
    }
  • 相关阅读:
    15款精美的 WordPress 电子商务网站模板
    15套免费的扁平化界面设计素材【免费下载】
    35幅使用小图片组成的创意插图作品 【上篇】
    sqlserver2014两台不同服务器上数据库同步
    C++ Reflection Library
    美国的包容主要体现在接受移民,那么,中国的包容主要体现在哪里呢?答案就是资本
    mysql主从复制
    Kubernetes+Istio
    Net异步编程
    研发的困境----DEVOPS
  • 原文地址:https://www.cnblogs.com/parryyang/p/5774037.html
Copyright © 2011-2022 走看看