好的,我的目标是能够对每行单个条目的文本文件进行排序。我坚持到必须创建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")) ;
}
}
列表实施:
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)
{
}
}
答案 0 :(得分:1)
这篇文章似乎是在问两个问题。所以我已经单独回答了。
编辑:刚刚注意到您的linkedlist类存在问题。您必须首先解决此问题,然后查看我对您问题的回答。
您的实施不正确,因为您没有存储对链接列表中下一个链接的引用。 Entries
应存储对下一个元素的引用。我建议阅读this article.
如果您查看该页面上的图表...
每个链接(或您正在调用它的条目)都有一个指向其邻居的链接。
实施插入排序
我假设您希望按照其中的条目按字母顺序对链接列表进行排序。如果是这种情况,您只需在插入排序中换出整数比较,您将在教科书/网络上看到,以进行字母比较。
看看Comparing strings by their alphabetical order这是一个类似的问题,告诉你如何进行字母比较。
我昨晚在Scala中为整数写了一个插入排序类here你可能觉得它很有用。
迭代LinkedList
要传递您的链接列表,只需传递head
链接即可。然后,您可以通过调用列表的next
元素来遍历链表。
例如..
while(link < null){
link.next
}
假设link
等于列表的头部,上面的循环将继续获取链表中的下一个元素,直到null(这应该代表列表的结尾)