2014-11-05 171 views
0

我已經完成了將代碼添加到LinkedList中的代碼。現在我想按排序順序將元素插入到列表中。我怎樣才能做到這一點?添加方法應該按排序順序將元素插入到列表中

  public void add(String element) 
        { 

         if (isEmpty()) 
         { 
          first = new Node(element); 
          last = first; 
         } 
         else 
         { 
          // Add to end of existing list 
          last.next = new Node(element); 
          last = last.next; 
         }   
        } 

我的主類是本作的LinkedList和ArrayList這就要求SimpleLinkedList類的方法和SimpleArrayListClass

  package Comp10152_linkedlist; 


      import java.util.Random; 

      public class Comp10152_Lab4 
      { 
       public static void main(String[] args) 
       { 
       final int NUMBER_OF_ITERATIONS = 10; 
       String names[] = {"Amy", "Bob", "Al", "Beth", "Carol", "Zed", "Aaron"}; 
       SimpleLinkedList ll = new SimpleLinkedList(); 
       final int TOTALOPERATIONS = names.length * NUMBER_OF_ITERATIONS; 

       Random random = new Random(); 

       for (int i=0; i<NUMBER_OF_ITERATIONS;i++) 
       { 
        for (int j=0; j<names.length; j++) 
        ll.add(names[j]); 
       } 
       System.out.println("The members of list are:"); 
        System.out.println(ll); 
       // remove half of the items in the list by selecting randomly from names 
       for (int i=0; i<TOTALOPERATIONS/2;i++) 
       { 
        ll.remove(names[random.nextInt(names.length)]); 
       } 
       System.out.println("The members of list are:"); 
        System.out.println(ll); 
       SimpleArrayList al = new SimpleArrayList(); 
       try 
       { 
       for (int i=0; i<NUMBER_OF_ITERATIONS;i++) 
       { 
        for (int j=0;j<names.length;j++) 
        al.add(i,names[j]); 
       } 
        System.out.println("The members of array are:"); 
        System.out.println(al); 

       // remove half of the items in the list by selecting randomly from names 
       for (int i=0; i<TOTALOPERATIONS/2;i++) 
       { 
        al.remove(names[random.nextInt(names.length)]); 
       } 
       System.out.println("The members of array are:"); 
        System.out.println(al); 
       } 
       catch (Exception e) 
       { 
        System.out.println(e); 
       } 
       }  
      } 

回答

0

首先插入列表之外的元素,然後通過調用插入到列表添加方法。 如何對列表之外的元素進行排序取決於您使用的數據結構,數據的類型以及要應用的算法。

0

雖然插入到列表中本身會添加排序順序。 首先搜索排序列表中比元素大的元素,然後搜索要插入的元素,然後在該元素之前添加新元素。

喜歡的東西..

//Considering ascending order 
public void add(String element) { 
    if(isEmpty) { 
     first = new Node(element); 
     last = first; 
    } else { 
     currentNode = first; 
     while(currentNode.next != null && currentNode.next.element > element) { 
      currentNode = currentNode.next; 
     } 

     Node newNode = new Node(element); 
     newNode.next = currentNode.next; 
     currentNode.next = newNode; 
    } 
} 
+0

異常在線程 「主」 顯示java.lang.NullPointerException \t在Comp10152_linkedlist.SimpleLinkedList.add(SimpleLinkedList.java:108) \t在Comp10152_linkedlist.Comp10152_Lab4.main(Comp10152_Lab4 .java:30) – 2014-11-05 05:33:07

+0

和currentNode ????? – 2014-11-05 05:33:35

+0

和「currentNode.next.element」在你使用哪個元素? – 2014-11-05 05:35:40

相關問題