简体   繁体   English

在运行时按字母顺序将节点添加到LinkedList

[英]Adding nodes to the LinkedList in alphabetical order on the run

I want to add nodes to the linked list in alphabetical order. 我想按字母顺序将节点添加到链接列表。 The input is scanned from the file. 输入是从文件中扫描的。 Everything is good, but the first name in the text file is shown at the last. 一切都很好,但是文本文件中的名字显示在最后。 I cannot figure out what's happening. 我不知道发生了什么事。 Can you guys help fixing this issue? 你们可以帮忙解决此问题吗?

import java.io.File;
import java.util.Scanner;

public class ScaryLinkedList {

    public static void main(String args[]){

        node myNode = new node();
        Scanner scan = new Scanner(System.in);
        node current = myNode.front;
        System.out.println("Enter the name of the txt file that you want to access.");
        String input = scan.next();

        File file = new File(input);
        int i =0;

        node spot, temp;
        String name;
        try
        {
            Scanner myScan = new Scanner(file);

            while(myScan.hasNextLine())
            {
                name = myScan.nextLine();
                if(i == 0)
                {
                    myNode.front = myNode.makeNode(name);
                }
                else
                {
                    spot = myNode.findSpot(name);
                    if (spot == myNode.front) 
                    {
                        temp = myNode.front;
                        myNode.front = myNode.makeNode(name);
                        myNode.front.next = temp;
                    }
                    else
                    {
                        myNode.InsertAfter(spot, name);
                    }
                }

                i++;
            } 
        }
        catch (Exception e) 
        {
            System.out.println("Error!! "+e);
        }

        System.out.println("What do you want to do, today?\n1. View the current list (Press 1)\n2. View the sorted list (Press 2)\n3. Find out the length of the current list (Press 3)\n4. Delete an entry from the list (Press 4)\n5. Request the length of a section of the list (Press 5)\n6. Print out sections of names. (Press 6)");
        int selection = scan.nextInt();

        if(selection == 1)
        {
            showList(myNode);
        }
        else if(selection == 2) 
        {
            sort(myNode);
        }
        else if(selection == 3) 
        {
            System.out.println("The length of the current list is: "+myNode.length());
        }
        else if(selection == 4) 
        {
            deleteAfter(myNode);
        }
        else if(selection == 5) 
        {
            selectedListLength(myNode);
        }
        else if(selection == 6) 
        {
            selectedList(myNode);
        }
    }

    private static void selectedListLength(node myNode) {
        Scanner ascan = new Scanner(System.in);
        System.out.println("Enter the alphabet whose list length you want. ");
        String letter = ascan.next();
        char l = letter.charAt(0);
        node curr = myNode.front;
        int x = 0;
        while(curr.next != null){
            if(curr.data.charAt(0) == l) {
                x++;
            }
            curr= curr.next;
        }
        System.out.println("The total number of names that start with "+letter+ " is: "+ x);
    }

    private static void selectedList(node myNode) {
        Scanner ascan = new Scanner(System.in);
        System.out.println("Enter the alphabet whose list you want. ");
        String letter = ascan.next();
        char l = letter.charAt(0);
        node curr = myNode.front;
        System.out.println("This is the total list of people whose name start from "+letter);
        while(curr.next != null){
            if(curr.data.charAt(0) == l) {
                System.out.println(curr.data);  
            }
            curr= curr.next;
        }
    }

    public static void deleteAfter(node myNode) {
        System.out.println("This is the current list: ");
        sort(myNode);
        Scanner iscan = new Scanner(System.in);
        node curr = myNode.front;       
        System.out.print("Enter the name you want to delete: \n");
        String name = iscan.next();
        int x= 0;
        System.out.println(name+ " has been deleted from the system.");
        while(curr.next != null){
            if(name.toLowerCase().equals(curr.next.data))
            {
                myNode.deleteAfter(curr);
                System.out.println("The list length after the removal of "+name+" is: "+myNode.length());
                System.out.println("\nUpdated list after the removal of "+ name+".");

                sort(myNode);
            }
            else{
                curr = curr.next;
            }
        }
    }

