Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在运行时按字母顺序向LinkedList添加节点_Java_Linked List - Fatal编程技术网

Java 在运行时按字母顺序向LinkedList添加节点

Java 在运行时按字母顺序向LinkedList添加节点,java,linked-list,Java,Linked List,我想按字母顺序将节点添加到链接列表中。从文件中扫描输入。一切都很好,但文本文件中的第一个名称显示在最后。我不知道发生了什么事。你们能帮我解决这个问题吗 import java.io.File; import java.util.Scanner; public class ScaryLinkedList { public static void main(String args[]){ node myNode = new node(); Scanner

我想按字母顺序将节点添加到链接列表中。从文件中扫描输入。一切都很好,但文本文件中的第一个名称显示在最后。我不知道发生了什么事。你们能帮我解决这个问题吗

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

使用Java中集合类的优点。 使用此代码,您将获得所需的结果:

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);
 }        
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Collections;
导入java.util.LinkedList;
导入java.util.Scanner;
公共类分类链接列表{
公共静态void main(字符串args[])引发FileNotFoundException{
LinkedList=新建LinkedList();
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入要访问的txt文件的名称”);
字符串输入=scan.next();
scan.close();
文件=新文件(输入);
Scanner myScan=新扫描仪(文件);
while(myScan.hasNextLine())
{
list.add(myScan.next());
}    
myScan.close();
集合。排序(列表);
系统输出打印项次(列表);
}        
}
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
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);
 }        
}