java單鏈表的實現自己動手寫一個單鏈表


單鏈表:單鏈表是一種鏈式存取的數據結構,用一組地址任意的存儲單元存放線性表中的數據元素。鏈表中的數據是以結點來表示的,每個結點的構成:元素(數據元素的映象) + 指針(指示后繼元素存儲位置),元素就是存儲數據的存儲單元,指針就是連接每個結點的地址數據。
自己手動寫一個單鏈表:
首先,定義一個節點類:

package com.wei;

public class Link {
    public int data;// 存放數據
    public Link next;// 存放下一個節點

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

    public Link(int data, Link next) {
        this.data = data;
        this.next = next;
    }

    public Link() {
    }

    public void display() {
        System.out.println(data + " ");
    }
}

第二部分是定義一個鏈表類:

package com.wei;

public class LinkList {

    public Link frist;// 定義一個頭節點
    public Link last;//尾指針永遠指向頭節點
    public int size = 0;// 節點的位置

    public LinkList() {
        this.frist = null;//
    }

    /**
     * 判斷鏈表是否為空
     * 
     * @return
     */
    public boolean isis() {
        return size == 0;
    }

    /**
     * 頭插法
     * 
     * @param data
     */
    public void addfrist(int data) {
        Link L = new Link(data);
        L.next = frist;
        frist = L;
        size++;
    }

    /**
     * 尾插法
     * 
     * @param data
     */
    public void addlast(int data) {

        if (frist == null) {
            frist = new Link(data);
            last = frist;
        } else {
            Link newL = new Link(data);
            last.next = newL;
            last = newL;
        }
        size++;
    }

    /**
     * 從頭刪除
     * 
     * @return
     */
    public Link removefrist() {
        Link d = frist;
        frist = d.next;
        size--;
        return d;
    }

    /**
     * 刪除最后一個
     */
    public void dellast() {
        Dell(size - 1);
    }

    /**
     * 獲取鏈表長度
     */
    public void displayAllLink() {
        Link cure = frist;
        while (cure != null) {
            cure.display();
            cure = cure.next;
        }
        System.out.println("長度" + size);
    }

    /**
     * 獲取指定位置的節點元素
     * 
     * @param index
     * @return
     */
    public Link getData(int index) {
        if (index < 0 && index > size - 1) {
            throw new IndexOutOfBoundsException("越界");
        }
        Link count = frist;
        for (int i = 0; i < size && count != null; i++, count = count.next) {
            if (i == index) {
                return count;
            }
        }
        return null;
    }

    /**
     * 按值查找指定位置
     * 
     * @param element
     * @return
     */
    public int selectIndex(int element) {
        Link current = frist;
        for (int i = 0; i < size && current != null; i++, current = current.next) {
            if (current.data == element) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 刪除鏈表中數值最大的元素
     */
    public void delMax() {
        // 要遍歷的鏈表
        Link cu = frist;
        // 初始化一個節點,當中間變量
        Link cc = new Link(0);
        // 遍歷
        for (int i = 0; i < size && cu != null; i++, cu = cu.next) {
            if (cu.data > cc.data) {
                cc.data = cu.data;
            }
        }
        int data = cc.data;
        int number = selectIndex(data);
        Dell(number);
    }

    /**
     * 刪除鏈表中數值最小的元素
     */
    public void delMin() {
        // 要遍歷的鏈表
        Link cu = frist;
        // 初始化一個節點,當中間變量
        Link cc = new Link(0);
        // 遍歷
        for (int i = 0; i < size && cu != null; i++, cu = cu.next) {
            if (cu.data < cc.data) {
                cc.data = cu.data;
            }
        }
        int data = cc.data;
        int number = selectIndex(data);
        Dell(number);
    }

    /**
     * 從指定位置處插入數據
     * 
     * @param t
     * @param index
     */
    public void insert(int t, int index) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("索引超出線性表范圍");
        }
        if (frist == null) {
            addlast(t);
        } else {
            if (index == 0) {
                addfrist(t);
            } else {
                Link k = getData(index - 1);
                k.next = new Link(t, k.next);
                size++;
            }

        }
    }

    /**
     * 從指定位置處刪除
     * 
     * @param index
     */
    public void Dell(int index) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("索引超出線性表范圍");
        }
        Link del = null;
        if (index == 0) {
            del = frist.next;
            frist = frist.next;
        } else {
            Link neL = getData(index - 1);
            del = neL.next;
            neL.next = del.next;
            del.next = null;
        }
        size--;
    }

    /**
     * 清空鏈表
     */
    public void clear() {
        frist = null;
        last = null;
        size = 0;
    }

    /**
     * 按從小到大排序
     */
    public void Min_to_Max() {
        // 要遍歷的鏈表
        Link cu = frist;
        // 記錄最小值
        int min;
        while (cu != null) {
            // 內重循環從當前節點的下一個節點循環到尾節點,找到和外重循環的值比較最小的那個,然后與外重循環進行交換
            Link nextLink = cu.next;
            while (nextLink != null) {
                // 比外循環小的值放在前面
                if (nextLink.data < cu.data) {
                    min = nextLink.data;
                    nextLink.data = cu.data;
                    cu.data = min;
                }
                nextLink = nextLink.next;
            }
            cu = cu.next;
        }

    }

    /**
     * 按從大到大排序
     */
    public void Max_to_Min() {
        // 要遍歷的鏈表
        Link cu = frist;
        // 記錄最小值
        int min;
        while (cu != null) {
            // 內重循環從當前節點的下一個節點循環到尾節點
            //找到和外重循環的值比較最小的那個,然后與外重循環進行交換
            Link nextLink = cu.next;
            while (nextLink != null) {
                // 比外循環小的值放在前面
                if (nextLink.data > cu.data) {
                    min = nextLink.data;
                    nextLink.data = cu.data;
                    cu.data = min;
                }
                nextLink = nextLink.next;
            }
            cu = cu.next;
        }

    }
    
}

最后是測試類:

package com.wei;

public class Test {

    public static void main(String [] arr) {
        LinkList g = new LinkList();
        g.addlast(13);
        g.addlast(16);
        g.addlast(-3);
        g.addlast(8);
        g.addlast(5);
        g.addlast(22);
        g.Min_to_Max();
        g.displayAllLink();
        g.Max_to_Min();
        g.displayAllLink();
    }
}

一條鏈表就這樣寫完了,需要什么功能可以自己擴展。

 


免責聲明!

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



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