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

时间:2014-11-08 03:37:36

标签: java linked-list

我的问题在于添加方法。我想我知道我想要它做什么,但我无法弄清楚我应该用什么类型的循环来查看列表。正如你所看到的,我开始创建一个if else循环,但我无法弄清楚我应该使用什么作为计数器。我很确定我在处理添加时有正确的逻辑,但我觉得我还没到那里。我想以某种方式使用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;
       }  
   }    

}

2 个答案:

答案 0 :(得分:0)

由于你通常不知道链表的长度,直到你走下去,通常会使用while循环(就像你在toString()方法中所做的那样)

答案 1 :(得分:0)

也许使用双向链表会更有益。考虑对您的班级进行以下更改:

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);
   }

}