1. 程式人生 > >Java單鏈表實現

Java單鏈表實現

package com.datastruct;

public class ListNode {

    Node head;
    int length;

    // 單鏈表長度
    public int size() {
        return length;
    }

    // 頭插法
    public void addNodeHead(Node node) {
       node.next = head;
       head = node;
       length++;
    }

    // 尾插法
    public void addNodeTail(Node node) {
        // 也可以使用長度來判斷
        if (head == null) {
            head = node;
        } else {
            Node n = head;
            while (n.next != null) {
                n = n.next;
            }
            n.next = node;
        }
        length++;
    }

    // 在指定位置上插入節點
    public void addNode(int index, Node node) {
        if (index < 0 || index > length) {
            throw new IllegalArgumentException();
        }
        Node n = head;
        Node previous = head;
        int pos = 0;
        while (pos != index) {
            previous = n;
            n = n.next;
            pos++;
        }
        if (n == head) {
            node.next = head;
            head = node;
        } else {
            node.next = n;
            previous.next = node;
        }
        length++;
    }
    // 查詢指定位置節點
    public Node findNodeByIndex(int index) {
        Node n = head;
        int pos = 0;
        while (pos != index) {
            n = n.next;
            pos++;
        }
        return n;
    }
    // 獲得所有節點
    public void getAllNodes() {
        Node n = head;
        StringBuilder sb = new StringBuilder();
        while (n != null) {
            sb.append(n.data).append(" ");
            n = n.next;
        }
        System.out.println(sb.toString());
    }

     // 刪除指定位置節點
    public void delNodeByIndex(int index) {
        if (index < 0 || index > length) {
            throw new IllegalArgumentException();
        }
        Node n = head;
        Node previous = head;
        int pos = 0;
        while (pos != index) {
            previous = n;
            n = n.next;
            pos++;
        }
        if (n == head) {
            head = head.next;
        } else {
            previous.next = n.next;
        }
        length--;
    }    
   
    // 清空單鏈表
    public void clear() {
        length = 0;
        head = null;
    }
}

package com.datastruct;

public class Node {

    int data;
    Node next;

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

    @Override
    public String toString() {
        return  "data=" + data ;
    }
}

class TestLinked1 {
    public static void main(String[] args) {
        ListNode list = new ListNode();
        list.addNodeTail(new Node(1));
        list.addNodeTail(new Node(2));
        list.addNodeTail(new Node(3));
        list.addNodeTail(new Node(4));

        // 查詢指定節點
        list.getAllNodes();
        Node node = list.findNodeByIndex(0);
        System.out.println(node.data);

        // 頭插法
        //list.addNodeHead(new Node(7));

        // 指定位置插入節點
        list.getAllNodes();
        list.addNode(4,new Node(8));
        list.getAllNodes();

        list.delNodeByIndex(4);
        list.getAllNodes();

        list.clear();
        list.getAllNodes();
    }
}