【问题标题】:Sorting list in LinkedList [closed]LinkedList中的排序列表[关闭]
【发布时间】:2014-11-06 07:22:47
【问题描述】:

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

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

【问题讨论】:

  • 为了清楚起见,您是在问如何将节点添加到列表的正确排序顺序中?还有你的班级名称是什么?
  • 如果您有独特的元素,请使用SortedSet。它将根据equals()保持正确的顺序。
  • 您目前观察到什么问题?
  • 我只想按排序顺序插入..这段代码@JasonC没有问题,想要实现删除方法...

标签: java sorting linked-list


【解决方案1】:

这是一个排序的链表。插入时,我们可以让它插入到正确的位置。

1) 如果列表为空,则将第一个和最后一个作为新元素。否则

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

  }

【讨论】:

  • 非常感谢@Sangeeth.. 这对我来说是非常有用的代码。
【解决方案2】:

这应该可行:

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

【讨论】:

  • 如果您可以解释 OP 代码中存在哪些问题、您是如何得出这个解决方案的以及为什么这样做,这将是一个有用的答案。纯代码答案并不能帮助任何人学习任何东西。
猜你喜欢
  • 1970-01-01
  • 2020-04-27
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 2013-09-04
  • 1970-01-01
相关资源
最近更新 更多