0

リンクリストの定義方法を再実装するように求められました。タスクは次のとおりです。LinkedListクラスの最初のノードへの参照を削除して、リストの最後の要素のみを追跡するようにします。また、このリンクリストが循環リンクリストになるように、最後の要素から最初の要素へのnext()参照を作成するように求められます。これを行うためのエレガントな方法はありますか?

これは私がこれまでに持っているものです:

import java.util.NoSuchElementException;

public class LinkedList
{  
   private Node last;

   /** 
      Constructs an empty linked list.
   */
   public LinkedList()
   {  
      last = null;
   }

   /**
      Returns the first element in the linked list.
      @return the first element in the linked list
   */
   public Object getFirst()
   {  
      if (last == null) 
         throw new NoSuchElementException();
      return last.next.data;
   }

   /**
      Removes the first element in the linked list.
      @return the removed element
   */
   public Object removeFirst()
   {  
      if (last == null) 
         throw new NoSuchElementException();
      Object element = last.next.data;
      last.next = last.next.next;
      return element;
   }

   /**
      Adds an element to the front of the linked list.
      @param element the element to add
   */
   public void addFirst(Object element)
   {  
        if( last == null ){
          last = new Node();
          last.data = element;
          last.next = last;
        }
        else{
      Node newNode = new Node();
      newNode.data = element;
      last.next = newNode;
      newNode.next = last.next;
      }
   }

   /**
      Returns an iterator for iterating through this list.
      @return an iterator for iterating through this list
   */
   public ListIterator listIterator()
   {  
      return new LinkedListIterator();
   }

   class Node
   {  
      public Object data;
      public Node next;
   }

   class LinkedListIterator implements ListIterator
   {  
      private Node position;
      private Node previous;
      /**
         Constructs an iterator that points to the front
         of the linked list.
      */
      public LinkedListIterator()
      {  
         position = null;
         previous = null;
      }

      /**
         Moves the iterator past the next element.
         @return the traversed element
      */
      public Object next()
      {  
         if (!hasNext())
            throw new NoSuchElementException();
         previous = position; // Remember for remove

         if (position == null)
            position = last.next;
         else
            position = position.next;

         return position.data;
      }

      /**
         Tests if there is an element after the iterator position.
         @return true if there is an element after the iterator position
      */
      public boolean hasNext()
      {  
         if (position == null)
            return last != null;
         else
            return position.next != null;
      }

      /**
         Adds an element before the iterator position
         and moves the iterator past the inserted element.
         @param element the element to add
      */
      public void add(Object element)
      {  
         if (position == null)
         {
            addFirst(element);
            position = last;
         }
         else
         {  
            Node newNode = new Node();
            newNode.data = element;
            newNode.next = position.next;
            position.next = newNode;
            position = newNode;
         }
         previous = position;
      }

      /**
         Removes the last traversed element. This method may
         only be called after a call to the next() method.
      */
      public void remove()
      {  
         if (previous == position)
            throw new IllegalStateException();

         if (position == last)
         {
            removeFirst();
         }
         else 
         {  
            previous.next = position.next;
         }
         position = previous;
      }

      /**
         Sets the last traversed element to a different value. 
         @param element the element to set
      */
      public void set(Object element)
      {
         if (position == null)
            throw new NoSuchElementException();
         position.data = element;
      }
   }
}

これは元のコードでした:

import java.util.NoSuchElementException;

/**
   A linked list is a sequence of nodes with efficient
   element insertion and removal. This class 
   contains a subset of the methods of the standard
   java.util.LinkedList class.
*/
public class LinkedList
{  
   private Node first;

   /** 
      Constructs an empty linked list.
   */
   public LinkedList()
   {  
      first = null;
   }

   /**
      Returns the first element in the linked list.
      @return the first element in the linked list
   */
   public Object getFirst()
   {  
      if (first == null) 
         throw new NoSuchElementException();
      return first.data;
   }

   /**
      Removes the first element in the linked list.
      @return the removed element
   */
   public Object removeFirst()
   {  
      if (first == null) 
         throw new NoSuchElementException();
      Object element = first.data;
      first = first.next;
      return element;
   }

   /**
      Adds an element to the front of the linked list.
      @param element the element to add
   */
   public void addFirst(Object element)
   {  
      Node newNode = new Node();
      newNode.data = element;
      newNode.next = first;
      first = newNode;
   }

   /**
      Returns an iterator for iterating through this list.
      @return an iterator for iterating through this list
   */
   public ListIterator listIterator()
   {  
      return new LinkedListIterator();
   }

   class Node
   {  
      public Object data;
      public Node next;
   }

   class LinkedListIterator implements ListIterator
   {  
      private Node position;
      private Node previous;
      /**
         Constructs an iterator that points to the front
         of the linked list.
      */
      public LinkedListIterator()
     {  
         position = null;
         previous = null;
      }

      /**
         Moves the iterator past the next element.
         @return the traversed element
      */
      public Object next()
      {  
         if (!hasNext())
            throw new NoSuchElementException();
         previous = position; // Remember for remove

         if (position == null)
            position = first;
         else
            position = position.next;

         return position.data;
      }

      /**
         Tests if there is an element after the iterator position.
         @return true if there is an element after the iterator position
      */
      public boolean hasNext()
      {  
         if (position == null)
            return first != null;
         else
            return position.next != null;
      }    

      /**
         Adds an element before the iterator position
         and moves the iterator past the inserted element.
         @param element the element to add
      */
      public void add(Object element)
      {  
         if (position == null)
         {
            addFirst(element);
            position = first;
         }
         else
         {  
            Node newNode = new Node();
            newNode.data = element;
            newNode.next = position.next;
            position.next = newNode;
            position = newNode;
         }
         previous = position;
      }

      /**
         Removes the last traversed element. This method may
         only be called after a call to the next() method.
      */
      public void remove()
      {  
         if (previous == position)
            throw new IllegalStateException();

         if (position == first)
         {
            removeFirst();
         }
         else 
         {  
            previous.next = position.next;
         }
         position = previous;
      }

      /**
         Sets the last traversed element to a different value. 
         @param element the element to set
      */
      public void set(Object element)
      {
         if (position == null)
            throw new NoSuchElementException();
         position.data = element;
      }
   }
}
4

2 に答える 2

1

あなたがする必要があるのは、Nodeクラスを次のように変更することだと思います。

class Node {
    // point to the previous node in the linked list
    private Node prev;
    private Object data;   
}

これで、リンクリストは一種の後方リンクになり、LinkedListでは「テール」を追跡するだけで済み、テールからのリンクをたどってリンクリストの任意のノードを取得できます。

LinkedListを円にするには、リンクリストのヘッドノード(最初のノード)の「prev」フィールドが常にテールを指していることを確認するだけです。これを行う方法は次のとおりです。

  1. リストが空の場合は、何もしません:)
  2. 最初のノードがリストに追加されたら、LinkedListの「tail」をそのノードにポイントし、ノードの「prev」もそれ自体にポイントします。ノードが尻尾と頭の両方だからですよね?
  3. さらにノードを追加するときは、最初に「テール」からリンクをたどって「テール」にリンクするノードが見つかるまで、LinkedListでヘッドノードを見つけます。次に、新しいノードを追加し、それに応じてヘッド/テールノードのリンクを更新します。
于 2012-09-06T08:04:29.217 に答える
0

不正な操作が試みられた場合に適切な例外をスローする、セクション 3.4.1 の循環リンク リスト データ構造のより堅牢な実装を提供します。

于 2013-03-09T16:01:15.033 に答える