【Java】单项不循环链表实现

目录

用到的包和接口

IMySingleLinkedList接口

PosNotLegalException异常(用来判断pos是否合法)

MySingleLinkedList类

Test测试类


用到的包和接口

IMySingleLinkedList接口

package singlelinkedlist;

public interface IMySingleLinkedList {
    void addFirst(int val);
    void addLast(int val);
    void display();
    int size();
    void addIndex(int pos,int val);
    boolean contains(int val);
    void remove(int val);
    void removeAllKey(int val);
    void clear();
}

PosNotLegalException异常(用来判断pos是否合法)

public class PosNotLegalException extends RuntimeException {
    public PosNotLegalException(){}
    public PosNotLegalException(String msg){
        super(msg);
    }
}

MySingleLinkedList类

package singlelinkedlist;

public class MySingleLinkedList implements IMySingleLinkedList {
    static class ListNode {
        public int val;
        public ListNode next;

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

    //创建头节点head
    public ListNode head;

    @Override
    public void addFirst(int val) {
        //首先要申请一个节点
        ListNode newNode = new ListNode(val);
        newNode.next = head;
        head = newNode;
    }

    @Override
    public void addLast(int val) {
        ListNode newNode = new ListNode(val);
        if (head == null) {
            head = newNode;
            return;
        }
        ListNode current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = newNode;
    }

    @Override
    public int size() {
        ListNode current = head;
        int count = 0;
        while (current != null) {
            count++;
            current = current.next;
        }
        return count;
    }

    @Override
    public void addIndex(int pos, int val) {
        //需要判断pos的合法性
        try {
            checkPosLegal(pos);
        } catch (PosNotLegalException e) {
            e.printStackTrace();
        }
        //pos在开头
        if (pos == 0) {
            addFirst(val);
            return;
        }
        //pos在结尾
        if (pos == size()) {
            addLast(val);
            return;
        }
        //pos在中间
        ListNode newNode = new ListNode(val);
        ListNode current = head;
        int count = 0;
        //找到pos前面一个位置
        while(pos-1 >0){
            current = current.next;
            pos--;
        }
        //进行插入
        newNode.next = current.next;
        current.next = newNode;
    }

    private void checkPosLegal(int pos) throws PosNotLegalException {
        if (pos < 0 || pos > size()) {
            throw new PosNotLegalException("Pos位置不合法...");
        }
    }

    @Override
    public boolean contains(int val) {
        ListNode current = head;
        while(current!=null){
            if(current.val == val){
                return true;
            }
            current = current.next;
        }
        return false;
    }

    @Override
    public void remove(int val) {
        //在头部
        if(val == head.val){
            head = head.next;
            return;
        }
        //不在头部
        ListNode current = head;
        while(current.next!=null ){
            if(current.next.val == val){
                //此时的current已经是想要删除的前一个节点
                current.next = current.next.next;
            }
            current = current.next;
        }
    }

    @Override
    public void removeAllKey(int val) {
        if(head == null){
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur!=null){
            if(cur.val == val){
                prev.next = cur.next;
            }else{
                prev = prev.next;
            }
            cur =cur.next;
        }
        //处理头节点
        if(head.val == val){
            head = head.next;
        }
    }

    @Override
    public void display() {
        ListNode current = head;
        while (current != null) {
            System.out.print(current.val);
            if (current.next != null) {
                System.out.print("->");
            }
            current = current.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur!=null){
            ListNode del = cur;
            cur = cur.next;
            del.next = null;
        }
    }
}

Test测试类

public class Test {
    public static void main(String[] args) {
        IMySingleLinkedList list = new MySingleLinkedList();
        /*list.addFirst(1);
        list.addFirst(2);
        list.addFirst(3);
        System.out.println("====addFirst===");
        list.display();*/
        System.out.println("===addLast===");
        list.addLast(3);
        list.addLast(2);
        list.addLast(3);
        list.addLast(3);
        list.addLast(3);
        list.display();
        System.out.println("===size===");
        System.out.println(list.size());
        /*System.out.println("===addIndex===");
        list.addIndex(0,10);
        list.addIndex(4,10);
        list.display();
        list.addIndex(6,10);*/
        System.out.println("===contains===");
        System.out.println(list.contains(1));
        System.out.println(list.contains(3));
        /*        System.out.println("===remove===");
        list.remove(1);
        list.remove(1);
        list.display();*/
        System.out.println("===removeAllKey===");
        list.removeAllKey(3);
        list.display();
        list.clear();
    }
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值