模拟一个LinkedList

1.创建List接口,完成需要实现的接口功能定义

package com.page.pagetookit.service;

public interface IList<T> {
    boolean add(T t);
    boolean remove(T t);
    boolean contains(T t);
    boolean isEmpty();
    boolean clean();
    T[] toArray();
    int size();
    T get(int index);
    T set(int index, T value);
}

2.创建节点类
注意:移除的根节点由List判断

package com.page.pagetookit.entity;

import com.page.pagetookit.service.impl.ILinkedList;
import lombok.Data;

import javax.sound.midi.SoundbankResource;

/**
 * @author xiefenghong
 * @version V1.0
 * @date 2022/2/18
 * @description node节点
 */
@Data
public class Node<T> {
    private T data;
    private Node<T> next;

    public Node(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        String statement = "";
        Node<T> node = this;
        while (node != null) {
            statement +=","+node.data;
            node=node.next;
        }
        return statement.substring(1);
    }

    public void add(Node<T> o) {
        if(this.next == null){
            this.next=o;
        }else{
            this.next.add(o);
        }

    }

    public Node<T> getNode(ILinkedList link,Integer index) {
        int foot = link.getFoot();
        if(index < 0) {
            throw new IllegalArgumentException();
        }
        if (foot++==index){
            return this;
        }
        link.setFoot(foot);
        return this.next.getNode(link,index);
    }

    public boolean contains(T o) {
        if (this.data==o){
            return true;
        }
        if (this.next==null){
            return false;
        }
        return this.next.contains(o);
    }

    public void remove(T o) {
        if(this.next.getData()==o){
            this.next=this.next.next;
            return;
        }
        this.next.remove(o);
    }
}

3.List实现类创建
注意:list类负责管理根节点和统计节点数,节点的细节实现由节点类处理。由于节点类也会需要用到List实现类的内部参数,为了方便可以将节点类创建为List实现类的内部类。

package com.page.pagetookit.service.impl;

import com.page.pagetookit.entity.Node;
import com.page.pagetookit.service.IList;
import lombok.Data;

import java.util.Objects;

/**
 * @author xiefenghong
 * @version V1.0
 * @date 2022/2/18
 * @description linedlist
 */
public class ILinkedList<T> implements IList<T> {
    private Node<T> root;
    private int count;
    private int foot;
    @Override
    public boolean add(T o) {
        count++;
        Node<T> node = new Node<T>(o);
        if(this.root == null){
            this.root =node;
        }else{
            this.root.add(node);
        }
        return true;
    }

    @Override
    public boolean remove(T o) {
        if (!contains(o)){
            return false;
        }
        if (this.root.getData()==o){
            this.root=this.root.getNext();
            return true;
        }
        this.root.remove(o);
        return true;
    }

    @Override
    public boolean contains(T o) {
        return root.contains(o);
    }

    @Override
    public boolean isEmpty() {
        return root == null;
    }

    @Override
    public boolean clean() {
        root=null;
        count=0;
        return true;
    }

    @Override
    public T[] toArray() {
        T[] objects = (T[])new Object[count];
        int i = 0;
        for (Node<T> node =root; node!=null; node=node.getNext()) {
            objects[i++] = node.getData();
        }
        return objects;
    }

    @Override
    public int size() {
        return count;
    }

    @Override
    public T get(int index) {
        if(index >= count) {
            return null;
        }
        this.foot=0;
        return root.getNode(this,index).getData();
    }

    @Override
    public T set(int index, T value) {
        if(index >= count) {
            return null;
        }
        this.foot=0;
        Node<T> node = root.getNode(this, index);
        T t=node.getData();
        node.setData(value);
        return t;
    }

    @Override
    public String toString() {
        if (Objects.isNull(root)){
            return "{}";
        }
        return "ILinkedList{" +root.toString()+
                '}';
    }

    public int getFoot() {
        return foot;
    }

    public void setFoot(int foot) {
        this.foot = foot;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public Node<T> getRoot() {
        return root;
    }

    public void setRoot(Node<T> root) {
        this.root = root;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值