📄 mylist.java
字号:
package myList;
import java.io.*;
public class MyList
{
static LinkedList l = new LinkedList();
static Elem[] myElem = new Elem[20];
static DLinkedListT dlt = new DLinkedListT();
static DElemT[] myDElemT = new DElemT[20];
static DLinkedListA dla = new DLinkedListA();
static DElemA[] myDElemA = new DElemA[20];
static int lengthLinkedList;
public static void main(String[] args)
{
if (args.length < 3)
{
System.out.println("Usage: MyFileIo <inputfilename> <outputfilename> <sorted by Author filename> <sorted by Title filename>");
System.exit(-1);
};
constructElem(args[0]);
constructLinkedList();
writeLinkedListToFile(args[1]);
constructDoublyElemA(l);
constructDoublyElemT(l);
constructDoublyLinkedListA(myDElemA);
constructDoublyLinkedListT(myDElemT);
writeDoublyLinkedListAToFile(args[2]);
writeDoublyLinkedListTToFile(args[3]);
}
public static void constructElem(String infile)
{
try
{
System.out.println("Reading the Author & Title of books from the imputfile and filling the Elements of LinkedList");
BufferedReader in = new BufferedReader(new FileReader(infile));
String inTitle = new String();
String inAuthor = new String();
for(int i = 0; i <20; i++)
{
myElem[i] = new Elem();
if((inAuthor = in.readLine()) == null)
break;
myElem[i].author = inAuthor;
//System.out.println("Author" + myElem[i].author);
inTitle = in.readLine();
myElem[i].title = inTitle;
//System.out.println("Title" + myElem[i].title);
System.out.println("Reading the book " + (i+1) + " from the inputfile to construct elem[" + i + "] successfully");
lengthLinkedList = i;
}
}
catch(IOException e)
{
if (e instanceof FileNotFoundException)
{
System.out.println("Input file not found: " + infile);
}
else System.out.println("read error!");
System.exit(-1);
};
}
public static void constructLinkedList()
{
System.out.println("Constructing the LinkedList with the elements");
l.setHead(myElem[0]);
System.out.println("Constructing the head node successfully");
for(int i = 1; i<=lengthLinkedList; i++)
{
l.insertLast(myElem[i]);
System.out.println("Constructing the No." + (i+1) + " node successfully");
}
}
public static void writeLinkedListToFile(String outfile)
{
try
{
BufferedWriter out = new BufferedWriter(new FileWriter(outfile));
System.out.println("Wrinting the LinkedList of books to output file");
out.write(l.head.author); out.newLine();
out.write(l.head.title); out.newLine();
System.out.println("Writing the head node successfully");
Elem cur = l.head.next;
int i = 1;
while(cur != null)
{
out.write(cur.author); out.newLine();
out.write(cur.title); out.newLine();
System.out.println("Writing the element No." + (i+1) + " node sucessfully");
cur = cur.next;
i++;
}
out.close();
}
catch(IOException e)
{
if (e instanceof FileNotFoundException)
{
System.out.println("Output file for LinkedList not found: " + outfile);
}
else System.out.println("read error!");
System.exit(-1);
};
}
public static void writeDoublyLinkedListAToFile(String dlafile)
{
try
{
BufferedWriter out = new BufferedWriter(new FileWriter(dlafile));
System.out.println("Wrinting the DoublyLinkedListA sorted by Author to file");
out.write(dla.head.ref.author); out.newLine();
out.write(dla.head.ref.title); out.newLine();
System.out.println("Writing the head node successfully");
DElemA cur = dla.head.next;
int i = 1;
while(cur != dla.head)
{
out.write(cur.ref.author); out.newLine();
out.write(cur.ref.title); out.newLine();
System.out.println("Writing the element No." + (i+1) + " node sucessfully");
cur = cur.next;
i++;
}
out.close();
}
catch(IOException e)
{
if (e instanceof FileNotFoundException)
{
System.out.println("Output file for DoublyLinkedListA not found: " + dlafile);
}
else System.out.println("read error!");
System.exit(-1);
};
}
public static void writeDoublyLinkedListTToFile(String dltfile)
{
try
{
BufferedWriter out = new BufferedWriter(new FileWriter(dltfile));
System.out.println("Wrinting the DoublyLinkedListT sorted by Title to file");
out.write(dlt.head.ref.author); out.newLine();
out.write(dlt.head.ref.title); out.newLine();
System.out.println("Writing the head node successfully");
DElemT cur = dlt.head.next;
int i = 1;
while(cur != dlt.head)
{
out.write(cur.ref.author); out.newLine();
out.write(cur.ref.title); out.newLine();
System.out.println("Writing the element No." + (i+1) + " node sucessfully");
cur = cur.next;
i++;
}
out.close();
}
catch(IOException e)
{
if (e instanceof FileNotFoundException)
{
System.out.println("Output file for DoublyLinkedListT not found: " + dltfile);
}
else System.out.println("read error!");
System.exit(-1);
};
}
public static void constructDoublyElemA(LinkedList ll)
{
Elem min = new Elem();
Elem cur = new Elem();
for(int i = 0; i <= lengthLinkedList; i++)
{
min = ll.head;
cur = ll.head;
while(min.setA == true)
{if(min.next != null) min = min.next;}
for(int j = 0; j <= lengthLinkedList; j++)
{
if(min.author.compareTo(cur.author) > 0 && cur.setA == false)
min = cur;
if(cur.next != null)cur = cur.next;
}
myDElemA[i] = new DElemA();
myDElemA[i].author = min.author;
myDElemA[i].ref = min;
min.setA = true;
System.out.println("Constructing the DElementA No." + (i+1) + " node sucessfully " + "Author: " + myDElemA[i].author);
}
}
public static void constructDoublyElemT(LinkedList ll)
{
Elem min = new Elem();
Elem cur = new Elem();
for(int i = 0; i <= lengthLinkedList; i++)
{
min = ll.head;
cur = ll.head;
while(min.setT == true)
{if(min.next != null) min = min.next;}
for(int j = 0; j <= lengthLinkedList; j++)
{
if(min.title.compareTo(cur.title) > 0 && cur.setT == false)
min = cur;
if(cur.next != null)cur = cur.next;
}
myDElemT[i] = new DElemT();
myDElemT[i].title = min.title;
myDElemT[i].ref = min;
min.setT = true;
System.out.println("Constructing the DElementT No." + (i+1) + " node sucessfully " + "Title: " + myDElemT[i].title);
}
}
public static void constructDoublyLinkedListA(DElemA[] dea)
{
dla.setHead(dea[0]);
for(int i = 1; i <= lengthLinkedList; i++)
{
dla.insert(dea[i]);
}
}
public static void constructDoublyLinkedListT(DElemT[] det)
{
dlt.setHead(det[0]);
for(int i = 1; i <= lengthLinkedList; i++)
{
dlt.insert(det[i]);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -