链表的操作3

这篇博客详细介绍了链表数据结构的操作,包括如何修改指定索引的节点数据、在给定位置插入新节点以及根据索引查找和修改节点。通过递归算法实现内部节点的增删改查,提供了对外的接口方便使用。示例代码展示了如何在链表中插入‘餐车’和‘卖票车’节点。
摘要由CSDN通过智能技术生成

链表的操作

1.修改指定索引结点的数据

修改节点数据的本质是根据索引查找对应的节点然后对该节点进行修改

//内部类方法
        public boolean setNode(int index,T newdata){
            if (foot++==index){
                this.data=newdata;
            }else {
                if (this.nextNode==null){
                    return false;
                }else {
                    return this.nextNode.setNode(index,newdata);
                }
            }
            return false;
        }
//外部类方法
    public boolean set(int index,T data){
        foot = 0;
        if (index>count||index<0){
            return false;
        }
        if (this.root==null){
            return false;
        }
        return this.root.setNode(index,data);
    }
2.在指定位置插入结点
//内部类方法
        public void addNode(int index,Node<T> previousNode,Node<T> newNode){
            if (foot++==index){
                newNode.nextNode = this;
                previousNode.nextNode = newNode;
            }else {
                this.nextNode.addNode(index,this,newNode);
            }
        }
//外部类方法
public void add(int index,T data){
        Node<T> newNode = new Node<>(data);
        if (index==0){
            //让之前的结点成为新节点的下一个结点
            newNode.nextNode = this.root;
            this.root = newNode;
        }else {
            this.root.addNode(index, this.root,newNode);
        }
        foot=0;
    }

在外部类方法中:先是判断第一个结点的内容
内部类方法中,对当前结点和下一个结点进行判断,并利用递归算法,对下一节点进行同样的操作

综合代码
package lianbiao;
public class Link<T> {
    private Node<T> root;//链表的根节点
    private int foot;//表示链表结点的索引
    private int count;//对添加数据进行计数
    private class Node<T>{
        private T data;
        private Node nextNode;
        public Node() {
        }
        public Node(T data) {
            this.data = data;
        }
        public T getData() {
            return data;
        }
        public void setData(T data) {
            this.data = data;
        }
        public Node getNextNode() {
            return nextNode;
        }

        public void setNextNode(Node nextNode) {
            this.nextNode = nextNode;
        }
        /**
         * 添加结点
         */
        public void addNode(Node<T> newNode){
            if (this.getNextNode()==null){
                this.setNextNode(newNode);
            }else {
                this.getNextNode().addNode(newNode);
            }
        }
        /**
         * 输出结点数据
         */
        public void printNode(){
            System.out.print(this.data+" ");
            if (this.nextNode!=null){
                this.nextNode.printNode();
            }
        }
        /**
         * 判断是否包含某个数据
         */
        public boolean containsNode(T data){
            if (this.data.equals(data)){
                return true;
            }else {
                if (this.nextNode==null){
                    return false;
                }else {
                    return this.nextNode.containsNode(data);
                }
            }
        }
        /**
         * 根据索引取得结点的数据,返回类型为泛型T
         * index 传入的参数,表述要查找的索引
         * 如果有数据返回指定结点的数据,没有就返回null
         */
        public T getNodedata(int index){
            //从0开始判断是否是对应的索引
            if(foot++==index){
                return this.data;
            }else if (this.nextNode==null){
                return null;
            }else {
                return (T)this.nextNode.getNodedata(index);
            }
        }
        /**
         * 修改某个结点的数据
         * 给定索引 index,以及newdata
         */
        public boolean setNode(int index,T newdata){
            if (foot++==index){
                this.data=newdata;
            }else {
                if (this.nextNode==null){
                    return false;
                }else {
                    return this.nextNode.setNode(index,newdata);
                }
            }
            return false;
        }
        /**
         * 在指定位置添加结点,index 前结点,新结点
         */
        public void addNode(int index,Node<T> previousNode,Node<T> newNode){
            if (foot++==index){
                newNode.nextNode = this;
                previousNode.nextNode = newNode;
            }else {
                this.nextNode.addNode(index,this,newNode);
            }
        }
    }
    //在外部类定义主题能够访问的方法,从而调用内部类的方法
    //添加结点
    public void add(T data){
        Node<T> newNode = new Node<>(data);//创建结点对象
        if (this.root==null){
            this.root = newNode;
        }else {
            this.root.addNode(newNode);
        }
        count++;
    }
    //输出数据
    public void print(){
        if (this.root == null){
            return;
        }else {
            this.root.printNode();
        }
    }
    //判断结点的长度,或者结点的个数
    public int size(){
        return this.count;
    }
    //判断结点是否为空
    public boolean isEmpty(){
        return this.count==0;
    }
    //判断结点中是否包含某数据
    public boolean contains(T data){
        if (this.root==null){
            return false;
        }else {
            return this.root.containsNode(data);
        }
    }
    //根据所以查找数据
    public T get(int index){
        if (this.root==null){
            return null;
        }if (index<0||index>count){
            return null;
        }
        this.foot=0;//重新初始化下标,因为在此查询的时候还是从第一个结点开始查的,当此时foot已经发生改变,并不是第一个索引0
        return this.root.getNodedata(index);
    }
    //修改数据,先找到指定索引,在进行修改
    public boolean set(int index,T data){
        foot = 0;
        if (index>count||index<0){
            return false;
        }
        if (this.root==null){
            return false;
        }
        return this.root.setNode(index,data);
    }
    /**
     * 指定位置添加结点
     */
    public void add(int index,T data){
        Node<T> newNode = new Node<>(data);
        if (index==0){
            //让之前的结点成为新节点的下一个结点
            newNode.nextNode = this.root;
            this.root = newNode;
        }else {
            this.root.addNode(index, this.root,newNode);
        }
        foot=0;
    }


}

package lianbiao;
public class Test2 {
    public static void main(String[] args) {
       Link<String> link = new Link<>();
       //添加数据
       link.add("火车头");
       link.add("1车厢");
       link.add("2车厢");
       link.add("3车厢");
       link.add("4车厢");
       link.add("车尾");
//       System.out.println("修改前");
//       link.print();
//       link.set(0,"和谐号");
//       System.out.println("修改后");
//       link.print();
       System.out.print("插入前: ");
       link.print();
       System.out.println();
       link.add(1,"餐车");
       link.add(2,"卖票车");
       System.out.print("插入后: ");
       link.print();

    }

}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值