单链表

在这里插入图片描述

public interface ILinked {
    //头插法
    void addFirst(int data);
    //尾插法
    void addLast(int data);
    //任意位置插入,第一个数据节点为0号下标
    boolean addIndex(int index,int data);
    //查找是否包含关键字key是否在单链表当中
    boolean contains(int key);
    //删除第一次出现关键字为key的节点3.3 链表面试题
    int remove(int key);
    //删除所有值为key的节点
    void removeAllKey(int key);
    //得到单链表的长度
    int getLength();
    void display();
    void clear();
}
public class MySingleList implements ILinked{

    //节点
    class Node {
        private int data;
        private Node next;
        public Node(int data) {
            this.data = data;
            //this.next = null;
        }
    }
    private Node head;
    public MySingleList(){
        this.head = null;
    }


    @Override
    public void addFirst(int data) {
        Node node = new Node(data);
        //第一次插入的时候,没有任何的数据节点
        if(this.head == null) {
            this.head = node;
        }else {
            node.next = this.head;
            this.head = node;
        }
    }

    @Override
    public void addLast(int data) {
        Node node = new Node(data);
        //write
        if(this.head == null) {
            this.head = node;
        }else {
            Node cur = this.head;
            while(cur.next != null) {
                cur = cur.next;
            }
            //cur所指向的位置就是尾节点
            cur.next = node;
        }
    }
    //检查Index的合法性
    private void checkIndex(int index) {
        if(index < 0 || index > getLength()) {
            throw new UnsupportedOperationException("Index不合法");
        }
    }
    //找到index-1的位置  函数返回该位置的节点引用
    private Node searchIndex(int index) {
        int count = 0;
        Node cur = this.head;
        while(count < index-1){
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public boolean addIndex(int index, int data) {
        checkIndex(index);
        //头插法
        if(index == 0) {
            addFirst(data);
            return true;
        }
        Node node = new Node(data);
        Node cur = searchIndex(index);
        node.next = cur.next;
        cur.next = node;
        return true;
    }

    @Override
    public boolean contains(int key) {
        Node cur = this.head;
        while(cur != null) {
            if(cur.data == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //查找关键字Key的前驱
    //如果找不到返回null
    private Node searchPre(int key) {
        Node pre = this.head;
        //头结点是要删除的数据节点
        if(pre.data == key) {
            return this.head;
        }
        while(pre.next != null) {
            if(pre.next.data == key) {
                return pre;
            }
            pre = pre.next;
        }
        return null;
    }

    @Override
    public int remove(int key) {
        int oldData = 0;
        Node pre = searchPre(key);
        if(pre == null) {
            throw new UnsupportedOperationException("不存在key节点");
        }
        //是头结点的时候
        if(pre == head && pre.data == key) {
            oldData = this.head.data;
            this.head = this.head.next;
            return oldData;
        }
        Node del = pre.next;
        oldData = del.data;
        pre.next = del.next;
        return oldData;
    }

    @Override
    public void removeAllKey(int key) {
        if(this.head == null) {
            return;
        }
        Node pre = this.head;
        Node cur = this.head.next;
        while(cur != null) {
            if(cur.data == key) {
                pre.next = cur.next;
                cur = cur.next;
            }else{
                pre = cur;
                cur = cur.next;
            }
        }
        if(this.head.data == key) {
            this.head = this.head.next;
        }
    }

    @Override
    public int getLength() {
        int count = 0;
        Node cur = this.head;
        while(cur != null) {
            count++;
            cur =cur.next;
        }
        return count;
    }

    @Override
    public void display() {
        Node cur = this.head;
        while(cur != null) {
            System.out.print(cur.data+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        //this.head = null;
        while(this.head != null) {
            Node cur = this.head.next;
            this.head.next = null;
            this.head = cur;
        }
    }

测试用例:
package com.bit.SingleLinkedList;

public class TestDemo {
    public static void main(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addFirst(1);
        mySingleList.addFirst(2);
        mySingleList.addFirst(4);
        mySingleList.addFirst(4);
        mySingleList.addFirst(5);
        mySingleList.addFirst(6);
        mySingleList.display();
        mySingleList.addLast(1);
        mySingleList.addLast(2);
        mySingleList.addLast(3);
        mySingleList.addLast(4);
        mySingleList.display();
        mySingleList.addIndex(3,7);
        mySingleList.display();
        System.out.println(mySingleList.contains(7));
        mySingleList.remove(4);
        mySingleList.display();
        mySingleList.removeAllKey(4);
        mySingleList.display();
        System.out.println(mySingleList.getLength());
        
    }
}

结果:
6 5 4 4 2 1
6 5 4 4 2 1 1 2 3 4
6 5 4 7 4 2 1 1 2 3 4
true
6 5 7 4 2 1 1 2 3 4
6 5 7 2 1 1 2 3
8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值