add方法应该按排序顺序将元素插入到列表中

时间:2014-11-05 04:59:49

标签: java linked-list

我已完成此代码以将元素添加到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;
                      }          
                  }

My Main类是针对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);
                }
              }     
            }

2 个答案:

答案 0 :(得分:0)

首先在列表外部插入元素,然后通过调用add方法插入到列表中。 如何对列表外的元素进行排序取决于您使用的数据结构,数据类型以及要应用的算法。

答案 1 :(得分: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;
    }
}