简体   繁体   中英

Confused about choosing a loop to iterate a linked list

My problem is in the add method. I think I know what I want it to do but I can't figure out what type of loop I should use to look through the list. As you can see I started to make a if else loop but I couldn't figure out what I should use as the counter. I'm pretty sure I have the right logic in dealing with the add but I feel like I'm not quite there yet. I was thinking of using compareTo in some fashion.

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()方法中所做的那样)

Perhaps using a doubly linked list would be more beneficial. Consider the following alterations to your class:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM