java鏈表操作

本來該學習java鏈表源碼了,在學習源碼之前先複習一下java的鏈表知識
下面是java操作鏈表的幾個簡單例子:
先定義一個Node的類

public class Node {

    private int record;//變量
    private Node nextNode;//
    public Node(){

    }
    public Node(int record){
        this.record = record;
    }
    public int getRecord() {
        return record;
    }
    public void setRecord(int record) {
        this.record = record;
    }
    public Node getNextNode() {
        return nextNode;
    }
    public void setNextNode(Node nextNode) {
        this.nextNode = nextNode;
    }

}

自己寫的鏈表工具類

public class Linked {

    private static int count = 0;//用來記錄鏈表的長度
    public static void main(String [] args){
        //構造喲個含有元素的鏈表
        Node head = new Node();
        Node temp = null;//臨時節點
        Node cur = null;//當前結點
        for(int i=1;i<10;i++){
            temp = new Node(i);
            if(i != 1){//說明不是頭節點
                cur.setNextNode(temp);
            }else{//是頭節點
                head.setNextNode(temp);
            }
            cur = temp;
            count++;
        }

        Node before = head;
        while(before != null){
            System.out.println(before.getRecord());
            before = before.getNextNode();
        }
        System.out.println("count"+count);
    }

    //向鏈首添加一個元素
    public static Node AddToHead(Node head,int value){
        Node newNode = new Node(value);
        if(null == head)//當傳進來的head爲空也就是說鏈表不存在
        {
            head = newNode;//新生成的節點作爲頭節點
            return head;
        }

        newNode.setNextNode(head.getNextNode());//把頭節點之後的第一個節點賦值給newNode
        head.setNextNode(newNode);
        count++;
        return head;
    }
    //向鏈尾添加一個元素
    public static Node AddToTail(Node head,int value){
        Node newNode = new Node(value);
        if(null == head){
            head = newNode;
            return head;
        }
        //遍歷到最後一個節點
        while(head.getNextNode()!= null)
        {
            head = head.getNextNode();
        }
        //把最後的一個節點的元素置爲newNode
        head.setNextNode(newNode);
        count++;
        return head;
    }


    //刪除一個元素,並返回所刪除元素的data
    public static int remove(Node head,int index){
        if(null == head)return -1;
        int counts = 0;
        while(head.getNextNode()!= null){
            ++counts;
            if(counts == index)//跳出循環時指針指向index-1的位置
                break;
            head = head.getNextNode();
        }
        //此時head是要刪除元素的前一個元素
        if(counts < index)//說明鏈表的長度小於index
            return -1;
        int elem = head.getNextNode().getRecord();
        head.setNextNode(head.getNextNode());
        count --;
        return elem;
    }

    //倒着輸出鏈表,兩種方式1:先把鏈表反轉,在輸出 2:把值存入棧中。採用第二種
    public static Stack<Integer> printReverse(Node head){
        Stack<Integer> nodeStack = new Stack<Integer>();
        if(head == null){
            nodeStack.push(-1);
            return nodeStack;
        }
        while(head.getNextNode()!= null){
            nodeStack.push(head.getNextNode().getRecord());
            head = head.getNextNode();
        }
        return nodeStack;
    }

    //反轉鏈表
    public static Node Reverse(Node head){
        if(null == head){
            return head;
        }
        Node pre = head;
        Node cur = head.getNextNode();
        Node next;
        while(null != cur){
            next = cur.getNextNode();
            cur.setNextNode(pre);
            pre = cur;
            cur = next;
        }
        head.setNextNode(null);
        head = pre;
        return head;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章