How would I get this linked list in alphabetical order?

Ads
Ads

How would I get this linked list in alphabetical order?


rebecca
How would I get this linked list in alphabetical order?
0 Answer(s)      25 days ago
Posted in : Java Beginners
Ads

import java.io.*; import java.util.Scanner;

public class LinkedList2 {

 public LinkedListNode head;
String fname;

public static void main(String[] args) throws FileNotFoundException{
        Scanner scan = new Scanner(new File("Names.txt"));
        LinkedList2 l = new LinkedList2();    
        int i = 1;
        while(scan.hasNext()) {
            String s = scan.nextLine();
            l.insertBack(s);
            i++;
        }
        System.out.print(l.showList());
    }

public void insertBack(String data){


    if(head == null){
        head = new LinkedListNode(data);
    }else{
        LinkedListNode newNode = new LinkedListNode(data);
        LinkedListNode current = head;
        while(current.getNext() != null){
            current = current.getNext();
        }
        current.setNext(newNode);
    }
}

public String showList(){
    int i = 0, j;
    String name = "List nodes:\n";
    LinkedListNode current = head;
    while(current != null){
        i++;
        name += "Node " + i + ": " + current.getData() + "\n";
        current = current.getNext();

    }

    return name;
}

}

this is the code that i have so far. i am able to read in the text file and print it out but i can get in in alphabetical order.

View Answers
Ads