Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不知道如何实现仅以最后一个节点为参考的循环链表_Java_Singly Linked List - Fatal编程技术网

Java 不知道如何实现仅以最后一个节点为参考的循环链表

Java 不知道如何实现仅以最后一个节点为参考的循环链表,java,singly-linked-list,Java,Singly Linked List,我被要求重新实现链表的定义方式。任务是:删除对LinkedList类中第一个节点的引用,以便只跟踪列表中的最后一个元素。我还被要求将最后一个元素的next()引用到第一个元素,这样这个链表就变成了一个循环链表。有没有一种优雅的方法可以做到这一点 这就是我到目前为止所做的: import java.util.NoSuchElementException; public class LinkedList { private Node last; /** Constr

我被要求重新实现链表的定义方式。任务是:删除对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;
      }
   }
}

我认为您需要做的是将Node类修改为如下内容:

class Node {
    // point to the previous node in the linked list
    private Node prev;
    private Object data;   
}
所以现在你的链表是反向链接的,在LinkedList中你只需要跟踪“尾部”,你就可以从尾部跟踪链接来获得链表的任何节点,对吗

现在要使LinkedList成为一个圆圈,您需要做的就是确保链接列表的头节点(第一个节点)的“prev”字段始终指向尾部。以下是如何做到这一点:

  • 当列表为空时,不执行任何操作:)
  • 将第一个节点添加到列表中时,将LinkedList中的“tail”指向该节点,并将该节点的“prev”指向其自身。这是因为节点既是尾部又是头部,对吗
  • 当添加更多节点时,首先在LinkedList中查找头节点,从“tail”开始一直跟踪链接,直到找到链接到“tail”的节点。然后添加新节点,并相应地更新头部/尾部节点的链接

  • 给出循环链表数据结构的一个更健壮的实现 第3.4.1节中,如果
    尝试操作

    如果您没有工作代码示例,则发布少量您感到困惑的代码更有用。如果确实有一个工作代码示例,您可以考虑将其发布到