    public static void sort(node myNode) {
        String [] arr = new String[myNode.length()];

        node current = myNode.front;

        for (int i = 0; i < arr.length; i++) {
            arr[i] = current.data;
            current = current.next;
        }

        int x = 0; int j = 0;

        boolean swapped = true;

        String tmp;

        while (swapped) 
        {
            swapped = false;
            j++;

            for (int i = 0; i < arr.length - j; i++) {
                if (arr[i].compareTo( arr[i + 1])>0) {
                    tmp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = tmp;
                    swapped = true;
                }
            }
        }
        System.out.println("Sorted List: ");
        for (int i = 0; i < arr.length; i++) 
        {
            System.out.println(arr[i]);
        }
    }

    public static void addNodeAtEndOfList(node myNode, String nextLine) {
        node tail;
        tail = myNode.findTail(myNode.front);
        tail.next = myNode.makeNode(nextLine);
    }

    public static void showList(node myNode) {
        System.out.println("Unsorted List: ");
        node curr = myNode.front;           
        while (curr.next != null) {
            System.out.println(curr.data);
            curr = curr.next;
        }
        System.out.println(curr.data);
    }
}

Node Class : 节点类别:

public class node {
    public String data;
    public node next;
    public node front;

    public void init()
    {
        front = null;
    }

    public node makeNode(String data)
    {
        node newNode;
        newNode = new node();
        newNode.data = data;
        newNode.next = null;

        return newNode;
    }

    public node findTail(node front)
    {
        node current;

        current = front;
        while(current.next != null) {
            current = current.next;
        }
        return current;
    }

    public void addAtEndOfList(node spot, String data)
    {
        node tail;

        if (front == null) {
            front = makeNode(data);
        }
        else {
            tail = findTail(spot);
            tail.next = makeNode(data);
        }
    }

    public void InsertAfter(node spot, String data)
    {
        node newNode;
        newNode = makeNode(data);
        newNode.next = spot.next;
        spot.next = newNode;
    }

    public void deleteAfter(node spot)
    {
        node nextNode;
        nextNode = spot.next;
        spot.next = nextNode.next;
    }

    public node findSpot(String s)
    {
        node curr, prev;
        curr = front;
        prev = curr;
        while((curr.next != null) && (curr.data.compareTo(s) < 0)) 
        {
            prev = curr;
            curr = curr.next;
        }

        return prev;
    }

    public int length() {
       int size = 0;
       node n;
       for( n = front; n.next != null; n = n.next)
       {
           size++;     
       }
       size++;
       return size;
    }

    public void delete(node curr)
    {
        curr = curr.next;
    }
}

Name list : 名单 :

joe
bob
harry
mary
brian
tom
jerry
bullwinkle
pam
ellis
dale
bill
barrack
george
gertrude
zack
zeus
apollo
gemini
greg
larry
meriam
webster
thomas
stewart
dianna
theresa
billyjoe
carl
karl
charles
karla
donna
tena
kerry
howard
johnson
ulyssess
paul
peter
issaac
marvin
dudz
chuck
ellie
anny
judy
matt
ross
dan
robert
kim
eric
junkun
ghassan
cris
raymond
avery
roy
halley
mitzee
ziggy
rocky
twirly
max
huey
dewy
hongkongfooey
clarence
lala
sammy
fred
francis

Use The Benefits of Collection Classes in Java. 在Java中使用集合类的好处。 Use This Code,You'll get desired results: 使用此代码,您将获得理想的结果:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;

public class SortedLinkList {

 public static void main(String args[]) throws FileNotFoundException{

        LinkedList<String> list=new LinkedList<String>();
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter the name of the txt file that you want to access.");
        String input = scan.next();
        scan.close();
        File file = new File(input);

        Scanner myScan = new Scanner(file);

        while(myScan.hasNextLine())
        {

            list.add(myScan.next());

        }    

        myScan.close();

        Collections.sort(list);
        System.out.println(list);
 }        
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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