java實現單鏈表


早上清早起來,第一件事就是打開博客園隨便的翻看下博客, 看到有篇文章提到的算法數據結構,實現一個單鏈表的操作。作者是使用c#編寫的, 那么我這里通過java也來弄弄。

首先定義一個Node類

public class Node {
     protected Node next; //指針域  
     public  int data;//數據域  
       
     public Node( int data) {  
           this. data = data;  
     }  
       
     //顯示此節點  
     public void display() {  
          System. out.print( data + " ");  
     }  
}

接下來定義一個單鏈表,並實現相關方法:

public class LinkList {
    public Node first; // 定義一個頭結點
    private int pos = 0;// 節點的位置

    public LinkList() {
        this.first = null;
    }

    // 插入一個頭節點
    public void addFirstNode(int data) {
        Node node = new Node(data);
        node.next = first;
        first = node;
    }

    // 刪除一個頭結點,並返回頭結點
    public Node deleteFirstNode() {
        Node tempNode = first;
        first = tempNode.next;
        return tempNode;
    }

    // 在任意位置插入節點 在index的后面插入
    public void add(int index, int data) {
        Node node = new Node(data);
        Node current = first;
        Node previous = first;
        while (pos != index) {
            previous = current;
            current = current.next;
            pos++;
        }
        node.next = current;
        previous.next = node;
        pos = 0;
    }

    // 刪除任意位置的節點
    public Node deleteByPos(int index) {
        Node current = first;
        Node previous = first;
        while (pos != index) {
            pos++;
            previous = current;
            current = current.next;
        }
        if (current == first) {
            first = first.next;
        } else {
            pos = 0;
            previous.next = current.next;
        }
        return current;
    }

    // 根據節點的data刪除節點(僅僅刪除第一個)
    public Node deleteByData(int data) {
        Node current = first;
        Node previous = first; // 記住上一個節點
        while (current.data != data) {
            if (current.next == null) {
                return null;
            }
            previous = current;
            current = current.next;
        }
        if (current == first) {
            first = first.next;
        } else {
            previous.next = current.next;
        }
        return current;
    }

    // 顯示出所有的節點信息
    public void displayAllNodes() {
        Node current = first;
        while (current != null) {
            current.display();
            current = current.next;
        }
        System.out.println();
    }

    // 根據位置查找節點信息
    public Node findByPos(int index) {
        Node current = first;
        if (pos != index) {
            current = current.next;
            pos++;
        }
        return current;
    }

    // 根據數據查找節點信息
    public Node findByData(int data) {
        Node current = first;
        while (current.data != data) {
            if (current.next == null)
                return null;
            current = current.next;
        }
        return current;
    }
}

最后我們可以通過測試類來做相關測試:

public class TestLinkList {
    public static void main(String[] args) {  
        LinkList linkList = new LinkList();  
        linkList.addFirstNode(20);  
        linkList.addFirstNode(21);  
        linkList.addFirstNode(19);  
         //print19,21,20  
        linkList.add(1, 22); //print19,22,21,20  
        linkList.add(2, 23); //print19,22,23,21,20  
        linkList.add(3, 99); //print19,22,23,99,21,20  
      //調用此方法會print 19,22,23,99,21,20 
        linkList.displayAllNodes();  
        }
}

至此,對單鏈表的操作就筆記到這里了。


免責聲明!

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



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