java 實現單鏈表

c語言中鏈表需要自己實現,而java提供好了LinkedList供調用,閒來無事上手用JAVA寫一個,菜鳥一枚,不當不規範之處希望各位大佬指正^_^

 

先創建一個節點的模板類

class Node{/* 創建鏈表子節點模板*/
    public String name;//節點數據示例
    public String sex;//性別
    public int age;//年齡
    public int score;//總分
    public Node next = null;//下個節點指針
    public Node(String name,String sex,int age,int score){
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.score = score;
    }
}

創建鏈表操作方法類,提供鏈表操作方法

/*鏈表訪問控制類*/
class PacketNode{
    public Node head = null;//頭節點
    public Node last = null;//尾節點
/*構造方法存儲鏈表頭節點*/
    public PacketNode(Node head)

/*添加節點到表尾*/
    public boolean AddEnd(Node node)

/*插入節點到指定位置*/
    public  boolean Add(Node node,int place)

/*刪除指定位置節點*/
    public boolean Delete(int place)

/*遍歷節點,返回長度*/
    public int Length()

/*輸出所有數據*/
    public void PrintAll()
}

具體實現步驟和調用方法:

/*******************************************************************
* Java實現鏈表數據結構
* 用法示例:----------
* PacketNode node = new PacketNode(new Node("List_head"));
 *
 * //創建頭節點-創建一條鏈表,第一個數據爲"List_head"
* node.AddEnd(new Node("666"));//到表尾插入字符串"666"
* node.Add(new Node("inset"),5);//在第5個(0-5)元素位置插入數據"inset"
* node.Delete(2);//刪除第2個(0-2)元素
* *******************************************************************/
/**/
class Node{/* 創建鏈表子節點模板*/
    public String name;//節點數據示例
    public String sex;//性別
    public int age;//年齡
    public int score;//總分
    public Node next = null;//下個節點指針
    public Node(String name,String sex,int age,int score){
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.score = score;
    }
}
/*鏈表訪問控制類*/
class PacketNode{
    public Node head = null;//頭節點
    public Node last = null;//尾節點
    /*構造方法存儲鏈表頭節點*/
    public PacketNode(Node head){
        this.head = head;
    }
    /*添加節點到表尾*/
    public boolean AddEnd(Node node){
        Node temp = this.head;
        while (temp.next != null){//遍歷至尾
            temp = temp.next;//下一個節點
        }
        temp.next = node;//連接尾節點
        last = node;//尾節點更新
        return true;
    }
    /*插入節點到指定位置*/
    public  boolean Add(Node node,int place){
        Node temp = this.head;
        int count = 0;
        if (place<0) {
            System.out.println("\n插入失敗,必須是正整數..^_^..");
            return false;
        }else if (place == 0){//如果插入的是第一個節點位置
            node.next = this.head;//node的next指向第一個節點
            this.head = node;//首節點給node
            return true;
        }
        while (temp.next != null){//遍歷至尾
            if (count == place-1){//找到插入位置前一節點
                node.next = temp.next;//把後一節點指針給要插入的node
                temp.next = node;//連接node到前一節點
                return true;
            }
            temp = temp.next;//下一個節點
            count++;
        }
        if (count == place-1){//插入的是尾節點
            temp.next = node;
            last = node;//尾節點更新
            return true;
        }
        System.out.println("\n插入失敗,超範圍啦..^_^..");
        return false;
    }
    /*刪除指定位置節點*/
    public boolean Delete(int place){
        Node temp = this.head;//
        int count = 0;
        int length = Length()-1;//長度按1---計數
        if (length < place || place < 0){
            System.out.println("\n該刪除位置超過表的範圍!!\n");
            return false;
        }else if (place == 0){//如果刪除的是第一個節點位置
            this.head = temp.next;
            temp.next = null;
            return true;
        }else {
            while (temp.next != null) {
                if (count == place-1){//找到刪除位置前一節點
                    temp.next = temp.next.next;//後後節點指針給前節點的next
                    return true;
                }
                temp = temp.next;//下一個節點
                count++;
            }
            last = temp;//尾節點緩存
        }
        return false;
    }
    /*遍歷節點,返回長度*/
    public int Length() {
        int length = 1;
        Node temp = this.head;//交換頭結點指針
        while (temp.next != null){
             temp = temp.next;//下一個節點
            length++;
        }
        return length;
    }
    /*輸出所有數據*/
    public void PrintAll(){
        Node temp = this.head;//new Node();
        System.out.println("姓名 | 性別 | 年齡 | 總分");
        while (temp.next != null){
            System.out.println(temp.name+"  "+temp.sex+"  "+temp.age+"  "+temp.score+"  ");
            temp = temp.next;//下一個節點
        }
        System.out.println(temp.name+"  "+temp.sex+"  "+temp.age+"  "+temp.score+"  ");
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章