zoukankan      html  css  js  c++  java
  • 自定义实现LinkList

            linkList底层是双向链表,其增加,删除速度快,但是查找慢。

           增加新节点时,因为插入是有序的,所以应该进行尾插。

     //添加元素
        public boolean add(T e) {
            Node newNode = new Node(e,null,null);
            if(size == 0){
                tail = head = newNode;
            }else {
                //新节点的直接前驱
                newNode.pre = tail;
                //新节点的直接后继
                newNode.next = null;
                //更新tail结点
                tail.next = newNode;
                tail = newNode;
            }
            size++;
            return true;
        }
    

      获取元素:

        public T get(int index){
            Node tmp = head;
            if(index<0 || index>=size){
                throw new RuntimeException("参数不合法"+index);
            }else {
                for (int i = 0; i < index ; i++) {
                    tmp = tmp.next;
                }
            }
            return (T)tmp.data;
        }

         链表的查找效率低,为了提高查找的效率,分两部分进行查找元素,当将元素的链表分为两部分,当查找的元素靠近头,从头开始查找,反之从尾部开始查找。

    改进如下:

        /**
         * 获取元素,提高查找效率
         * @param index
         * @return
         */
        public T get(int index){
            if(index<0 || index>=size){
                throw new RuntimeException("参数不合法"+index);
            }
            Node tmp = null;
            if(index <= (size>>1)){  //size>>1 相当于除以2
                tmp = head;
                for (int i = 0; i < index ; i++) {
                    tmp = tmp.next;
                }
            }else {
                    tmp = tail;
                    for (int i = size-1; i > index ; i--) {
                        tmp = tmp.pre;
                    }
                }
            return (T)tmp.data;
        }
    

      删除元素:

                                                   (删除元素1)

        public T remove(int index){
            Node tmp = head;
            if(index<0 || index>=size){
                throw new RuntimeException("参数不合法"+index);
            }else {
                for (int i = 0; i < index ; i++) {
                    tmp = tmp.next;
                }
            }
            //第一个节点
            if (tmp == head){
                Node temp = head.next;
                head = temp;
            //最后一个节点
            }else if(tmp == tail) {
                Node temp = tmp.pre;
                tail = temp;
                //中间节点
            } else {
                //该节点的前一个结点
                Node up = tmp.pre;
                //该节点的后一个节点
                Node down = tmp.next;
                //串接
                up.next = down;
                down.pre = up;
            }
            size--;
            return (T)tmp.data;
        }
    

      代码如下:

     public class MyLinkList<T> {
        private  Node  head; //头节点
        private  Node  tail; //尾节点
        private int size;   //节点个数
    
        public MyLinkList(){
        this.head = new Node((T)new Object(),null,null);
        }
        //结点Node类
        class Node{
            private Object data;//存储的元素
            private Node  pre; //前驱节点
            private Node  next; //后继节点
    
            public Node(T value, Node pre, Node next) {
                data = value;
                this.pre = pre;
                this.next = next;
            }
        }
    
        //添加元素
        public boolean add(T e) {
            Node newNode = new Node(e,null,null);
            if(size == 0){
                tail = head = newNode;
            }else {
                //新节点的直接前驱
                newNode.pre = tail;
                //新节点的直接后继
                newNode.next = null;
                //更新tail结点
                tail.next = newNode;
                tail = newNode;
            }
            size++;
            return true;
        }
    
    //    //获取元素
    //    public T get(int index){
    //        Node tmp = head;
    //        if(index<0 || index>=size){
    //            throw new RuntimeException("参数不合法"+index);
    //        }else {
    //            for (int i = 0; i < index ; i++) {
    //                tmp = tmp.next;
    //            }
    //        }
    //        return (T)tmp.data;
    //    }
    
        /**
         * 获取元素,提高查找效率
         * @param index
         * @return
         */
        public T get(int index){
            if(index<0 || index>=size){
                throw new RuntimeException("参数不合法"+index);
            }
            Node tmp = null;
            if(index <= (size>>1)){  //size>>1 相当于除以2
                tmp = head;
                for (int i = 0; i < index ; i++) {
                    tmp = tmp.next;
                }
            }else {
                    tmp = tail;
                    for (int i = size-1; i > index ; i--) {
                        tmp = tmp.pre;
                    }
                }
            return (T)tmp.data;
        }
    
        //元素的个数
        public int size() {
            return size;
        }
    
        // 删除元素
        public T remove(int index){
            Node tmp = head;
            if(index<0 || index>=size){
                throw new RuntimeException("参数不合法"+index);
            }else {
                for (int i = 0; i < index ; i++) {
                    tmp = tmp.next;
                }
            }
            //第一个节点
            if (tmp == head){
                Node temp = head.next;
                head = temp;
            //最后一个节点
            }else if(tmp == tail) {
                Node temp = tmp.pre;
                tail = temp;
                //中间节点
            } else {
                //该节点的前一个结点
                Node up = tmp.pre;
                //该节点的后一个节点
                Node down = tmp.next;
                //串接
                up.next = down;
                down.pre = up;
            }
            size--;
            return (T)tmp.data;
        }
    
        public static void main(String[] args) {
            MyLinkList<Integer> myLinkList = new MyLinkList();
            myLinkList.add(7);
            myLinkList.add(1);
            myLinkList.add(2);
            myLinkList.add(3);
            myLinkList.add(6);
            myLinkList.add(4);
            myLinkList.add(10);
            System.out.println("原长度:"+myLinkList.size());
            System.out.println("原链表:");
            for (int i = 0; i < myLinkList.size(); i++) {
                System.out.print(myLinkList.get(i)+" ");
            }
            System.out.println();
            System.out.println("删除的元素:"+myLinkList.remove(6));
            System.out.println("删除的元素:"+myLinkList.remove(0));
            System.out.println("后长度:"+myLinkList.size());
            System.out.println("删除后链表:");
            for (int i = 0; i < myLinkList.size(); i++) {
                System.out.print(myLinkList.get(i)+" ");
            }
            System.out.println();
    
    
        }
    
    }
    

      

     运行如下:

  • 相关阅读:
    学电脑入门与提高
    Autodesk Revit Architecture 2014官方标准教程
    Photoshop移动UI界面设计实用教程 全彩超值版
    中文版SQL Server 2000开发与管理应用实例
    Excel2016数据处理与分析从新手到高手
    云计算(第三版)
    无线局域网搭建与管理
    R语言数据挖掘
    Spring 3.0就这么简单
    SVN使用教程总结
  • 原文地址:https://www.cnblogs.com/128-cdy/p/12288532.html
Copyright © 2011-2022 走看看