Class CircularDLinkedList<X>

java.lang.Object
Jcg.util.CircularDLinkedList<X>

public class CircularDLinkedList<X> extends Object
Minimal implementation of a circular doubly linked list, storing generic type elements
Author:
Luca Castelli Aleardi (Ecole Polytechnique, 2021-2024)
  • Field Details

    • first

      public DListNode<X> first
      a reference to a node in the list (the HEAD)
    • size

      private int size
      number of nodes in the list
  • Constructor Details

    • CircularDLinkedList

      public CircularDLinkedList(DLinkedList<X> list)
      Create a circular linked list from a doubly linked list (not circular).

      Warning: the input 'list' is modified.
  • Method Details

    • clone

      public CircularDLinkedList<X> clone()
      Create a clone (a copy) of a circular doubly linked list.

      Remark: the two lists shared the elements, but the nodes are different.
      The original (current) list is not modified.
      Overrides:
      clone in class Object
    • size

      public int size()
    • isEmpty

      public boolean isEmpty()
      Check whether the list is empty
    • getFirst

      public DListNode<X> getFirst()
      Return the first node of the list The result is a null reference if the list is empty
    • insertAfter

      public void insertAfter(DListNode<X> pNode, X el)
      Insert a new element after a given node in the list
      Parameters:
      el - the element to insert
      nPnode - the place where the element will be inserted
    • insertBefore

      public void insertBefore(DListNode<X> pNode, X el)
      Insert a new element before a given node in the list
      Parameters:
      el - the element to insert
      nPnode - the place where the element will be inserted
    • delete

      public void delete(DListNode<X> pNode)
      Remove a given node from the list
      Parameters:
      nPnode - the node to delete
    • toString

      public String toString()
      Return a String representing the list
      Overrides:
      toString in class Object
    • containsNode

      public boolean containsNode(DListNode<X> node)
      Check whether the circular list contains a given node (check reference equality)

      Remark: useful for debugging
    • main

      public static void main(String[] args)