LinkedList中的排序列表

时间:2014-11-06 07:22:48

标签: java sorting linked-list

我已编写此代码以插入元素并将其删除并形成链接列表。我想将元素插入到排序列表中的列表中。如何改进我的“添加”方法来做到这一点?

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

      }

      /**
      * The toString method computes the string representation of the list.
      * 
      * @return The string form of the list.
      */
      public String toString()
      {
        StringBuilder strBuilder = new StringBuilder();

        // Use p to walk down the linked list
        Node p = first;
        while (p != null) {
          strBuilder.append("[" + p.value + "]");
          p = p.next;
        }
        return strBuilder.toString();
      }


           The remove method removes an element.
           @param element The element to remove.
           @return true if the remove succeeded, 
             false otherwise.


        public boolean remove(String element)
        {
           if (isEmpty()) 
               return false;      

           if (element.equals(first.value))
           {
              // Removal of first item in the list
              first = first.next;
              if (first == null)
                  last = null;       
              return true;
           }

          // Find the predecessor of the element to remove
          Node pred = first;
          while (pred.next != null && 
                 !pred.next.value.equals(element))
          {
              pred = pred.next;
          }

          // pred.next == null OR pred.next.value is element
          if (pred.next == null)
              return false;

          // pred.next.value  is element
          pred.next = pred.next.next;    

          // Check if pred is now last
          if (pred.next == null)
              last = pred;

          return true;       
        }

2 个答案:

答案 0 :(得分:0)

这是一个已排序的链接列表。插入时,我们可以将其插入正确的位置。

1)如果list为空,则将first和last作为新元素。其他

2)如果新元素小于第一个节点,则将新元素作为第一个。其他

3)遍历列表,找到前一节点较小且下一节点较大或为空的位置。将新节点插入此位置

因此,使用此方法,我们可以始终对列表进行排序。

 public void add(String element)
  {
      Node newNode = new Node(element);
      if (isEmpty()) 
      {
          first = newNode;
          last = newNode;
      }
      else if (element.compareTo(first.value) < 0)//if element is lesser than all elements in list
      {
         newNode.next = first;
         first= newNode;
      }
      else
      {
        Node after = first.next;
        Node before = first;
        while (after != null)// finding exact position to insert
        {
            if (element.compareTo(after.value) < 0)
                break;
            before = after;
            after = after.next;
        }
        // insert between before & after
        newNode.next = before.next;
        before.next = newNode;
      }

  }

答案 1 :(得分:-1)

这应该有效:

public void addElementInSortOrder(final List<String> list, final String element) {
    if (list.isEmpty()) {
        list.add(element);
    } else {
        // look for the correct index
        int index = list.size();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).compareTo(element) >= 0) {
                index = i;
                break;
            }
        }
        list.add(index, element);
    }
}