자바 링크 데이터 구조 구현

package linearlist;


import sortalgorthims.Tool;
/**
 *      Java          。
 * @author Stephen Huge
 */
public class LinkList<T> {
    /**
     * LinkList      Node,          T     data Node     next。
     */
    public class Node {
        private T data;
        private Node next;
        public Node() { }
        /**
         * LinkList   Node       
         */
        public Node(T data, Node next) {
            this.data = data;
            this.next = next;
        }
    }

    /**         */
    private Node header;
    /**        */
    private Node tail;
    /**      */
    private int size;

    /**
     *       
     */
    public LinkList() {
        header = null;
        tail = null;
    }
    /**
     *       ,    element          ,       element
     *     ,           。
     */
    public LinkList(T element) {
        header = new Node(element, null);
        tail = header;
        size++;
    }
    /**
     *          
     */
    public int length() {
        return size;
    }
    /** 
     *       index   
     */
    public T get(int index) {
        return getNodeByIndex(index).data;
    }

    private Node getNodeByIndex(int index) {
        if(index < 0 || index > size - 1){
            throw new IndexOutOfBoundsException("     ");
        }
        Node current = header;
        for(int i = 0; i < size && current != null; i++, current = current.next){
            if(i == index) {
                return current;
            }   
        }
        return null;
    }

    /**
     *   element      ,-1     
     */
    public int locate(T element) {
        Node current = header;
        for(int i = 0; i < size && current != null; i++, current = current.next){
            if(current.data == element) {
                return i;
            }    
        }
        return -1;
    }

    /**
     *  index      element
     */
    public void insert(T element, int index) {
        if(index < 0 || index > size) {
            throw new IndexOutOfBoundsException("       ");
        }
        if(header == null) {
            add(element);
        }else{
            if(index == 0) {
                addAtHeader(element);
            }else{
                Node prev = getNodeByIndex(index - 1);
                prev.next = new Node(element, prev.next);
                size++;
            }
        }
    }
    /**
     *              
     */
    private void add(T element) {
        if(header == null) {
            header = new Node(element, null);
            tail = header;
        }else{
            Node newNode = new Node(element, null);
            tail.next = newNode;
            tail = newNode;
        }
        size++;
    }
    /**
     *              
     */
    private void addAtHeader(T element) {
            header = new Node(element, header);
            if(tail == null){
                tail = header;
            }           
            size++;
    }
    /**
     *   index     
     */
    public T delete(int index) {
        if(index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException("       ");
        }
        Node del = null;
        if(index == 0) {
            del = header;
            header = header.next;           
        }else{
            Node pre = getNodeByIndex(index - 1);
            del = pre.next;
            pre.next = del.next;
            del.next = null;            
        }
        size--;
        return del.data;
    }
    /**
     *            
     */
    public T remove() {     
        return delete(size - 1);
    }

    /**
     *         
     */
    public boolean isEmpty() {
        return size==0;
    }

    /**
     *     
     */
    public void clear() {
        header = null;
        tail = null;
        size = 0;
    }
    public String toString() {
        if(isEmpty()) {
            return "[]";
        }else{
            StringBuilder sb = new StringBuilder("[");
            for(Node current = header; current != null; current = current.next){
                sb.append(current.data.toString() + ", ");
            }
            int len = sb.length();
            return sb.delete(len - 2, len).append("]").toString();
        }

    }

    public static void main(String[] args) {
        LinkList list = new LinkList();
        list.insert("aaa", 0);
        list.add("bbb");
        list.add("ccc");
        list.add("ddd");
        Tool.print(list.toString());
        list.insert("eee", 1);
        Tool.print(list.toString());
        list.delete(3);
        Tool.print(list.toString());
        System.out.println("ccc       :"+list.locate("ccc"));  
        System.out.println("     2    :"+list.get(2));  
    }
}

실행 결과:
[aaa, bbb, ccc, ddd]
[aaa, eee, bbb, ccc, ddd]
[aaa, eee, bbb, ddd]
ccc-1
     2    :bbb

참조 사이트 주소:http://blog.csdn.net/caiwenfeng_for_23/article/details/8496029

좋은 웹페이지 즐겨찾기