07 | 链表(下):如何轻松写出正确的链表代码?

package com.example.demo1;


public class MyList<T> {
  private int count = 0;
  private Node<T> firstNode = null;
  private Node<T> lastNode = null;

  public Node<T> getFirstNode() {
    return firstNode;
  }

  public Node<T> getLastNode() {
    return lastNode;
  }

  private static class Node<T> {
    Node<T> preNode;
    T t;
    Node<T> nextNode;

    public Node(Node<T> preNode, T t, Node<T> nextNode){
      this.preNode = preNode;
      this.t = t;
      this.nextNode = nextNode;
    }

    public Node<T> getPreNode() {
      return preNode;
    }

    public T getT() {
      return t;
    }

    public Node<T> getNextNode() {
      return nextNode;
    }

    public void setPreNode(Node<T> preNode) {
      this.preNode = preNode;
    }

    public void setNextNode(Node<T> nextNode) {
      this.nextNode = nextNode;
    }
  }
  public void add(T t){
    Node<T> node = new Node<>(lastNode,t,null);
    if(count == 0){
      firstNode = node;
      lastNode = node;
    }else{
      lastNode.nextNode = node;
      lastNode = node;
    }
    count ++;
  }

  public void addFirst(T t){
    Node<T> node = new Node<>(null,t,firstNode);
    if(count == 0){
      lastNode = node;
      firstNode = node;
    }else{
      firstNode.preNode = node;
      firstNode = node;
    }
    count ++;
  }
  public void remove(){
    if(count == 0){
      return;
    }
    if(count == 1){
      firstNode = null;
      lastNode = null;
    }else{
      lastNode = lastNode.getPreNode();
      lastNode.setNextNode(null);
    }
    count --;
  }
  public void removeFirst(){
    if(count == 0){
      return;
    }
    if(count == 1){
      firstNode = null;
      lastNode = null;
    }else{
      firstNode = firstNode.getNextNode();
      firstNode.setPreNode(null);
    }
    count --;
  }

  public int size(){
    return count;
  }

  public MyList<T> reverse(){
    MyList<T> newList = new MyList<>();
    if(count == 0){
      return newList;
    }
    Node<T> tmpNode = lastNode;
    while(tmpNode!=null){
      newList.add(tmpNode.getT());
      tmpNode = tmpNode.getPreNode();
    }
    return newList;
  }

  public void insertAfter(Node<T> node, T insertValue){
    Node<T> insertNode = new Node<>(node,insertValue,node.nextNode);
    node.nextNode.preNode = insertNode;
    node.nextNode = insertNode;
    count ++;
  }

  public static void main(String[] args){
    MyList<String> myList = new MyList<>();
    myList.add("1");
    myList.add("2");
    myList.add("3");
    myList.add("4");
    myList.add("5");
    Node<String> tmpNode = myList.getFirstNode();
    while (tmpNode!=null){
      System.out.println(tmpNode.getT());
      tmpNode = tmpNode.getNextNode();
    }
    MyList<String> reverseList = myList.reverse();
    tmpNode = reverseList.getFirstNode();
    reverseList.insertAfter(tmpNode,"0");
    while (tmpNode!=null){
      System.out.println(tmpNode.getT());
      tmpNode = tmpNode.getNextNode();
    }
  }
}
package com.example.demo1;


public class MyHeadList<T> {
  private int count = 0;
  private Node<T> head;
  private Node<T> last;

  {
    head = new Node<>(null,null,null);
    last = new Node<>(head,null,null);
    head.setNextNode(last);
  }

  public Node<T> getHead() {
    return head;
  }

  public Node<T> getLast() {
    return last;
  }


  static class Node<T> {
    Node<T> preNode;
    T t;
    Node<T> nextNode;

    public Node(Node<T> preNode, T t, Node<T> nextNode){
      this.preNode = preNode;
      this.t = t;
      this.nextNode = nextNode;
    }

    public Node<T> getPreNode() {
      return preNode;
    }

    public T getT() {
      return t;
    }

    public Node<T> getNextNode() {
      return nextNode;
    }

    public void setPreNode(Node<T> preNode) {
      this.preNode = preNode;
    }

    public void setNextNode(Node<T> nextNode) {
      this.nextNode = nextNode;
    }
  }
  public void add(T t){
    Node<T> node = new Node<>(last.preNode,t,last);
    last.preNode.nextNode = node;
    last.preNode = node;
    count ++;
  }

  public void addFirst(T t){
    Node<T> node = new Node<>(head,t,head.nextNode);
    head.nextNode.preNode = node;
    head.nextNode = node;
    count ++;
  }
  public void remove(){
    last.preNode = last.preNode.preNode;
    last.preNode.nextNode = last;
    count --;
  }
  public void removeFirst(){
    head.nextNode = head.nextNode.nextNode;
    head.nextNode.preNode = head;
    count --;
  }

  public int size(){
    return count;
  }

  public MyHeadList<T> reverse(){
    MyHeadList<T> newList = new MyHeadList<>();
    if(count == 0){
      return newList;
    }
    Node<T> tmpNode = last.preNode;
    while(tmpNode!=head){
      newList.add(tmpNode.getT());
      tmpNode = tmpNode.getPreNode();
    }
    return newList;
  }

  public void insertAfter(Node<T> node, T insertValue){
    Node<T> insertNode = new Node<>(node,insertValue,node.nextNode);
    node.nextNode.preNode = insertNode;
    node.nextNode = insertNode;
    count ++;
  }

  public static void main(String[] args){
    MyHeadList<String> myList = new MyHeadList<>();
    myList.add("1");
    myList.add("2");
    myList.add("3");
    myList.add("4");
    myList.add("5");
    Node<String> tmpNode = myList.getHead().nextNode;
    while (tmpNode!=myList.getLast()){
      System.out.println(tmpNode.getT());
      tmpNode = tmpNode.getNextNode();
    }
    MyHeadList<String> reverseList = myList.reverse();
    tmpNode = reverseList.getHead().nextNode;
    reverseList.insertAfter(tmpNode,"0");
    while (tmpNode!=reverseList.getLast()){
      System.out.println(tmpNode.getT());
      tmpNode = tmpNode.getNextNode();
    }
  }
}

猜你喜欢

转载自www.cnblogs.com/lakeslove/p/12316020.html
07