簡體   English   中英

在運行時按字母順序將節點添加到LinkedList

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

我想按字母順序將節點添加到鏈接列表。 輸入是從文件中掃描的。 一切都很好,但是文本文件中的名字顯示在最后。 我不知道發生了什么事。 你們可以幫忙解決此問題嗎?

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

節點類別:

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

名單 :

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM