1. 程式人生 > >自己實現數據結構---LinkedList

自己實現數據結構---LinkedList

AS ID class truct void 是否 param || stringbu

一.先上代碼:

1.方式一:

public class LinkedList<E> {

    //節點,用來存放數據:數據+下一個元素的引用
    private class Node{
        private E e;
        private Node next;
        public Node(E e,Node next){
            this.e = e;
            this.next = next;
        }
        public Node(E e){
            this(e,null);
        }
        
public Node(){ this(null,null); } public String toString(){ return e.toString(); } } private Node head; private int size; /** * 構造方法 */ public LinkedList(){ head = null; size = 0; } /** * 獲取鏈表中元素的個數 *
@return */ public int getSize(){ return size; } /** * 判斷鏈表是否為空 * @return */ public boolean isEmpty(){ return size == 0; } /** * 鏈表頭添加新元素 */ public void addFirst(E e){ Node node = new Node(); node.next = head; head
= node; size ++; } /** * 鏈表中間添加元素 * @param index * @param e */ public void add(int index,E e){ if (index < 0 || index > size){ throw new IllegalArgumentException("Add Failed"); } if (index == 0){ addFirst(e); }else { Node prev = head; for (int i = 0 ; i < index-1; i++){ prev = prev.next; } Node node = new Node(e); node.next = new Node(e); prev.next = node; } size++; } /** * 鏈表末尾添加元素 * @param e */ public void addList(E e){ add(size,e); } }

2.方式二:

package com.amazing.jdk.datastructure;

/**
 * Created by yaming on 18-6-21.
 */
public class LinkedListPlus<E> {

    //節點,用來存放數據:數據+下一個元素的引用
    private class Node{
        private E e;
        private Node next;
        public Node(E e,Node next){
            this.e = e;
            this.next = next;
        }
        public Node(E e){
            this(e,null);
        }
        public Node(){
            this(null,null);
        }
        public String toString(){
            return e.toString();
        }
    }

    private Node dummyHead;//虛擬頭節點
    private int size;
    public LinkedListPlus(){
        dummyHead = new Node(null,null);
        size = 0;
    }

    public void add(int index,E e){
        if (index < 0 || index > size){
            throw new IllegalArgumentException("Add Failed");
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node node = new Node(e);
        node.next = prev.next;
        prev.next = node;
        size++;
    }

    public void addFirst(E e){
        add(0,e);
    }

    public void addLast(E e){
        add(size,e);
    }

    public E get(int index){
        if (index < 0 || index > size){
            throw new IllegalArgumentException("Add Failed");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i <size ; i++) {
            cur = cur.next;
        }
        return cur.e;
    }

    public E getFirst(){
        return get(0);
    }

    public E getLast(){
        return get(size-1);
    }

    public void set(int index,E e){
        if (index < 0 || index > size){
            throw new IllegalArgumentException("Add Failed");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i <size ; i++) {
            cur = cur.next;
        }
        cur.e = e;
    }

    public boolean contains(E e){
        Node cur = dummyHead.next;
        while (cur != null){
            if (cur.e.equals(e)){
                return true;
            }
            cur = cur.next;
        }
        return false;

    }

    public E remove(int index){
        if (index < 0 || index > size){
            throw new IllegalArgumentException("Add Failed");
        }
        Node prev = dummyHead;
        for (int i = 0; i <index ; i++) {
            prev = prev.next;
        }
        Node retNode = prev.next;
        prev.next = retNode.next;
        retNode.next = null;
        size--;
        return retNode.e;
    }

    public E removeFirst(){
        return remove(0);
    }

    public E removeLast(){
        return remove(size-1);
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = dummyHead.next;
        while (cur != null){
            res.append(cur+"->");
            cur = cur.next;
        }
        res.append("null");
        return res.toString();
    }
}

自己實現數據結構---LinkedList