简体   繁体   中英

Using a method to sort a Singly Linked List in Java

Implementing a Linked List, store up to 10 names, ordered in first in First Out. Then implement two methods, one of which to sort it alphabetically by last names. This is where I am having some trouble. Here is what I tried:

  1. Recursion. The method calls two nodes, compare them, swap if needed and then calls itself. Doesn't work with odd number of names and tends to be full bugs.

  2. Collection but I don't know enough about it to use it effectively.

  3. Sorting algorithms (ex. bubble sort): I can go though the list but have a hard time getting the nodes to swap.

My question is: What is the easiest way to do this?

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

Edit

After working on it a bit, I figured out how to go about sorting it. For that purpose, I am trying to implement a remove method that can remove a node anywhere on the list. While it does compile, it doesn't do anything when I run the program.

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

2: Collections is the easiest, but it seems to be not allowed in your homework

3: BubbleSort is easy but the worst known sorting algo, however for your homework it probably is ok

1: This is the same as bubble sort, but is prefered to be done without recursion

In BubbleSort you loop through your elements again and again till no swaps are neeeded anymore, then you are ready.

Collection is the easiest way to accomplish this.

  • Implement Comparable
  • Override hashcode and equals
  • Collection.sort()

You already has the linked list implemented, that is good.

Have you considered implementing MergeSort as the sorting algorithm? Being the divide&conquer algorithm, you will always end up with only two elements to form a list with.

The merge part is going to be trickier, but also easy. Basically you just create a new list and start filling it up with elements you get by comparing the first values of the two merging sets.

So for instance if you have two sets to merge:

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

the mergin process will go:

[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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM