Write a method IN JAVA, createArrayOfLL that takes a file of words, one in each
ID: 3601339 • Letter: W
Question
Write a method IN JAVA, createArrayOfLL that takes a file of words, one in each line and creates an array of linked list nodes as follows.
(a) Create an array of 26 linked lists
(b) Open the file and Insert each word into the array based on its first letter, eg: any word that begins with ‘a’ goes to A[0] list etc..
(c) Return the reference to the array of linked lists You must consider ALL cases . You must write reasonable comments to describe your algorithm.
You may assume public fields for each node, data and next. There WILL NOT be any partial credit for this problem.
The question will be graded ALL or NOTHING. [Use extra paper, if necessary to write the answer].
Node class is defined as follows.
Public Node[] createArrayOfLL(String inputfile) {
Explanation / Answer
Please find my implementation.
public Node[] createArrayOfLL(String inputfile) throws IOException {
// opening file
FileReader fr = new FileReader(inputfile);
BufferedReader br = new BufferedReader(fr);
String line;
// creating array of linked list
Node[] array = new Node[26];
// reading line by line
while((line = br.readLine()) != null) {
// getting first character
char c = Character.toLowerCase(line.trim().charAt(0));
// creating a node
Node newNode = new Node(line);
// getting head node for index of current character
Node head = array[c - 97];
newNode.next = head;
array[c-97] = newNode;
}
return array;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.