码迷,mamicode.com
首页 > 其他好文 > 详细

LinkedList

时间:2020-03-14 13:17:21      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:check   rem   获取   static   super   remove   lin   pac   span   

package TestList;

public class TestLinkedList {
    private Node first;
    private Node last;
    private int size;
    
    public int size() {
        return size;
    }
    
    //index越界
    private void checkRange(int index) {
        if(index<0||index>=size) {
            try{
                throw new Exception();
            }catch(Exception e) {
                e.printStackTrace();
            }
            
        }
    }
    
    //获取指定Index对应的节点
    public Node node(int index) {
        checkRange(index);
        Node temp=null;
        if(first!=null) {
            
            temp=first;
            for(int i=0;i<index;i++) {
                temp=temp.next;
            }
            
        }
        return temp;
    }
    
    //get
    public Object get(int index) {
        Node temp=node(index);
        if (temp!=null) {
            return temp.getObj();
        }
        return null;
        
        
    }
    
    
    //add
    public void add(Object obj) {
        Node n=new Node();
        if(first==null) {
            n.setPrevious(null);
            n.setObj(obj);
            n.setNext(null);    
            first=n;
            last=n;
        }else {
            n.setPrevious(last);
            n.setObj(obj);
            n.setNext(null);
            last.setNext(n);
            last=n;
        }
        size++;

    }
    //remove
    public void remove(int index) {
        Node temp=node(index);
        if(temp!=null) {
            //last节点
            Node lastNode=temp.previous;        
            //next节点
            Node nextNode=temp.next;
            
            lastNode.next=nextNode;
            nextNode.previous=lastNode;
            
        }
        size--;        
        
    }
    //add
    public void add(int index,Object obj) {
        checkRange(index);
        Node temp=node(index);
        
        Node newNode=new Node();
        newNode.obj=obj;
        
        if(temp!=null) {
            //上一个节点
            Node lastNode=temp.previous;
            //下一个节点
            Node nextNode=temp.next;
            
            lastNode.next=newNode;
            newNode.previous=lastNode;
            
            newNode.next=temp;
            temp.previous=newNode;
        }
        size++;

        
    }
    
    public static void main(String[] args) {
        TestLinkedList list=new TestLinkedList();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        System.out.println(list.size());
        System.out.println(list.get(1));
        //list.remove(1);
        //System.out.println(list.get(1));
        list.add(1,"xxx");
        System.out.println(list.get(1));
    }

}
package TestList;

public class Node {
    
    
        Node previous;
        Object obj;
        Node 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;
        }

        
        
        public Node() {
            
        }
        
        public Node(Node previous,Object obj,Node next) {
            super();
            this.previous=previous;
            this.obj=obj;
            this.next=next;
        }
        
}

 

LinkedList

标签:check   rem   获取   static   super   remove   lin   pac   span   

原文地址:https://www.cnblogs.com/hapyygril/p/12491408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!