Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.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_Linked List - Fatal编程技术网

Java 对选择循环来迭代链表感到困惑吗

Java 对选择循环来迭代链表感到困惑吗,java,linked-list,Java,Linked List,我的问题是add方法。我想我知道我想要它做什么,但我不知道我应该使用什么类型的循环来查看列表。正如你所看到的,我开始做一个if-else循环,但是我不知道应该用什么作为计数器。我很确定我在处理add时有正确的逻辑,但我觉得我还没有完全做到这一点。我在考虑以某种方式使用compareTo import java.util.*; public class OrderedLinkedList<E extends Comparable<E>> { private Node

我的问题是add方法。我想我知道我想要它做什么,但我不知道我应该使用什么类型的循环来查看列表。正如你所看到的,我开始做一个if-else循环,但是我不知道应该用什么作为计数器。我很确定我在处理add时有正确的逻辑,但我觉得我还没有完全做到这一点。我在考虑以某种方式使用compareTo

import java.util.*;
public class OrderedLinkedList<E extends Comparable<E>>
{
   private Node topNode;
   private class Node
   {
       private E data;
       private Node nextNode;

       public Node(E data)
       {
           this.data = data;
           nextNode = null;
       }
   }
   public OrderedLinkedList()
   {
       topNode = null;
   }   

   public boolean empty()
   {
       if(topNode == null)
        return true;
       return false; 
   }    

   public String toString()
   {
       String myString = "";
       Node nextNode = topNode;
       while(nextNode != null)
       {
           myString = topNode + " -> " + nextNode;
           nextNode = topNode.nextNode;
       } 
       return myString;
   }    

   public void add(E data)
   {
       Node myNode = new Node(data);
       Node priorNode = topNode;
       Node currentNode = topNode;
       if(___)
       {
           priorNode = currentNode;
           currentNode = currentNode.nextNode;
       }
       else
       {
          priorNode.nextNode = myNode;
          myNode.nextNode = currentNode;
       }  
   }    

}

因为通常在遍历链表之前,您不知道链表的长度,所以通常的做法是使用while循环,就像您在toString方法中所做的那样

也许使用双链表会更有益。考虑下面对你班的修改:

import java.util.*;
public class OrderedLinkedList<E extends Comparable<E>>
{
   private Node head;
   private Node tail;

   private class Node
   {
       private E data;
       private Node nextNode;
       private Node prevNode;

       public Node(E data)
       {
           this.data = data;
           nextNode = null;
           prevNode = null;
       }

       public void setNext(Node node)
       {
           this.nextNode = node;
       }

       public Node getNext()
       {
           return this.nextNode;
       }

       public void setPrev(Node node)
       {
           this.prevNode = node;
       }

       public Node getPrev()
       {
           return this.prevNode;
       }

       public E getData()
       {
           return this.data;
       }

       public int compareTo(Node that) {
           if(this.getData() < that.getData())
           {
               return -1;
           }
           else if(this.getData() == that.getData()
           {
               return 0;
           }
           else
           {
               return 1;
           }
       } 
   }

   public OrderedLinkedList()
   {
       head = new Node(null);
       tail = new Node(null);

       head.setNext(tail);
       tail.setPrev(head);
   }   

   public boolean empty()
   {
       if(head.getNext() == tail)
       {
           return true;
       }
       return false; 
   }

   public void add(E data) {
       Node tmp = new Node(data);

       if(this.empty()) {
           this.addNodeAfterNode(tmp, head);
       } else {
          Node that = head.getNext();

          // this while loop iterates over the list until finding the correct
          // spot to add the new node. The correct spot is considered to be
          // when tmp's data is >= that's data, or the next node after 'that'
          // is tail. In which case the node is added to the end of the list
          while((tmp.compareTo(that) == -1) && (that.getNext() != tail)) {
              that = that.getNext();
          }

          this.addNodeAfterNode(tmp, that);
       }
   }

   private void addNodeAfterNode(Node addNode, Node afterNode)
   {
       addNode.setNext(afterNode.getNext());
       afterNode.getNext().setPrev(addNode);
       afterNode.setNext(addNode);
       addNode.setPrev(afterNode);
   }

}

if不是任何类型的循环。我的意思是if-else sorryif-else也不是循环-循环是根据给定条件的计算结果是否为真而重复一组操作的结构。我考虑过这一点,但我如何让它在某个点停止以添加新节点?嗯,这取决于你对该点的了解。也许你想在末尾加上它,然后在next不为null的时候就可以了。另一方面,如果你试图保持某种排序顺序,你可以说下一个节点的排序比要插入的节点少——例如,如果你有一个a->c->f->q的列表,你想插入d,你会看a,继续,看c,cd,所以在c之后插入。明白了吗?我想是的。我可以做一些类似whilenextNode的事情。与newnodeeyes相比,我认为你已经有了这个想法。