Java - 單鏈表


鏈表是一種常見的基礎數據結構,是一種有序的列表,但不會按照線性順序存儲數據,而是在每一個節點里存儲下一個節點的指針(next)。鏈表適合插入、刪除,不宜過長,否則會導致遍歷性能下降。

  • 以節點方式存儲;
  • 每個節點包含data域,next域:指向下一個節點;
  • 鏈表的各個節點不一定是連續存儲的;

代碼實現:

節點類

public class HeroNode {

    protected Integer no;
    protected String name;
    protected HeroNode next;

    public HeroNode(Integer no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "HeroNode[" +
                "no=" + no +
                ", name='" + name + '\'' +
                ']';
    }
}

 

SingleLinkedList

public class SingleLinkedList {

    private HeroNode root;
    private Integer size = 0;

    /**
     * 添加至鏈表頭
     * @param hero
     */
    public void addFirst(HeroNode hero) {
        if (root == null) {
            root = hero;
        } else {
            hero.next = root;
            root = hero;
        }
        size++;
    }

    /**
     * 添加至鏈表尾
     * @param hero
     */
    public void addLast(HeroNode hero) {
        if (root == null) {
            root = hero;
        } else {
            HeroNode temp = root;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = hero;
        }
        size++;
    }

    /**
     * 按照某屬性的順序添加
     * @param hero
     */
    public void addByOrder(HeroNode hero) {
        if (root == null) {
            root = hero;

        } else {
            HeroNode tmp = root;
            // 新節點比頭節點小
            if (hero.no < tmp.no) {
                root = hero;
                root.next = tmp;
                return;
            }
            // 找到next節點編號大於等於新節點的編號的節點,該節點與它的next節點之間就是新節點所在位置,
            // 等於時不添加,控制台進行提示
            while (tmp.next != null && tmp.next.no < hero.no) {
                tmp = tmp.next;
            }
            if (tmp.next == null) {
                tmp.next = hero;
                return;
            }

            if (tmp.next.no.equals(hero.no)) {
                System.out.println("編號為" + hero.no + "已存在");
                return;
            }
            hero.next = tmp.next;
            tmp.next = hero;
        }
        size++;
    }

    /**
     * 修改
     * @param hero
     */
    public void modify(HeroNode hero) {
        HeroNode tmp = root;
        while(tmp !=null) {
            if (tmp.no.equals(hero.no)) {
                tmp.name = hero.name;
                break;
            }
            tmp = tmp.next;
        }
    }

    /**
     * 根據編號獲取節點
     * @param no
     * @return
     */
    public HeroNode query(int no) {
        HeroNode tmp = root;
        while (tmp != null) {
            if (tmp.no.equals(no)) {
                return tmp;
            }
            tmp = tmp.next;
        }
        return null;
    }

    /**
     * 根據編號刪除節點
     * @param no
     */
    public void remove(int no) {
        if (root == null) {
            return;
        }
        //根節點
        if (no == root.no) {
            if (null != root.next) {
                root = root.next;
            } else {
                root = null;
            }
            size--;
            return;
        }

        // 非根節點
        HeroNode temp = root;
        while (temp.next != null) {
            if (no == temp.next.no) {
                break;
            }
            temp = temp.next;
        }
        // 節點不存在
        if (temp.next == null) {
            return;
        }
        temp.next = temp.next.next;
        size--;
    }

    /**
     * 打印
     */
    public void display() {
        if (root == null) {
            System.out.println("This SingleLinkedList is null.");
            return;
        }
        HeroNode temp = root;
        while(temp != null) {
            System.out.println(temp.toString());
            temp = temp.next;
        }
    }

    /**
     * 獲取鏈表長度
     * @return
     */
    public Integer getSize() {
        return size;
    }
}
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM