2012-12-13 45 views
1

实现一个链接列表,最多可存储10个名称,按First Out排序。然后实施两种方法,其中一种按姓氏按字母顺序排序。这是我遇到麻烦的地方。这是我试过的:使用一种方法在Java中对单一链接列表进行排序

  1. 递归。该方法调用两个节点,比较它们,如果需要则交换,然后调用它自己。不适用于奇数个名称,并且往往是完整的错误。

  2. 收集,但我不知道它足够有效地使用它。

  3. 排序算法(例如冒泡排序):我可以通过列表,但很难让节点交换。

我的问题是:什么是最简单的方法来做到这一点?

public class List 
{ 
    public class Link 
    { 
     public String firstName; 
     public String middleName; 
     public String lastName; 
     public Link next = null; 

    Link(String f, String m, String l) 
    { 
     firstName = f; 
     middleName = m; 
     lastName = l; 
    } 
} 

private Link first_; 
private Link last_; 

List() 
{ 
    first_ = null; 
    last_ = null; 
} 

public boolean isEmpty() 
{ 
    return first_ == null; 
} 

public void insertFront(String f, String m, String l) 
{ 
    Link name = new Link(f, m, l); 
    if (first_ == null) 
    { 
     first_ = name; 
     last_ = name; 
    } 
    else 
    { 
     last_.next = name; 
     last_ = last_.next; 
    } 
} 

public String removeFront() 
{ 
    String f = first_.firstName; 
    String m = first_.middleName; 
    String l = first_.lastName; 
    first_ = first_.next; 
    return f + " " + m + " " + l; 
} 

public String findMiddle(String f, String l) 
{ 
    Link current = first_; 
    while (current != null && current.firstName.compareTo(f) != 0 && current.lastName.compareTo(l) != 0) 
    { 
     current = current.next; 
    } 
    if (current == null) 
    { 
     return "Not in list"; 
    } 
    return "That person's middle name is " + current.middleName; 
} 
} 

public class NamesOfFriends 
{ 
    public static void main(String[] args) 
    { 
     List listOfnames = new List(); 
     Scanner in = new Scanner(System.in); 

    for(int i = 0; i < 3; i++) 
    { 
     if(i == 0) 
     { 
      System.out.println("Please enter the first, middle and last name?"); 
      listOfnames.insertFront(in.next(), in.next(),in.next()); 
     } 
     else 
     { 
      System.out.println("Please enter the next first, middle and last name"); 
      listOfnames.insertFront(in.next(), in.next(),in.next()); 
     } 
    } 

    System.out.println("To find the middle name, please enter the first and last name of the person."); 
    System.out.println(listOfnames.findMiddle(in.next(),in.next())); 
    } 
} 

编辑

它的工作一点之后,我想通了,如何去选它。为此,我试图实现一个删除方法,该方法可以删除列表中任何位置的节点。在编译时,它在运行程序时不起作用。

public Link remove(String lastName) 
{ 
    Link current_ = first_; 
    Link prior_ = null; 
    Link temp_ = null; 
    while (current_ != null && current_.lastName.compareTo(lastName) != 0) 
    { 
     prior_ = current_; 
     current_ = current_.next; 
    } 
    if (current_ != null) 
    { 
     if (current_ == last_) 
     { 
      temp_ = last_; 
      last_ = prior_; 
     } 
     else if (prior_ == null) 
     { 
      temp_ = first_; 
      first_ = first_.next; 
     } 
    } 
    else 
    { 
     temp_ = current_; 
     prior_.next = current_.next; 
    } 
    return temp_; 
} 
+0

请不要使用作业标签。 – Woot4Moo

回答

1

2:收藏是最简单的,但它似乎在你的功课不被允许

3:冒泡是容易的,但最糟糕的已知排序算法中,但是对于你的功课,它可能是确定

1:这是一样的冒泡排序,但是是首选将不递归

通过你的元素一次又一次地做在冒泡你循环,直到没有互换再次需要,那么你已经准备好了。

0

收集是完成此操作的最简单方法。

  • 实施Comparable
  • 覆盖hashcodeequals
  • Collection.sort()
0

您已经有了链表实现的,这是好的。

您是否考虑实施MergeSort作为排序算法?作为征服算法的分水岭,你总是只用两个元素组成一个列表。

合并部分将变得更加棘手,但也很容易。基本上,您只需创建一个新列表,并通过比较两个合并集合的第一个值,开始填充您获得的元素。

因此,举例来说,如果你有两套合并:

[A]->[C]->[D] 
[B]->[E]->[F] 

的mergin过程会:

[A] 

[C]->[D] 
[B]->[E]->[F] 

[A]->[B] 

[C]->[D] 
[E]->[F] 

[A]->[B]->[C] 

[D] 
[E]->[F] 

[A]->[B]->[C]->[D] 

[E]->[F] 

[A]->[B]->[C]->[D]->[E] 

[F] 

[A]->[B]->[C]->[D]->[E]->[F] 
相关问题