Java【鏈表操作】

建立單列表節點類

package org.article.node;

import lombok.Data;

/**
 * @Author: yangyang7
 * @Description: 單鏈表節點
 * @Date: Created in 09:28 2019-11-25
 */

@Data
public class Node {

    /**
     * 數據域
     */
    private int data;

    /**
     * 指針域
     */
    private Node next;

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

建立單鏈表類

package org.article.node;

import lombok.Data;

/**
 * @Author: yangyang7
 * @Description: 建立簡單的單鏈表
 * @Date: Created in 09:30 2019-11-25
 */

@Data
public class SLinkList {

    private Node head;

    /**
     * 節點的長度
     */
    private int length = 0;

    public SLinkList() {
        this.head = null;
    }

    /**
     * 在鏈表頭部添加節點
     */
    public void addHead(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            newNode.setNext(head);
            head = newNode;
        }
        length++;
    }

    /**
     * 在鏈表頭部刪除節點
     */
    public void deleteHead() {
        if (head == null) {
            System.out.println("鏈表爲空");
            return;
        }
        Node curNode = head;
        head = curNode.getNext();
        length--;
    }

    /**
     * 在鏈表尾部添加節點
     */
    public void addTail(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            Node preNode = head;
            int count = 1;
            while (count < length) {
                preNode = preNode.getNext();
                count++;
            }
            Node curNode = preNode.getNext();
            newNode.setNext(curNode);
            preNode.setNext(newNode);
            length++;
        }
    }

    /**
     * 在鏈表尾部刪除節點
     */
    public void deleteTail() {
        if (head == null) {
            System.out.println("鏈表爲空");
            return;
        }
        Node preNode = head;
        int count = 1;
        while (count < length - 1) {
            preNode = preNode.getNext();
            count++;
        }
        Node curNode = preNode;
        preNode.setNext(curNode.getNext());
        length--;
    }

    /**
     * 在鏈表指定位置插入節點
     */
    public void insertList(int index, int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        }
        if (index < length || index < 0) {
            System.out.println("沒有指定節點位置");
            return;
        }
        if (index == 1) {
            // 在頭部插入
            head.setNext(head);
            head = newNode;
        } else {
            Node preNode = head;
            int count = 1;
            while (count < index - 1) {
                preNode = newNode.getNext();
                count++;
            }
            Node curNode = preNode.getNext();
            preNode.setNext(newNode);
            newNode.setNext(curNode);
        }
        length++;
    }

    /**
     * 在鏈表指定位置刪除節點
     */
    public void deleteList(int index) {
        if (head == null) {
            System.out.println("鏈表爲空");
            return;
        }
        if (index == 1) {
            // 刪除頭部
            Node curNode = head;
            head = curNode.getNext();
            length--;
            return;
        } else {
            Node preNode = head;
            int count = 1;
            while (count < index - 1) {
                preNode = preNode.getNext();
                count++;
            }
            Node curNode = preNode.getNext();
            preNode.setNext(curNode.getNext());
            length--;
        }
    }


    /**
     * 獲取指定位置的節點
     *
     * @param index
     * @return
     */
    public Node getIndexData(int index) {
        if (head == null) {
            System.out.println("空表");
            return null;
        }
        if (index > length || index < 1) {
            System.out.println("節點位置不存在,可查詢的位置爲1到" + length);
            return null;
        }
        Node preNode = head;
        int count = 1;
        while (count != index) {
            preNode = preNode.getNext();
            count++;
        }
        System.out.println(preNode);
        return preNode;
    }

    /**
     * 修改指定位置節點數據
     *
     * @param index
     * @param data
     */
    public void updateIndexData(int index, int data) {
        if (head == null) {
            System.out.println("空表");
        }
        if (index > length || index < 1) {
            System.out.println("節點位置不存在,可查詢的位置爲1到" + length);
        }
        Node preNode = head;
        int count = 1;
        while (count != index) {
            preNode = preNode.getNext();
            count++;
        }
        preNode.setData(data);
    }
}

建立雙向列表節點類

package org.article.node;

import lombok.Data;

/**
 * @Author: yangyang7
 * @Description:
 * @Date: Created in 16:47 2019-11-25
 */

@Data
public class DNode {

    /**
     * 數據域
     */
    private int data;

    /**
     * 後驅指針域
     */
    private DNode next;

    /**
     * 前驅指針域
     */
    private DNode previous;

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

建立雙向鏈表類

package org.article.node;

import lombok.Data;

/**
 * @Author: yangyang7
 * @Description: 雙向鏈表類
 * @Date: Created in 16:50 2019-11-25
 */

@Data
public class DLinkList {

