删除单链接和双链接列表的方法(Java)

时间:2017-04-07 20:41:47

标签: java

我正在努力了解如何实现remove();对于双重和单一链接类。我已经想出如何删除double中的第一个节点,但不是单个节点。首先我想调试,问题解决单个链接类,然后在之后处理double。

这是我到目前为止单链接类的代码。

public class SingleLinkedClass<T> {
    private Node <T> head;
    private Node <T>  tail;
    private int size;

    public SingleLinkedClass() {
        size = 0;
        head = null;
        tail = null;
    }

    public void insertAtHead(T v)
    {
      //Allocate new node
        Node newNode = new Node(v, head);

        //Change head to point to new node
        head = newNode;

        if(tail == null)
        {
            tail = head;
        }
        //Increase size
        size++;
    }

    public void insertAtTail(T v)
    {
        if(tail == null)
        {
            tail = new Node(v, null);
            head = tail;
            size++;
            return;
        }
        Node newNode = new Node(v, null);
        tail.nextNode = newNode;
        tail = newNode;
        size++;     
    }

    public T removeHead()
    {
        if(head == null)
        {
            throw new IllegalStateException("list is empty! cannot        delete");
        }
        T value = head.value;
        head = head.nextNode;
        size--;
        return value;
    }


    public void removeTail()
    {
        //Case 1: list empty
        if(head == null)
        {
            return;
        }

        //Case 2: list has one node
        else if(head == tail)
        {
            head = tail = null;
        }
        else
        {
            Node temp = head;
            while(temp.nextNode != tail)
            {
                temp = temp.nextNode;
            }
            tail = temp;
            tail.nextNode = null;
        }
        size--;
    }


    public boolean remove(T v) {
        Node<T> previous = head;
        Node<T> cursor = head.nextNode;
        if (head.nextNode == null) {
            return false;
        }

        while(cursor != tail){
             if (cursor.value.equals(v)) {
                 previous = cursor.nextNode;
                 return true;
        }   
             previous = cursor;
             cursor = cursor.nextNode;
        }
             return false;
        }

    public String toString() {
        if (head == null) {
            return "The list is Empty!";
        }
        String result = "";

        Node temp = head;

        while (temp != null) {
            result += temp.toString() + " ";
            temp = temp.nextNode;
        }

        return result;
    }

    public int size() {
        return size;
    }

    private class Node <T> {
        private T value;
        private Node <T> nextNode;

        public Node(T v, Node<T> n) {
            value = v;
            nextNode = n;
        }

        public String toString() {
            return "" + value;
        }
    }
}

这是我的Double Linked Class

public class DoubelyLinkedList<E> {

 private int size;
 private Node<E> header;
 private Node<E> trailer;

public DoubelyLinkedList() {
    size = 0;

    header = new Node<E>(null, null, null);
    trailer = new Node<E>(null, null, header);

    header.next = trailer;
}

public boolean remove(E v) {
    //If the list is empty return false
    if(header.next == trailer){
        return false;
    }
    //If v is the head of the list remove and return true
    Node <E> cursor = header.next;

    for (int i = 0; i < size; i++) {
    //Remove at Head
        if(cursor.value.equals(v)){
            removeAtHead(); 
    }
        cursor = cursor.next;
    }

    return true;

    }
    /*  
        } */



  public void insertAtHead(E v) {
    insertBetween(v, header, header.next);
   }

  public void insertAtTail(E v) {
    insertBetween(v, trailer.prev, trailer);
   }

  private void insertBetween(E v, Node<E> first, Node<E> second) {
    Node<E> newNode = new Node<>(v, second, first);
    first.next = newNode;
    second.prev = newNode;
    size++;
   }

  public E removeAtHead() {
    return removeBetween(header, header.next.next);
  }

 public E removeAtTail() {
    return removeBetween(trailer.prev.prev, trailer);
 }

 private E removeBetween(Node<E> first, Node<E> second) {
    if (header.next == trailer)// if the list is empty
    {
        throw new IllegalStateException("The list is empty!");
    }
    E result = first.next.value;
    first.next = second;
    second.prev = first;
    size--;

    return result;
   }

   public String toStringBackward() {
    if (size == 0) {
        return "The list is empty!";
    }
    String r = "";
    Node<E> temp = trailer.prev;
    while (temp != header) {
        r += temp.toString() + " ";
        temp = temp.prev;
    }
    return r;

   }

   public String toString() {
    if (size == 0) {
        return "The list is empty!";
    }
    String r = "";
    Node<E> temp = header.next;
    while (temp != trailer) {
        r += temp + " ";
        temp = temp.next;
    }
    return r;

    }

    private static class Node<T> {
      private T value;
      private Node<T> next;
      private Node<T> prev;

    public Node(T v, Node<T> n, Node<T> p) {
        value = v;
        next = n;
        prev = p;
    }

    public String toString() {
        return value.toString();
    }

    }

    }

这是我的驱动程序

public class Driver {

public static void main(String[] args) {
    DoubelyLinkedList<String> doubley = new DoubelyLinkedList();
    SingleLinkedClass<String> single = new SingleLinkedClass();

    single.insertAtHead("Bob");
    single.insertAtHead("Sam");
    single.insertAtHead("Terry");
    single.insertAtHead("Don");


    System.out.println(single);

    single.remove("Bob");
    System.out.println("Single Remove Head: " + single);
    /*  
    single.remove("Don");
    System.out.println("Single Remove Tail: " + single);

    single.remove("Terry");
    System.out.println("Single Remove Inbetween: " + single);
    */
    System.out.println();
    System.out.println();


    doubley.insertAtHead("Bob");
    doubley.insertAtHead("Sam");
    doubley.insertAtHead("Terry");
    doubley.insertAtHead("Don");

    System.out.println(doubley);


    doubley.remove("Bob");
    System.out.println("Double Remove Head: " + doubley);

    doubley.remove("Don");
    System.out.println("Double Remove Tail: " + doubley);
    /*
    doubley.remove("Sam");
    System.out.println("Double Remove Inbetween: " + doubley);
    */


}

}

1 个答案:

答案 0 :(得分:0)

removeHead移动头到下一个,它可能会变为空。然后尾巴也是头。然后tail也应该设置为null。

DoublyLinkedList的英语优于DoubelyLinkedList

由于这是家庭作业,我将其留下。