2012-04-17 7 views
0

私の目標は、1行に1つのエントリのテキストファイルをソートできることです。私はInsertionクラスを作成する必要があります。どのように私は単一のリンクのリスト(自分の実装ではなく、Javaの)と私はパラメータとして渡す必要がありますか?これまでのところ私のコードです。 P.Sリンクされたリストの独自の実装を使用している理由は、そのことがどのように機能し、リンクされたリストを使用して行われたさまざまなアクションがどのように機能するのかを知りたいからです。挿入ソートの実装方法は?

ご協力いただければ幸いです。

メイン:

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.DataInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.Scanner; 


public class Sort 
{ 
    public static void main(String[] args) throws Exception 
    { 
     Scanner kb = new Scanner (System.in) ; 
     File outputFile ; 
     EntriesList list = new EntriesList() ; 
     String line ; 
     String entry ; 
     String command ; 
     String textContent ; 


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

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

     }while (!outputFile.exists()) ; 

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

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

     //Welcome message + entry counter 
     System.out.println("Welcome. \nYou about to sort " + list.count("Entries.txt") + " entries. \nPlease use the following commands [Add -add new entry, View -view entries before sorting, -i -Insertion Sort, -s -Selection Sort, -m -Merge Sort, Exit]: "); 
     System. out.println ("") ;   
     command = kb.next() ; 

     // User Input 
     do 
     { 
      if (command.equalsIgnoreCase("Add")) 
      { 
       System.out.println("Enter String value:") ; 
       entry = kb.next() ; 
       textContent = entry ; 
       System.out.println("Entry added successfully") ; 

       try 
       { 
        //the "true" argument sets the FileWriter to append mode so that is does not overwrite the first time 
        BufferedWriter out = new BufferedWriter(new FileWriter("Entries.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() ; 

       list.insert(entry) ; 
      } 

      else if (command.equalsIgnoreCase("View")) 
      { 
       if (!list.isEmpty()) 
       { 
        list.printList(); 
        System.out.println ("Enter command:") ; 
        command = kb.next() ; 
       } 
       else 
       { 
        System.out.println("File is empty. Please enter records first."); 
        System.out.println ("Enter ADD command:") ; 
        command = kb.next(); 
       } 
      } 
      else if (command.equalsIgnoreCase("Exit")) 
      { 
       System.exit(0) ; 
      } 
      else 
      { 
       System.out.println("Unknown command. Please use ADD, VIEW or EXIT") ; 
       command = kb.next() ; 
      } 
     }while (!command.equalsIgnoreCase("Exit")) ; 
    } 
} 

List実装:

import java.io.BufferedInputStream; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 


public class EntriesList 
{ 
    private Entries head; 
    private int listCount ; 

    //LinkList constructor 
    public EntriesList() 
    { 
      head = new Entries (null) ; 
      listCount = 0 ;    
    } 

    //Returns true if list is empty 
    public boolean isEmpty() 
    { 
      return head == null; 
    } 

    //Inserts a new Entry at the end of the list 
    public void insert(String entryIn) 
    { 
      Entries temp = new Entries (entryIn) ; 
      Entries current = head ; 

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

      // Last Entries's next reference is set to the noew node 
      current.setNext(temp) ; 
      listCount++ ; 
    } 

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

     //Prints list data 
    public void printList() 
    { 
      Entries currentEntry = head; 
      while(currentEntry != null) 
      { 
       currentEntry.printLink(); 
       currentEntry = currentEntry.nextEntry; 
      } 
      System.out.println(""); 
    } 

// Count the lines in the text file 
    public int count(String filename) throws IOException 
    { 
     InputStream is = new BufferedInputStream(new FileInputStream(filename)); 
     try 
     { 
      byte[] c = new byte[1024] ; 
      int count = 0 ; 
      int readChars = 0 ; 
      while ((readChars = is.read(c)) != -1) 
      { 
       for (int i = 0 ; i < readChars ; ++i) 
       { 
        if (c[i] == '\n') 
         ++count ; 
       } 
      } 
      return count ; 
     } finally 
     { 
      is.close() ; 
     } 
    } 
} 

エントリ(リンク)作成者:

public class Entries 
{ 
    public String entry ; 
    public Entries nextEntry; 

    // Empty Link Constructor 
    public Entries() 
    { 

    } 

    //Link constructor 
    public Entries(String entryIn) 
    { 
     entry = entryIn ; 
     nextEntry = null ; 
    } 

    public String getEntry() 
    { 
     return entry ; 
    } 

    public void setEntry (String entryIn) 
    { 
     entry = entryIn ; 
    } 

    public Entries getNext() 
    { 
     return nextEntry ; 
    } 

    public void setNext (Entries nextEntryIn) 
    { 
     nextEntry = nextEntryIn ; 
    } 

    //Print Link data 
    public void printLink() 
    { 
      System.out.println("") ; 
      System.out.print(getEntry() +"\n"); 
      System.out.println("") ; 
    } 
} 

そして全能の挿入ソートクラス:

public class Insertion 
{ 
    public String Sort (EntriesList list) 
    { 

    } 
} 
+1

、変更すべきヌルまでにリンクされたリスト内の次の要素を取得していきますlinkをリストの先頭に等しいと仮定すると「Javaでの挿入ソートの実装方法」のタイトル:)次のリンクを試してくださいhttp://www.roseindia.net/java/beginners/arrayexamples/InsertionSort.shtml –

+0

これをどのように修正して文字列をソートする必要がありますか? – serge

答えて

1

この投稿は2つの異なる質問をしているようです。だから私は別々に答えました。

EDIT:リンクされたリストクラスに問題が発生しました。まずこれを修正してから、あなたの質問に対する私の答えを見てください。

リンクリストの次のリンクへの参照を保存していないため、実装が正しくありません。 Entriesは、次の要素への参照を格納する必要があります。あなたはそのページ上の図を見れば、私は

​​

各リンク(またはあなたがそれを呼んでいるようエントリは)その隣人へのリンクを持っている... this article.

を読んでお勧めします。

実装挿入ソート

私はあなたがそれの内部エントリのアルファベット順リストをリンクソートしたいと仮定します。これが当てはまる場合は、単純に文字の比較をテキストブック/ Web上のアルファベット順の比較で表示される挿入ソートでスワップします。

アルファベット順の比較方法については、Comparing strings by their alphabetical orderをご覧ください。

私は昨夜の整数のためにScalaの挿入ソートクラスを作成しました。hereあなたはそれが有用かもしれません。 LinkedListの

オーバー

反復処理あなたのリンクリストを渡すためにあなたは、単にheadリンクを渡します。リストのnext要素を呼び出すことによって、リンクされたリストを繰り返し処理できます。

たとえば..

while(link < null){ 
    link.next 
} 

上記のループは(リストの末尾を表すべきである)

+0

よろしくお願い致します。それを整理して投稿するにはしばらくお待ちください – serge

+0

はちょうど最初の部分(参照)を終了しました。それは大丈夫と思われますか? – serge

+0

@ voth1234はい、そうですね。これをテストする最良の方法は、リンクを挿入し、そのうちの6個または7個を挿入して、私のコードで提供しているwhileループを使ってそれらをトラバースする 'insert()'関数を作ることです。これにより、リンクされたリストが作成されたことがわかります。 – Aidanc

関連する問題