0

昨日コードのバージョンを投稿しましたが、何らかの改善か何かを処理できると思いました。物事は再び立ち往生しています。ご覧のとおり、リンクされたリストを並べ替えてテキスト ファイルに書き戻そうとしています。最初の質問は、挿入クラスで何をどのように渡すかということです。2番目の質問は、挿入ソートをALPHABETICALLYに実装する方法ですか? 整数と配列を使用した挿入の例を数多く見てきましたが、まだ問題があるようです。これが私のこれまでの進歩です。

編集1:それを忘れてください。

EDIT 2: Comparable を実装し、私のメソッドでアルファベット順の並べ替え (CompareToIgnoreCase) を使用する方法は?

主要:

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

public class Sort 
{
    public static void main(String[] args) throws Exception 
    {
        File outputFile ;
        Scanner kb = new Scanner (System.in) ; 
        LinkedList list = new LinkedList() ;
        String command ;
        Insertion insertion = new Insertion() ;

        // Create the new text file. If exists, it will continue to the next commands
        do
        {
            outputFile = new File("db.txt") ;

                if(!outputFile.exists())
                {
                    outputFile.createNewFile ();                    
                    System.out.println("The file was created as db.txt");
                    System.out.println("");
                }

        }while (!outputFile.exists()) ;

        // Define which file to stream in from          
        FileInputStream fileIn = new FileInputStream("db.txt") ;
        DataInputStream input = new DataInputStream (fileIn) ;
        BufferedReader br = new BufferedReader (new InputStreamReader (input)) ;
        String line ;

        try
        {               
            // Read each line of the file               
            while ((line = br.readLine()) != null)
            {
                list.add(line) ;
            }       
            input.close() ;
        }catch (Exception e){
            System.err.println("Error. Could not read the file") ;
        }

        //System.out.println (list.toString()) ;

        //Welcome message
        System.out.println("Welcome. \nPlease use the following commands [-i for Insertion Sort, -s for Selection Sort, -m for Merge sort, Exit to terminate]: " );
        System. out.println ("") ;          
        command = kb.next() ;

        do
        {
            if (command.equalsIgnoreCase("-i"))
            {
                insertion.Sort(list, list.size()) ;

                //try
                //{
                    //the "true" argument sets the FileWriter to append mode so that is does not overwrite the first line
                //  BufferedWriter out = new BufferedWriter(new FileWriter("db.txt", true));
                    //out.write(textContent) ;
                    //out.newLine() ;
                    //out.close() ;
                //}catch(IOException e)
                //{
                //  System.out.println("Could not write to file") ;
                //  System.exit(0) ;
                //}

                //System.out.println ("Enter command:") ;
                //command = kb.next() ;

            }

            else if (command.equalsIgnoreCase("-s"))
            {

            }
            else if (command.equalsIgnoreCase("-m"))
            {

            }
            else if (command.equalsIgnoreCase("Exit"))
            {
                System.exit(0) ;
            }
            else 
            {
                System.out.println("Unknown command. Please use -i, -s, -m or EXIT") ;
                command = kb.next() ;
            }
        }while (!command.equalsIgnoreCase("Exit")) ;

    }
}

リンクリスト:

    import java.lang.* ;

public class LinkedList
{
    //reference to the head node
    public Node head ;
    public int listCount ;

    //LinkedList Constructor
    public LinkedList ()
    {
        //empty list
        head = new Node (null) ;
        listCount = 0 ;
    }

    //add element to the end of the list
    public void add(String data)
    {
        Node temp = new Node (data) ;
        Node current = head ;

        //go to the end of the list
        while (current.getNext() != null)
        {
            current = current.getNext() ;
        }

        // last node\s next reference is set to the last node
        current.setNext(temp) ;
        //increment the number of elements
        listCount++ ; 
    }

    //return the size of the list
    public int size()
    {
        return listCount ;
    }

    public String toString()
    {
        Node current = head.getNext() ;
        String output = " " ;
        while (current != null)
        {
            output += "[" + current.getData().toString() + "]" ;
            current = current.getNext() ;
        }
        return output ;
    }


}

挿入:

public class Insertion 
{
    public static void Sort (LinkedList listIn, int size)
    {
        int temp, i, j ;
        for (j=1;j<size;j++)
        {
            while(listIn.head)
        }
    }
}

ノード クラス:

    public class Node 
    {
        //reference to the next node or null if not any
        Node next ;
        //the entries
        String data ;

        //Node Constructor
        public Node (String dataIn)
        {
            next = null ;
            data = dataIn ;
        }

        //Node constructor in case of the need to point to a certain node
        public Node (String dataIn, Node nextIn)
        {
            next = nextIn;
            data = dataIn ;
        }

        public String getData ()
        {
            return data ;
        }

        public void setData (String dataIn)
        {
            data = dataIn ;
        }

        public Node getNext ()
        {
            return next ;
        }

        public void setNext (Node nextIn)
        {
            next = nextIn ;
        }
    }
}
4

1 に答える 1

0

挿入クラスで何をどのように渡すのですか?

メソッドのことですか?(少なくとも) LinkedList(head だけでなく) 全体を渡す必要がありNodeます。

2番目の質問は、挿入ソートをALPHABETICALLYに実装する方法ですか?

タイプが を実装する要素のリストを取得するComparableか、別のComparatorパラメーターを取得して、それを使用して要素を比較します。StringComparator次に、for s のリストStringIntComparatorfor s などを渡すことができIntegerます。必要なのは、必要な Comparator を実装することだけです。これは通常は簡単です。したがって、任意の型に対してソート アルゴリズムを再利用できます。

最初のアプローチはより単純でString、箱から出してすぐに s に対して機能します (String既に実装されているようにComparable)。ただし、オブジェクト型に限定されていることに注意してください。 などのプリミティブ型のint場合は、2 番目の方法が必要です。繰り返しになりますが、プリミティブ型の要素を格納するジェネリック コレクションを作成することはできません。

2 種類のジェネリックsortメソッドの例を に示しCollectionsます。

public static <T extends Comparable<? super T>> void sort(List<T> list);
public static <T> void sort(List<T> list, Comparator<? super T> c)

ただし、非ジェネリックを保持したい場合LinkedListは、(ほとんどの)ジェネリック毛羽立ちを削除できます。

public static void sort(LinkedList list);
public static void sort(LinkedList list, Comparator<? super String> c)
于 2012-04-18T07:10:57.297 に答える