    /**
     * 表頭
     */
    private DNode head;
    private int length = 0;

    public DLinkList() {
        this.head = null;
    }

    /**
     * 在鏈表頭部添加結點
     *
     * @param data
     */
    public void addHead(int data) {
        DNode newNode = new DNode(data);
        if (head == null) {
            //如果鏈表爲空,增加新結點
            head = newNode;
        } else {
            newNode.setNext(head);
            head.setPrevious(newNode);
            head = newNode;
        }
        length++;
    }

    /**
     * 在鏈表頭部刪除結點
     */
    public void deleteHead() {
        if (head == null) {
            System.out.println("空表,刪除的結點不存在");
        } else {
            DNode curNode = head;
            head = curNode.getNext();
            head.setPrevious(null);
        }
        length--;
    }

    /**
     * 在鏈表尾部添加結點
     *
     * @param data
     */
    public void addTail(int data) {
        DNode newNode = new DNode(data);
        if (head == null) {
            head = newNode;
        } else {
            DNode curNode = head;
            int count = 1;
            while (count < length) {
                curNode = curNode.getNext();
                count++;
            }
            newNode.setNext(null);
            newNode.setPrevious(curNode);
            curNode.setNext(newNode);
        }
        length++;
    }

    /**
     * 在鏈表尾部刪除結點
     */
    public void deleteTail() {
        if (head == null) {
            System.out.println("空表,刪除的結點不存在");
        } else {
            DNode preNode = head;
            int count = 1;
            while (count < length - 1) {
                preNode = preNode.getNext();
                count++;
            }
            preNode.setNext(null);
        }
        length--;
    }


    /**
     * 在指定位置插入結點
     *
     * @param data
     * @param index
     */
    public void insertList(int data, int index) {
        DNode newNode = new DNode(data);
        if (head == null) {
            head = newNode;//鏈表爲空,插入
        }
        if (index > length + 1 || index < 1) {
            System.out.println("結點插入的位置不存在,可插入的位置爲1到" + (length + 1));
        }
        if (index == 1) {
            newNode.setNext(head);
            head.setPrevious(newNode);
            head = newNode;//在鏈表開頭插入
        } else {              //在鏈表中間或尾部插入
            DNode preNode = head;
            int count = 1;
            while (count < index - 1) {
                preNode = preNode.getNext();
                count++;
            }
            DNode curNode = preNode.getNext();
            newNode.setNext(curNode);
            newNode.setPrevious(preNode);
            preNode.setNext(newNode);

            curNode.setPrevious(newNode);

        }
        length++;
    }

    /**
     * 在指定位置刪除結點
     *
     * @param index
     */
    public void deleteList(int index) {
        if (index > length || index < 1) {
            System.out.println("結點刪除的位置不存在,可刪除的位置爲1到" + length);
        }
        if (index == 1) {
            DNode curNode = head;
            head = curNode.getNext();
            head.setPrevious(null);
            length--;
        } else {
            DNode preNode = head;
            int count = 1;
            while (count < index - 1) {
                preNode = preNode.getNext();
                count++;
            }
            DNode curNode = preNode.getNext();
            DNode laterNode = curNode.getNext();
            preNode.setNext(laterNode);
            if (laterNode != null) {  //若被刪除結點的後繼結點不是null結點,那麼設置其前驅結點
                laterNode.setPrevious(preNode);//指針指向被刪除結點的前驅結點
            }
            length--;
        }
    }

    /**
     * 獲取指定位置的數據,與單鏈表一樣
     *
     * @param index
     */
    public void getIndexData(int index) {
        if (head == null) {
            System.out.println("空表");
        }
        if (index > length || index < 1) {
            System.out.println("結點位置不存在,可獲取的位置爲1到" + length);
        }
        DNode curNode = head;
        int count = 1;
        while (count != index) {
            curNode = curNode.getNext();
            count++;
        }
        System.out.println(curNode);
        System.out.println();
    }

    /**
     * 修改指定位置的結點數據,與單鏈表一樣
     *
     * @param index
     * @param data
     */
    public void updateIndexData(int index, int data) {
        if (head == null) {
            System.out.println("空表");
        }
        if (index > length || index < 1) {
            System.out.println("結點位置不存在,可更新的位置爲1到" + length);
        }
        DNode curNode = head;
        int count = 1;//while也可以用for循環方式解決
        while (count != index) {
            curNode = curNode.getNext();
            count++;
        }
        curNode.setData(data);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章