單鏈表的讀取,刪除,插入,遍歷操作


鏈表是由指針把若干個節點連接成鏈狀結構。其中每個節點由存儲數據和指想下一個節點的指針構成。鏈表具有創建節點、插入節點、刪除節點等操作。

創建節點

在鏈表末尾創建節點

 void add(int x){ //將x加入鏈表最後
        Node pre, p, q;
        pre = head;
        p = head.next;
        while (p != null){
            pre = pre.next;
            p = p.next;
        }
        q = new Node();
        q.data = x;
        pre.next = q;
    }

插入節點

將數據加入升序鏈表

void add2(int x){ //將x加入升序鏈表
        Node pre, p ,q;
        pre = head;
        p = head.next;
        while (p != null){
            if (p.data > x)
                break;
            pre = pre.next;
            p = p.next;
        }
        q = new Node();
        q.data = x;
        q.next = p;
        pre.next = q;
    }

在指定位置插入數據

void inset(int index, int ins){ //插入,在索引index位置,插入ins值
        Node pre, p ,q;
        pre = head;
        p = head.next;
        while (index-- >1 && p != null ){
            pre = pre.next;
            p = p.next;
        }
        if (p == null){
            System.out.println("輸入索引大於鏈表長度");
        }else {
            q = new Node();
            q.data = ins;
            q.next = p;
            pre.next = q;
        }
    }

刪除節點

void delete(int x){ //刪除鏈表中的某個值(只能刪除第一次出現)
        Node pre, p;
        pre = head;
        p = head.next;
        while (p != null){
            if (p.data == x ){
                if (p.next != null) {
                    pre.next = p.next;
                    break;
                }
                else {
                    pre.next = null;
                    break;
                }
            }
            pre = pre.next;
            p = p.next;
        }
    }
    void delete2(int x){ //刪除鏈表中的某個值(所有)
        Node pre, p;
        pre = head;
        p = head.next;
        while (p != null){
            if (p.data == x ){
                if (p.next != null) {
                    pre.next = p.next;
                    p = p.next;
                    continue;
                }
                else {
                    pre.next = null;
                    break;
                }
            }
            pre = pre.next;
            p = p.next;
        }
    }
    
 void deleteValue(int index){ //刪除,刪除第index鏈表上的數
        Node pre, p ;
        pre = head;
        p = head.next;
        while (index -- > 1 && p != null){
            pre = pre.next;
            p = p.next;
        }
        if (p == null)
            System.out.println("Error index");
        else
            pre.next = p.next;
    }

遍歷鏈表

void showInfo(){ //打印鏈表中所有的數
        Node pre, p;
        pre = head;
        p = head.next;
        while (p != null){
          System.out.print(p.data+" ");
          pre = pre.next;
          p = p.next;
        }
        System.out.println(" ");
    }

測試代碼

public class SingleNode {
    class Node{
        int data;
        Node next;
    }

    Node head; //鏈表的表頭指針
    //初始化
    void init(){
        head = new Node();
        head.next = null;
    }

    void add(int x){ //將x加入鏈表最後
        Node pre, p, q;
        pre = head;
        p = head.next;
        while (p != null){
            pre = pre.next;
            p = p.next;
        }
        q = new Node();
        q.data = x;
        pre.next = q;
    }


    void add2(int x){ //將x加入升序鏈表
        Node pre, p ,q;
        pre = head;
        p = head.next;
        while (p != null){
            if (p.data > x)
                break;
            pre = pre.next;
            p = p.next;
        }
        q = new Node();
        q.data = x;
        q.next = p;
        pre.next = q;
    }

    boolean find(int x){ //查找鏈表中是否有數值x
        boolean found = false;
        Node pre, p;
        pre = head;
        p = head.next;
        while (p != null){
            if (p.data == x)
                found = true;
            pre = pre.next;
            p = p.next;
        }
        return  found;
    }

    void delete(int x){ //刪除鏈表中的某個值(只能刪除第一次出現)
        Node pre, p;
        pre = head;
        p = head.next;
        while (p != null){
            if (p.data == x ){
                if (p.next != null) {
                    pre.next = p.next;
                    break;
                }
                else {
                    pre.next = null;
                    break;
                }
            }
            pre = pre.next;
            p = p.next;
        }
    }
    void delete2(int x){ //刪除鏈表中的某個值(所有)
        Node pre, p;
        pre = head;
        p = head.next;
        while (p != null){
            if (p.data == x ){
                if (p.next != null) {
                    pre.next = p.next;
                    p = p.next;
                    continue;
                }
                else {
                    pre.next = null;
                    break;
                }
            }
            pre = pre.next;
            p = p.next;
        }
    }

    /* 大話數據結構中的插入、刪除、讀取*/
    int getValue(int index){ //獲取第I個元素的數據
        Node pre,p;
        pre = head;
        p = head.next;

        while (index-- >1 && p != null){
            pre = pre.next;
            p = p.next;
        }
        if (p == null)
            return -1;
        else
            return p.data;
    }

    void inset(int index, int ins){ //插入,在索引index位置,插入ins值
        Node pre, p ,q;
        pre = head;
        p = head.next;
        while (index-- >1 && p != null ){
            pre = pre.next;
            p = p.next;
        }
        if (p == null){
            System.out.println("輸入索引大於鏈表長度");
        }else {
            q = new Node();
            q.data = ins;
            q.next = p;
            pre.next = q;
        }
    }

    void deleteValue(int index){ //刪除,刪除第index鏈表上的數
        Node pre, p ;
        pre = head;
        p = head.next;
        while (index -- > 1 && p != null){
            pre = pre.next;
            p = p.next;
        }
        if (p == null)
            System.out.println("Error index");
        else
            pre.next = p.next;
    }

     void showInfo(){ //打印鏈表中所有的數
        Node pre, p;
        pre = head;
        p = head.next;
        while (p != null){
          System.out.print(p.data+" ");
          pre = pre.next;
          p = p.next;
        }
        System.out.println(" ");
    }

    public static void main(String[] args) {
        SingleNode L = new SingleNode();
        L.init();//初始化鏈表
        for (int i=0;i < 10; i++){
            L.add(i);
        }
        for (int i=10;i >= 0; i--){
            L.add(i);
        }
        L.showInfo();
        System.out.println("鏈表中包含1?"+L.find(1));
        System.out.println("鏈表中包含12?"+L.find(12));
        System.out.println("刪除鏈表中的1");
        L.add(1);
        L.add(1);
        L.delete(1);
        L.showInfo();
        System.out.println(L.getValue(22)+" "+L.getValue(23));
        L.inset(1,100);
        L.inset(22,200);
        L.showInfo();
        L.deleteValue(22);
        L.showInfo();
        L.deleteValue(23);
        L.showInfo();
        L.deleteValue(1);
        L.showInfo();
    }
}

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