單向鏈表(2021-11-12)

package packageA;

interface ILink<A> {
    public void add(A data); // 增加節(jié)點

    public int getLength(); // 獲取節(jié)點個數(shù)

    public boolean isEmpty(); // 判斷鏈表是否為空

    public Object[] toArray(); // 鏈表轉(zhuǎn)化為數(shù)組

    public A getData(int index); // 根據(jù)序號獲取數(shù)據(jù)

    public void setData(int index, A data); // 根據(jù)序號賦值

    public boolean contains(A data); // 判斷數(shù)據(jù)是否在鏈表中

    public void remove(A data); // 刪除節(jié)點

    public void clean(); // 清空鏈表
}

class LinkImpl<A> implements ILink<A> {
    class Node { // 將節(jié)點設(shè)為內(nèi)部類
        private A data;
        private Node next = null;

        public Node(A data) {
            this.data = data;
        }
    }

    private Node root = null;
    private int length = 0;
    private Object[] array = null;

    public void add(A data) {
        Node newNode = new Node(data);
        this.length++;

        if (this.root == null) {
            this.root = newNode;
        } else {
            Node temp = this.root;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = newNode;
        }
    }

    public int getLength() {
        return this.length;
    }

    public boolean isEmpty() {
        return this.root == null;
    }

    public Object[] toArray() {
        if (this.isEmpty()) {
            return null;
        } else {
            array = new Object[this.length];
            Node temp = this.root;
            int counter = 0;
            while (temp != null) {
                array[counter++] = temp.data;
                temp = temp.next;
            }
            return array;
        }
    }

    public A getData(int index) {
        Node temp = this.root;
        while (index != 0) {
            temp = temp.next;
            index--;
        }
        return temp.data;
    }

    public void setData(int index, A data) {
        Node temp = this.root;
        while (index != 0) {
            temp = temp.next;
            index--;
        }
        temp.data = data;
    }

    public boolean contains(A data) {
        Node temp = this.root;
        while (temp != null) {
            if (data.equals(temp.data)) {
                return true;
            } else
                temp = temp.next;
        }
        return false;
    }

    public void remove(A data) {
        if (this.root == null) {
            return;
        }
        if (data.equals(this.root.data)) {
            root = root.next;
        }
        Node previous = this.root;
        Node temp = this.root.next;
        while (temp != null) {
            if (data.equals(temp.data)) {
                previous.next = temp.next;
                this.length--;
                temp = null;
            } else {
                previous = temp;
                temp = temp.next;
            }
        }
    }

    public void clean() {
        this.root = null;
        this.length = 0;
    }

}

public class Main {
    public static void main(String[] args) {
        ILink<String> link = new LinkImpl<String>();
        System.out.println("[當(dāng)前節(jié)點個數(shù)]" + link.getLength() + " 、[鏈表是否為空]" + link.isEmpty());
        link.add("你好");
        link.add("世界");
        link.add("我是戰(zhàn)士k");
        System.out.println("——————————轉(zhuǎn)化為數(shù)組,然后輸出——————————");
        for (Object data : link.toArray()) {
            System.out.println(data);
        }
        System.out.println("[當(dāng)前節(jié)點個數(shù)]" + link.getLength() + " 、[鏈表是否為空]" + link.isEmpty());
        System.out.println();
        System.out.println("[2號節(jié)點數(shù)據(jù)]" + link.getData(2));
        link.setData(2, "我是fighterk");
        System.out.println("[修改后的2號節(jié)點數(shù)據(jù)]" + link.getData(2));
        System.out.println("[包含“世界”嗎]" + link.contains("世界"));
        System.out.println("[包含“world”嗎]" + link.contains("world"));
        link.remove("世界");
        System.out.println("——————————刪除了“世界”之后的鏈表——————————");
        for (Object data : link.toArray()) {
            System.out.println(data);
        }
        link.clean();
        System.out.println("[清空的鏈表isEmpty]"+link.isEmpty());
        
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ![Flask](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAW...
    極客學(xué)院Wiki閱讀 7,832評論 0 3
  • 不知不覺易趣客已經(jīng)在路上走了快一年了,感覺也該讓更多朋友認(rèn)識知道易趣客,所以就謝了這篇簡介,已做創(chuàng)業(yè)記事。 易趣客...
    Physher閱讀 3,821評論 1 2
  • 雙胎妊娠有家族遺傳傾向,隨母系遺傳。有研究表明,如果孕婦本人是雙胎之一,她生雙胎的機率為1/58;若孕婦的父親或母...
    鄴水芙蓉hibiscus閱讀 3,914評論 0 2
  • 晴天,擁抱陽光,擁抱你。雨天,想念雨滴,想念你。 我可以喜歡你嗎可以啊 我還可以喜歡你嗎可以,可是你要知道我們不可...
    露薇霜凝閱讀 1,360評論 1 2

友情鏈接更多精彩內(nèi)容