今日完成了数据结构中链表相关操作的自我实现,通过画图等加深了对链表的理解。
链表的优点在于插入和删除方便,这是和顺序表的不同。
下面给出链表代码;

import java.util.List;

public class MySingleList implements ILinkedList{

    //使用内部类来表示一个节点
    static class ListNode{

        public int val;

        public ListNode next;

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

    public ListNode head;

    //public int usedSize;

    /**
     * 头插节点
     * @param data
     */
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);

        if(this.head == null){
            this.head = node;
            return;
        }

        node.next = this.head;
        this.head = node;
    }

    /**
     * 尾插节点
     * @param data
     */
    @Override
    public void addLast(int data) {
        //如果链表为空,就是添加头节点
        ListNode node = new ListNode(data);
        if(this.head == null){
            this.head = node;
            return;
        }

        //链表不为空
        ListNode cur = this.head;
        while(cur.next != null){
            cur = cur.next;
        }

        cur.next = node;
    }

    /**
     * 在index位置插入节点
     * @param index
     * @param data
     */
    @Override
    public void addIndex(int index, int data) {

        checkPos(index);

        if(index == 0){
            addFirst(data);
            return;
        }

        if(index == size()){
            addLast(data);
            return;
        }

        //在中间位置插入
        ListNode cur = searchIndexNode(index);

        ListNode node = new ListNode(data);

        node.next = cur.next;
        cur.next = node;

    }

    /**
     * 找到index前一个节点
     * @param index
     * @return
     */
    private ListNode searchIndexNode(int index){
        ListNode cur = this.head;
        int count = 0;
        while(count != index - 1){
            cur = cur.next;
            count++;
        }

        return cur;
    }

    /**
     * 判断index是否合理
     * @param index
     */
    private void checkPos(int index){
        if(index < 0 || index > size()){
            throw new CheckPosException("index位置不合理" + index);
        }
    }

    /**
     * 判断链表里是否有key这个值
     * @param key
     * @return
     */
    @Override
    public boolean contains(int key) {

        ListNode cur = this.head;

        while (cur.next != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }

        return false;
    }

    /**
     * 删除第一个key值节点
     * @param key
     */
    @Override
    public void remove(int key) {
        if(this.head == null){
            System.out.println("链表为空,无法删除...");
            return;
        }

        if(this.head.val == key){
            this.head = this.head.next;
            return;
        }

        ListNode prev = this.head;
        ListNode cur = this.head.next;

        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                return;
            }
            cur = cur.next;
            prev = prev.next;
        }

    }

    /**
     * 面试题:在遍历一遍链表的情况下删除全部key值
     * @param key
     */
    @Override
    public void removeAllKey(int key) {
        //判断是否为空
        if(size() == 0){
            System.out.println("链表为空,无法删除...");
            return;
        }

        ListNode cur = this.head.next;
        ListNode prev = head;

        while (cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                cur = cur.next;
                prev = prev.next;
            }
        }

        if(this.head.val == key){
            head = this.head.next;
        }
    }

    /**
     * 计算链表长度
     * @return
     */
    @Override
    public int size() {

        ListNode cur = this.head;
        int count = 0;

        while (cur != null){
            count++;
            cur = cur.next;
        }

        return count;
    }

    /**
     * 清除链表
     */
    @Override
    public void clear() {
        this.head = null;
    }

    /**
     * 遍历打印链表
     */
    @Override
    public void display() {

        ListNode cur = this.head;

        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

}

其中相关接口与异常处理代码同步上传至gitee仓库

posted on 2025-04-16 15:56  -MARIO  阅读(23)  评论(0)    收藏  举报