开发者

How to sort a Linked list

开发者 https://www.devze.com 2023-04-11 11:41 出处:网络
I created a linked list using nodes that prints out a list(string) of names. I\'m trying to write a sort method that prints out the names in alphabetical order. If this was integers it would be easy b

I created a linked list using nodes that prints out a list(string) of names. I'm trying to write a sort method that prints out the names in alphabetical order. If this was integers it would be easy but I have no idea how to go about doing this. Any help or tips are appreciated. -I cannot use the builtin collections.sort(). I need to make my own method.

SortMethod: (this is what I used when I was printing a list of numbers out but now its strings so I know I can't use the > operator. I just don't know how to replace it.

     public void Sort( BigNode front)
    {
            BigNode first,second,temp;
            first= front;
               while(first!=null) {

                    second = first.next;

          while(second!=null) {

    if(first.dataitems < second.dataitems)
    {
    temp = new BigNode();
    temp.dataitems =first.dataitems;
    first.dataitems = second.dataitems;
    second.dataitems = temp.dataitems;

    }
    second=second.next;
    }

Heres the rest of program(I dont think its needed but just in case)

          import java.util.*;

public class BigNode {

   public String dataitems; 
    public BigNode next; 
    BigNode front ;
    BigNode current;

    String a = "1", b = "2", c = "3", d = "4";
    String[] listlength;

    public void initList(){
        front = null;
    }

    public BigNode makeNode(String number){
        BigNode newNode;
        newNode = new BigNode();
        newNode.dataitems = number;
        newNode.next = null;
        return newNode;
    }

    public boolean isListEmpty(BigNode front){
        boolean balance;
        if (front == null){
            balance = true;
        }
        else {
            balance = false;
        }
        return balance;

    }

    public BigNode findTail(BigNode front) {
        BigNode current;
        current = front;
        while(current.next != null){
            //System.out.print(current.dataitems);
            current = current.next;

        } //System.out.println(current.dataitems);
        return current;
    }

    public void addNode(BigNode front ,String name){
        BigNode tail;
        if(isListEmpty(front)){
            this.front = makeNode(name);
        } 
        else {
            tail = findTail(front);
            tail.next = makeNode(name);
        }
    }

    public void addAfter(BigNode n, String dataItem2) { // n might need to be front
        BigNode temp, newNode;
        temp = n.next;
        newNode = makeNode(dataItem2);
        n.next = newNode;
        newNode.next = temp;


    }

    public void findNode(BigNode front, String value) {

        boolean found , searching;
        BigNode curr;
        curr = front;
        found = false;
        searching = true;
            while ((!found) && (searching)) {
                if (curr == null) {
                    searching = false;
                }
                else if (curr.dataitems == value) { // Not sure if dataitems should be there (.data in notes)
                    found = true;

                }
                else {
                    curr = curr.next;

                }
            }   
            System.out.println(curr.dataitems);

    }


    public void deleteNode(BigNode front, String value) {
        BigNode curr, previous = null; boolean found; 

            if (!isListEmpty(front)){
                curr = front;
                found = false;

                while ((curr.next != null) && (!found)) {
                    if(curr.dataitems.equals(value)) {
                        found = true;
                    } 
                    else {
                        previous = curr;
                        curr = curr.next;
                    }
                }
                if (!found) {
                    if(curr.dataitems.equals(value)) {
                        found = true;
                    }
                }
                if (found) {
                    if (curr.dataitems.equals(front.dataitems)){ // front.dataitems may be wrong .dataitems 
                        front = curr.next;
                    } else { 

                        previous.nex开发者_JAVA技巧t = curr.next;
                    }
                } else {
                    System.out.println("Node not found!");
                    //curr.next = null; // Not sure If this is needed
                }
        } 
            showList(front);
    }




    public void printNodes(String[] len){
        listlength = len;

        int j;
        for (j = 0; j < len.length; j++){

            addNode(front, len[j]);
        }  showList(front);
    }

          public void showList(BigNode front){

        current = front;
        while ( current.next != null){
            System.out.print(current.dataitems + ", ");
            current = current.next;
        }
        System.out.println(current.dataitems);
        MenuOptions();
    }

    public void MenuOptions() {
        Scanner in = new Scanner(System.in);
        System.out.println("Choose an Option Below:");
        System.out.println(a + "(Display Length of List)" + ", " + b + "(Delete a person)" + ", " + c + ("(Exit)"));
        String pick = in.nextLine();

        if (pick.equals(b)) {
            System.out.println("Who would you like to delete?");

             String delete = in.nextLine();
             deleteNode(front, delete);

        }

         if (pick.equals(a)) {
            System.out.println("Length of List = " + listlength.length);
            MenuOptions();
        } 

         if (pick.equals(c)) {

         }

    }



    public static void main(String[] args) {


        String[] names = {"Billy Joe", "Sally Mae", "Joe Blow", "Tasha Blue", "Malcom Floyd"}; // Trying to print theses names..Possibly in alphabetical order

        BigNode x = new BigNode(); 

        x.printNodes(names);

    } 

}


first.dataitems.compareTo(second.dataitems)<0

or if you have a comparator

comp.compare(first.dataitems, second.dataitems)<0
0

精彩评论

暂无评论...
验证码 换一张
取 消