文章

最简单的单向链表实现

2020.11.30 ・ 共 374 字,您可能需要 1 分钟阅读

Tags: 代码

interface ILink<E> {
  public void add(E e);

  public int size();

  public boolean isEmpty();

  public Object[] toArray();

  public E get(int index);

  public void set(int index, E data);

  public boolean contains(E data);

  public void remove(E data);

  public void clean();
}

class LinkImpl<E> implements ILink<E> {
  private class Node {
    private E data;
    private Node next;

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

    public void addNode(Node newNode) {
      if (this.next == null) {
        this.next = newNode;
      } else {
        this.next.addNode(newNode);
      }
    }

    public void toArrayNode() {
      LinkImpl.this.returnData[foot++] = this.data;
      if (this.next != null) {
        this.next.toArrayNode();
      }
    }

    public E getNode(int index) {
      if (LinkImpl.this.foot++ == index) {
        return this.data;
      }
      return this.next.getNode(index);
    }

    public void setNode(int index, E data) {
      if (LinkImpl.this.foot++ == index) {
        this.data = data;
      } else {
        this.next.setNode(index, data);
      }
    }

    public boolean containsNode(E data) {
      if (this.data.equals(data)) {
        return true;
      } else {
        if (this.next == null) {
          return false;
        } else {
          return this.next.containsNode(data);
        }
      }
    }

    public void removeNode(Node previous, E data) {
      if (this.data.equals(data)) {
        previous.next = this.next;
      } else {
        if (this.next != null) {
          this.next.removeNode(this, data);
        }
      }
    }
  }

  private Node root;
  private int count;
  private int foot;
  private Object[] returnData;

  public int size() {
    return this.count;
  }

  public boolean isEmpty() {
    return this.count == 0;
  }

  public void add(E data) {
    if (data == null) {
      return;
    }

    Node newNode = new Node(data);
    if (this.root == null) {
      this.root = newNode;
    } else {
      this.root.addNode(newNode);
    }
    this.count++;
  }

  public Object[] toArray() {
    if (isEmpty()) return null;
    this.foot = 0;
    this.returnData = new Object[this.count];
    this.root.toArrayNode();
    return this.returnData;
  }

  public E get(int index) {
    if (index >= this.count) {
      return null;
    }
    this.foot = 0;
    return this.root.getNode(index);
  }

  public void set(int index, E data) {
    if (index >= this.count) {
      return;
    }
    this.foot = 0;
    this.root.setNode(index, data);
  }

  public boolean contains(E data) {
    if (data == null) {
      return false;
    }

    return this.root.containsNode(data);
  }

  public void remove(E data) {
    if (this.contains(data)) {
      if (this.root.data.equals(data)) {
        this.root = this.root.next;
      } else {
        this.root.next.removeNode(this.root, data);
      }
      this.count--;
    }
  }

  public void clean() {
    this.root = null;
    this.count = 0;
  }
}

public class Test {
  public static void main(String[] args) {
    ILink<String> all = new LinkImpl<>();
  }
}