zoukankan      html  css  js  c++  java
  • LinkedList底层源码实现

    /**
     * Created by chengbx on 2018/5/18.
     * LinkedList其实也就是我们在数据结构中的链表,这种数据结构有这样的特性:
     * 分配内存空间不是必须是连续的;
     * 插入、删除操作很快,只要修改前后指针就OK了,时间复杂度为O(1);
     * 访问比较慢,必须得从第一个元素开始遍历,时间复杂度为O(n);
     */
    public class CbxLinkedList {
        private Node first;
        private Node last;
        private int size;
    
        public void add(Object object){
            //如果是第一个节点 那么在创建之后 第一个节点 也是最后一个节点
            Node n = new Node();
            if(first == null){
                n.setPrevious(null);
                n.setObject(object);
                n.setNext(null);
                first = n;
                last = n;
            }else{
                //直接往last后面添加新的节点
                n.setPrevious(last);
                n.setObject(object);
                n.setNext(null);
                //新增的节点为最后一个节点
                last.setNext(n);
                last = n;
            }
            size++;
        }
        public Object get(int index){
            RangeCheck(index);
            Node temp = getNodeByindex(index);
            return temp.getObject();
        }
        public int size(){
            return size;
        }
    
        private void RangeCheck(int index){
            if(index >= size){
                try {
                    throw new Exception();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        public Node getNodeByindex(int index){
            Node temp = null;
            if(first!=null){
                temp = first;
                for(int i =0;i<index;i++){
                    temp = temp.getNext();
                }
            }
            return temp;
        }
        public void remove(int index){
            Node temp = getNodeByindex(index);
            Node preNode = temp.getPrevious();
            Node nextNode = temp.getNext();
            preNode.setNext(nextNode);
            nextNode.setPrevious(preNode);
            size--;
        }
        public void add(int index,Object object){
            //获取对应索引的节点
            Node temp = getNodeByindex(index);
            if(temp!=null){
                Node preNode = temp.getPrevious();
    
                Node newNode = new Node();
                newNode.setPrevious(preNode);
                newNode.setObject(object);
                newNode.setNext(temp);
    
                preNode.setNext(newNode);
                temp.setPrevious(newNode);
            }
            size++;
        }
    
        @Test
        public void test(){
            CbxLinkedList cbxLinkedList = new CbxLinkedList();
            cbxLinkedList.add("aaa");
        }
    
    }
    class Node{
        private Node previous;
        private Object object;
        private Node next;
    
        public Node getPrevious() {
            return previous;
        }
    
        public void setPrevious(Node previous) {
            this.previous = previous;
        }
    
        public Object getObject() {
            return object;
        }
    
        public void setObject(Object object) {
            this.object = object;
        }
    
        public Node getNext() {
            return next;
        }
    
        public void setNext(Node next) {
            this.next = next;
        }
    
        public Node() {
        }
    
        public Node(Node previous, Object object, Node next) {
            this.previous = previous;
            this.object = object;
            this.next = next;
        }
    }
  • 相关阅读:
    045_List view button
    044_Schedule Job 间隔时间自动执行
    043_关于Salesforce集中权限的解释
    041_Record Type
    040_Field Set
    039_External Data Source(转载)
    037_Clone Button
    036_在salesforce中更换自定义app的logo
    020_Json的使用
    033_SFDC-Apex案例整理-未完
  • 原文地址:https://www.cnblogs.com/cbxBlog/p/9126332.html
Copyright © 2011-2022 走看看