java Java中的LinkedList DataStructure

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Java中的LinkedList DataStructure相关的知识,希望对你有一定的参考价值。

package com.art.datastructures;

import java.util.NoSuchElementException;

public class SinglyLinkedList<T> {

  Node head;

  SinglyLinkedList() {
    this.head = null;
  }

  class Node {

    private T data;
    private Node next;

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

  /**
   * Inserts new Node at the beginning of the List
   */
  void addFirst(T data) {
    head = new Node(data, head);
  }

  /**
   * Insert new Node at the end of the List
   */
  void addLast(T data) {
    if (head == null) {
      addFirst(data);
    } else {
      // Get Temp Pointer for Head; Traverse Nodes till End
      Node tmp = head;
      while (tmp.next != null) {
        tmp = tmp.next;
      }
      tmp.next = new Node(data, null);
    }
  }

  /**
   * Insert new Node after the Key
   */
  void insertAfter(T key, T data) {
    Node tmp = head;

    while (tmp != null && !tmp.data.equals(key)) {
      tmp = tmp.next;
    }

    if (tmp != null) {
      tmp.next = new Node(data, tmp.next);
    }
  }

  /**
   * Removes First Element in the List
   */
  T removeFirst() {
    T tmp = getFirst();
    head = head.next;
    return tmp;
  }

  private T getFirst() {
    if (head == null) {
      throw new NoSuchElementException();
    }
    return head.data;
  }


  /**
   * Helper Override to Print LinkedList
   */
  @Override
  public String toString() {
    StringBuffer sb = new StringBuffer();
    Node currentNode = head;

    while (currentNode != null) {
      sb.append(currentNode.data);
      sb.append(" -- ");
      currentNode = currentNode.next;
    }
    if (currentNode == null) {
      sb.append(" [null]");
    }
    return sb.toString();
  }
}
package com.art.datastructures;

public class LinkedListDemo {
  public static void main(String[] args) {
    singlyLinkedList();
    doublyLinkedList();
  }

  private static void singlyLinkedList(){
    SinglyLinkedList<String> myList = new SinglyLinkedList<>();

    myList.addLast("BLUE");
    System.out.println(myList);

    myList.addLast("YELLOW");
    System.out.println(myList);

    myList.addLast("PURPLE");
    System.out.println(myList);

    myList.addFirst("RED");
    System.out.println(myList);

    myList.insertAfter("RED", "PINK");
    System.out.println(myList);

    myList.removeFirst();
    System.out.println(myList);
  }

  private static void doublyLinkedList(){
    DoublyLinkedList<String> myList = new DoublyLinkedList<>();
    myList.addFirst("BLUE");
    System.out.println(myList);

    myList.addFirst("YELLOW");
    System.out.println(myList);

    myList.addLast("PURPLE");
    System.out.println(myList);

    myList.removeFirst();
    System.out.println(myList);
  }

}
package com.art.datastructures;

import com.google.common.collect.Lists;
import java.util.LinkedList;

/*
Example of LinkedList Implementation in JDK

LinkedList is a doubly-linked list implementation of the List and Deque interfaces.
It implements all optional list operations and permits all elements (including null).

Usage
* Add Element
* Remove Element
* Queue Operations

*/
public class JavaLinkedListDemo {

  public static void main(String[] args) {

    LinkedList<String> myList = Lists.newLinkedList();

    myList.addLast("BLUE");
    System.out.println(myList);

    myList.addLast("YELLOW");
    System.out.println(myList);

    myList.addLast("PURPLE");
    System.out.println(myList);

    myList.addFirst("RED");
    System.out.println(myList);

    myList.removeFirst();
    System.out.println(myList);
  }

}
package com.art.datastructures;

import java.util.NoSuchElementException;

public class DoublyLinkedList<T> {

  Node head;

  DoublyLinkedList() {
    this.head = null;
  }

  class Node {
    private T data;

    private Node previous;
    private Node next;

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

  void addFirst(T data){
    // For First Item
    if (head == null){
      head = new Node(data, null, head);
    } else {
      head = new Node(data, head.previous, head);
    }
  }

  void addLast(T data){
    if (head == null) {
      addFirst(data);
    } else {
      // Get Temp Pointer for Head; Traverse Nodes till End
      Node tmp = head;
      while (tmp.next != null) {
        tmp = tmp.next;
      }
      tmp.next = new Node(data, tmp.previous, null);
    }
  }

  /**
   * Removes First Element in the List
   */
  T removeFirst() {
    T tmp = getFirst();
    head = head.next;
    return tmp;
  }

  private T getFirst() {
    if (head == null) {
      throw new NoSuchElementException();
    }
    return head.data;
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    Node currentNode = head;

    while (currentNode != null){
      sb.append(currentNode.data);
      sb.append(" -- ");
      currentNode = currentNode.next;
    }

    if (currentNode == null) {
      sb.append(" [null]");
    }

    return sb.toString();
  }
}

以上是关于java Java中的LinkedList DataStructure的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Java 中的 LinkedList 获取节点条目参考

使用ListIterator在Java中的LinkedList上来回移动

[Java]ArrayList与LinkedList的模拟实现

Java 中的 LinkedList 实现,具有泛型并增强了

源码系列Java中的数据结构——栈,队列,链表与LinkedList

源码系列Java中的数据结构——栈,队列,链表与LinkedList