Class DogLinkedList<T>

java.lang.Object
org.ddogleg.struct.DogLinkedList<T>

public class DogLinkedList<T> extends Object
A double linked list. Internal data structures are recycled to minimize creation of new memory.
  • Field Details

  • Constructor Details

    • DogLinkedList

      public DogLinkedList()
  • Method Details

    • reset

      public void reset()
      Puts the linked list back into its initial state. Elements are saved for later use.
    • isEmpty

      public boolean isEmpty()
      Checks to see if there are no elements in the list
      Returns:
      true if empty or false if not
    • getElement

      public DogLinkedList.Element<T> getElement(int index, boolean fromFront)
      Returns the N'th element when counting from the from or from the back
      Parameters:
      index - Number of elements away from the first or last element. Must be positive.
      Returns:
      if true then the number of elements will be from first otherwise last
    • pushHead

      public DogLinkedList.Element<T> pushHead(T object)
      Adds the element to the front of the list.
      Parameters:
      object - Object being added.
      Returns:
      The element it was placed inside of
    • pushTail

      public DogLinkedList.Element<T> pushTail(T object)
      Adds the element to the back of the list.
      Parameters:
      object - Object being added.
      Returns:
      The element it was placed inside of
    • insertAfter

      public DogLinkedList.Element<T> insertAfter(DogLinkedList.Element<T> previous, T object)
      Inserts the object into a new element after the provided element.
      Parameters:
      previous - Element which will be before the new one
      object - The object which goes into the new element
      Returns:
      The new element
    • insertBefore

      public DogLinkedList.Element<T> insertBefore(DogLinkedList.Element<T> next, T object)
      Inserts the object into a new element before the provided element.
      Parameters:
      next - Element which will be after the new one
      object - The object which goes into the new element
      Returns:
      The new element
    • swap

      public void swap(DogLinkedList.Element<T> a, DogLinkedList.Element<T> b)
      Swaps the location of the two elements
      Parameters:
      a - Element
      b - Element
    • remove

      public void remove(DogLinkedList.Element<T> element)
      Removes the element from the list and saves the element data structure for later reuse.
      Parameters:
      element - The item which is to be removed from the list
    • removeHead

      public T removeHead()
      Removes the first element from the list
      Returns:
      The object which was contained in the first element
    • removeTail

      public T removeTail()
      Removes the last element from the list
      Returns:
      The object which was contained in the last element
    • find

      @Nullable public @Nullable DogLinkedList.Element<T> find(T object)
      Returns the first element which contains 'object' starting from the head.
      Parameters:
      object - Object which is being searched for
      Returns:
      First element which contains object or null if none can be found
    • getHead

      @Nullable public @Nullable DogLinkedList.Element<T> getHead()
      Returns the first element in the list
      Returns:
      first element
    • getTail

      @Nullable public @Nullable DogLinkedList.Element<T> getTail()
      Returns the last element in the list
      Returns:
      last element
    • getFirst

      public T getFirst()
      Returns the value in the head
    • getLast

      public T getLast()
      Returns the value in the trail
    • addAll

      public void addAll(List<T> list)
      Add all elements in list into this linked list
      Parameters:
      list - List
    • addAll

      public void addAll(T[] array, int first, int length)
      Adds the specified elements from array into this list
      Parameters:
      array - The array
      first - First element to be added
      length - The number of elements to be added
    • size

      public int size()
      Returns the number of elements in the list
    • requestNew

      protected DogLinkedList.Element<T> requestNew()
      Returns a new element. If there are old elements available those are returned, otherwise a new one is returned.
      Returns:
      Unused element.