單鏈表的創建及增刪查驗

1.單鏈表的初始化

單鏈表是由節點構成的,所以,在創建單鏈表時,我們首先應該創建構成單鏈表的節點

class ListNode{
    public int val;
    public ListNode next;
    public ListNode(value){
        this.val = value;
    }
}

這裏我們設定了節點類,其中val用於存儲節點值,next是指向下一個ListNode的引用(c裏面稱爲指針),構造函數初始化對象的值。

然後,我們構造單鏈表,在單鏈表中,我們設定一個哨兵節點指向頭節點方便我們進行操作,所以在鏈表類中,我們要初始化哨兵節點。

public class LinkedList{
    public LinkedList(){
        ListNode dummy = new ListNode(-1);
    }
}

這樣,我們的初始化就完成了。

後面,我們需要將對應的方法寫入鏈表類中。

2.增加節點

public class LinkedList{
    public LinkedList(){
        ListNode dummy = new ListNode(-1);
    }

    public void add(int location, int val){
        ListNode pre = dummy; //初始時指向哨兵節點
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        ListNode node = new ListNode(val);
        node.next = pre.next;
        pre.next = node;
    }
}

3.刪除節點

public class LinkedList{
    public LinkedList(){
        ListNode dummy = new ListNode(-1);
    }

    public void add(int location, int val){
        ListNode pre = dummy; //初始時指向哨兵節點
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        ListNode node = new ListNode(val);
        node.next = pre.next;
        pre.next = node;
    }

    public void remove(int location){
        ListNode pre = dummy; //初始時指向哨兵節點
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        pre.next = pre.next.next;
    }
}

這裏刪除節點我們不考慮location超過鏈表長度或鏈表空無法繼續刪除的情況,以簡化操作。

4.查詢節點

public class LinkedList{
    public LinkedList(){
        ListNode dummy = new ListNode(-1);
    }

    public void add(int location, int val){
        ListNode pre = dummy; //初始時指向哨兵節點
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        ListNode node = new ListNode(val);
        node.next = pre.next;
        pre.next = node;
    }

    public void remove(int location){
        ListNode pre = dummy; //初始時指向哨兵節點
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        pre.next = pre.next.next;
    }

    public int get(int location){
        ListNode cur = dummy.next; //初始時指向哨兵節點
        for(int i = 0; i < location; i++){
            cur = cur.next;
        }
        return cur.val;
    }
}

5.驗證是否包含某節點

public class LinkedList{
    public LinkedList(){
        ListNode dummy = new ListNode(-1);
    }

    public void add(int location, int val){
        ListNode pre = dummy; //初始時指向哨兵節點
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        ListNode node = new ListNode(val);
        node.next = pre.next;
        pre.next = node;
    }

    public void remove(int location){
        ListNode pre = dummy; //初始時指向哨兵節點
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        pre.next = pre.next.next;
    }

    public int get(int location){
        ListNode cur = dummy.next; //初始時指向哨兵節點
        for(int i = 0; i < location; i++){
            cur = cur.next;
        }
        return cur.val;
    }

    public boolean contain(int val){
        ListNode head = dummy.next; //初始時指向哨兵節點
        while(head != null){
            if(head.val = val){
                return True;
            }
            head = head.next;
        }
        return False;
        
    }
}

6.設置打印鏈表的函數

public class LinkedList{
    public LinkedList(){
        ListNode dummy = new ListNode(-1);
    }

    public void add(int location, int val){
        ListNode pre = dummy; //初始時指向哨兵節點
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        ListNode node = new ListNode(val);
        node.next = pre.next;
        pre.next = node;
    }

    public void remove(int location){
        ListNode pre = dummy; //初始時指向哨兵節點
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        pre.next = pre.next.next;
    }

    public int get(int location){
        ListNode cur = dummy.next; //初始時指向哨兵節點
        for(int i = 0; i < location; i++){
            cur = cur.next;
        }
        return cur.val;
    }

    public boolean contain(int val){
        ListNode head = dummy.next; //初始時指向哨兵節點
        while(head != null){
            if(head.val = val){
                return True;
            }
            head = head.next;
        }
        return False;
        
    }
    public void print(){
        ListNode head = dummy.next;
        while(head != null){
            System.out.print(head.val + "->");
            head = head.next;
        }
        System.out.println("null");
    }
}

 

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