無頭單向非循環鏈表及部分接口(Java實現)

鏈表

存儲在不連續的內存空間上 更擅長插入和刪除

此處我們在結尾以鏈表的插入接口爲例 用圖的形式詳解該操作

public class TestLinkedlist {
    public static void main(String[] args) {
        testAddFirst();
        testAddLast();
        testAddIndex();
        testContains();
        testRemove();
    }

    public static void testAddFirst() {
        System.out.println("測試頭插");
        LinkedList list = new LinkedList();
        list.addFirst(1);
        list.addFirst(2);
        list.addFirst(3);
        list.addFirst(4);
        list.display();
    }

    public static void testAddLast() {
        System.out.println("測試尾插");
        LinkedList list = new LinkedList();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        list.addLast(4);
        list.display();
    }

    public static void testAddIndex() {
        System.out.println("測試插入任意位置");
        LinkedList list = new LinkedList();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        list.addLast(4);
        list.addIndex(2,5);

        list.display();
    }

    public static void testContains() {
        System.out.println("測試是否存在");
        LinkedList list = new LinkedList();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        list.addLast(4);
        list.contains(3);

        list.display();
    }

    public static void testRemove() {
        System.out.println("測試刪除鏈表中的元素");
        LinkedList list = new LinkedList();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        list.addLast(4);
        list.remove(3);

        list.display();
    }
}

 

import com.sun.org.apache.bcel.internal.generic.LNEG;
import sun.awt.image.ImageWatched;

class LinkedNode {
    public int data = 0;
    public LinkedNode next = null;
    public  LinkedNode(int data) {
        this.data = data;
    }
}

public class LinkedList {
    //頭結點
    private LinkedNode head = null;

    public void display() {
        //打印
        System.out.print("[");
        for (LinkedNode node = this.head;
             node != null;
             node = node.next) {
            System.out.print(node.data);
            if (node.next != null) {
                System.out.print(",");
            }
        }
        System.out.println("]");
    }

    //頭插
    public void addFirst(int elem) {
        //先創建一個節點 讓這個節點的值就是elem
        LinkedNode node = new LinkedNode(elem);
        if(this.head == null) {
            //鏈表爲空時
            this.head = node;
            return;
        }
        //如果不是空鏈表 就把新的節點放到鏈表的開始位置
        node.next = head;
        this.head = node;
        return;

    }

    //尾插
    public void addLast(int elem) {
        LinkedNode node = new LinkedNode(elem);
        if (this.head == null) {
            //空鏈表
            this.head = node;
            return;
        }
        //非空 需要先找到最後一個節點
        LinkedNode cur = this.head;
        //循環結束 cur就是最後一個結點
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    //任意位置插入
    public void addIndex(int index,int elem) {
        LinkedNode node = new LinkedNode(elem);

        //1.判斷
        int len = size();
        if (index < 0 || index > len) {
            return;
        }
        //2.頭插
        if (index == 0) {
            addFirst(elem);
            return;
        }
        //3.尾插
        if (index == len) {
            addLast(elem);
            return;
        }
        //4.中間位置 需要找到 index - 1 這個位置
        LinkedNode prev = getIndexPos(index - 1);
        node.next = prev.next;
        prev.next = node;
    }



    private LinkedNode getIndexPos(int index) {

        LinkedNode cur = this.head;
        for (int i = 0;i < index;i++) {
            cur = cur.next;
        }
        return cur;
    }

    public int size() {
        int size = 0;
        for (LinkedNode cur = this.head;cur != null;cur = cur.next) {
            size++;
        }
        return size;
    }

    //判斷是否存在
    public boolean contains(int toFind) {
        for (LinkedNode cur = this.head;cur != null;cur = cur.next) {
            if (cur.data == toFind) {
                return true;
            }
        }
        return false;
    }

    //刪除第一次出現的元素
    public void remove(int toRemove) {
        //1.考慮空鏈表的情況
        if (head == null) {
            return;
        }
        //2.先判斷是否是頭結點
        if (head.data == toRemove) {
            this.head = this.head.next;
            return;
        }
        //3.刪除中間結點 找到前一個結點
        LinkedNode prev = searchPrev(toRemove);
        prev.next = prev.next.next;
    }

    private LinkedNode searchPrev(int toRemove) {
        if (this.head == null) {
            return null;
        }
        LinkedNode prev = this.head;
        while (prev.next != null) {
            if (prev.next.data == toRemove) {
                return prev;
            }
            prev = prev.next;
        }
        return null;
    }

}

鏈表插入(int index , int elem):

我們最基礎的思想就是對輸入進行判斷,即判斷要插入位置的情況

1.判定是否有效 

即 index < 0 || index >len

2.頭插

3.尾插

4.中間位置

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