单链表头、尾插法,显示,插入,删除,判断值是否存在

单链表的头插法、打印、尾插法、任意位置插入、获取单链表长度、判断插入位置是否合法、修改指定位置的值、删除指定位置的元素。

/**
 * 接口,定义方法
 */
public interface Ilinked {
     void headInsert(int data);//头插法
     void printLinked();//打印单链表
     void lastInsert(int data);//尾插法
     boolean indexInsert(int index,int data);//任意位置插入,0开始
     int getLength();//获取单链表的长度
     void checkIndexIfTure(int index);//判断插入的index位置是否合法
     linkListed.Node searchIndex(int index);//找到下表为index-1的结点,方便插入
     boolean contains(int key);//查找链表是否包含关键字key
     void updateIndexElemt(int index,int data);//修改指定索引位置的值
     void deleteIndexElemt(int index);//删除指定索引位置的元素
}

/**
 * 定义结点以及实现的具体方法
 */
public class linkListed implements Ilinked {
    //定义一个结点类
    class Node{
        private int data;
        private Node next;

        public Node(int data) {
            this.data = data;
        }
    }
    private Node head;//定义一个头结点

    public linkListed() {
    }

    /**
     * 头插法
     * @param data 插入的数据
     */
    @Override
    public void headInsert(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 printLinked() {
        Node current =this.head;
        while (current!=null){
            System.out.print(current.data+" ");
            current = current.next;
        }
    }

    /**
     * 尾插法
     * @param data 数据
     */
    @Override
    public void lastInsert(int data) {
        Node node = new Node(data);
        Node current = this.head;//当前结点
        if (this.head == null){//如果头结点是空,这个结点就是头结点
            this.head = node;
        }else {
            while (current.next!=null){//如果不是空,当当前结点.next==null说明为尾结点
                current = current.next;
            }
            current.next = node;//开始插入
        }
    }
    /**
     * 任意位置插入,从0开始
     * @param index 插入索引位置
     * @param data 插入的数据
     */
    @Override
    public boolean indexInsert(int index, int data) {
        Node node = new Node(data);
        Node current = searchIndex(index);
        if (current == null){
            node.next = this.head;
            this.head = node;
        }else{
            node.next = current.next;
            current.next = node;
        }
        return  true;
    }

    /**
     * 获取单链表的长度
     */
    @Override
    public int getLength() {
        Node current = this.head;
        int total = 0;
        while (current!=null){
            current = current.next;
            total++;
        }
        return total;
    }

    /**
     * 判断插入的位置是否合法
     */
    @Override
    public void checkIndexIfTure(int index) {
        if (index<0 || index>getLength()){
            throw new IndexOutOfBoundsException("插入下标异常");
        }
    }

    /**
     * 寻找下表为index-1的位置
     * @param index
     * @return
     */
    @Override
    public Node searchIndex(int index) {
        checkIndexIfTure(index);
        if (index == 0){
            return null;
        }
        int count = 0;
        Node current = this.head;
        while (current.next!=null && count<index-1) {
            current = current.next;
            count++;
        }
        return current;
    }

    /**
     * 查找是否包含关键字key
     * @param key 关键字
     * @return true/false
     */
    @Override
    public boolean contains(int key) {
        Node current = this.head;
        while( current != null){
            if(current.data == key){
                return true;
            }
            current = current.next;
        }
        return true;
    }

    @Override
    public void updateIndexElemt(int index, int data) {
        checkIndexIfTure(index);
        Node current = searchIndex(index);
        if (current == null){
            this.head.data=data;
        }else{
            current.next.data=data;
        }

    }

    /**
     * 删除指定位置索引的元素
     * @param index 要删除的索引位置
     */
    @Override
    public void deleteIndexElemt(int index) {
        checkIndexIfTure(index);
        Node current = searchIndex(index);
        if (index == 0 ){
            this.head = this.head.next;
        }else{
            current.next=current.next.next;
        }
    }
}

/**
 * 测试类
 */
public class Test {
    public static void main(String[] args) {
        linkListed linkListed = new linkListed();
        linkListed.headInsert(12);
        linkListed.headInsert(165);
        linkListed.headInsert(85);
        linkListed.printLinked();//打印头插法结果85 165 12
        System.out.println();
        linkListed.lastInsert(52);
        linkListed.lastInsert(63);
        linkListed.lastInsert(75);
        linkListed.printLinked();//打印尾插法结果85 165 12 52 63 75
        System.out.println();
        System.out.print(linkListed.getLength());//打印长度6
        System.out.println();
        linkListed.indexInsert(4,100);
        linkListed.printLinked();//打印插入后的结果85 165 12 52 100 63 75
        System.out.println();
        System.out.println(linkListed.contains(100));//打印是否包含100,true
        System.out.println(linkListed.contains(55));//打印是否包含55,false
        System.out.println();
        linkListed.updateIndexElemt(0,200);
        linkListed.printLinked();//打印插入后的结果200 165 12 52 200 63 75
        System.out.println();
        linkListed.updateIndexElemt(6,400);
        linkListed.printLinked();//打印插入后的结果200 165 12 52 200 63 400
        System.out.println();
        linkListed.deleteIndexElemt(0);//打印插入后的结果165 12 52 200 63 400
        linkListed.printLinked();
        System.out.println();
        linkListed.deleteIndexElemt(5);//打印插入后的结果165 12 52 200 63
        linkListed.printLinked();
        System.out.println();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值