PLEASE HELP IN JAVA: This is an exercise in using the java LinkedList class. Usi
ID: 3749708 • Letter: P
Question
PLEASE HELP IN JAVA:
This is an exercise in using the java LinkedList class.
Using input file words.txt , create a LinkedList BUT you must NOT add a word that is already in the list and you MUST add them in the order received to the end of the list.
For example, if your input was cat, cat, dog, mouse, dog, horse
your list would now contain cat, dog, mouse, horse. And the head of the list would be cat, and the tail would be horse. Note: it is NOT sorted.
This list represents a code, and the code for one word is the next word in the list. So the code for cat is dog and the code for mouse is horse, and the code for horse is cat(go round to the head of the list).
Now ask your user for a phrase (input from the keyboard) and output the coded phrase. In the above example if the input was "mouse cat" the output would be "horse dog".
How do you know if you have the correct output? I suggest you make your own (small) input file so you can test it.
Rubric:
* use of try/catch
*use of while loop to collect inout
* no duplicates put into list
* prompt user for phrase
* code the phrase
* correct output
Explanation / Answer
package DS;
import java.util.Scanner;
// Defines a class MyNode
class MyNode
{
// Instance variable to store data
protected String value;
// MyNode object to point to next node
protected MyNode nextLink;
// Default constructor to initialize data member
public MyNode()
{
nextLink = null;
value = "";
}// End of default constructor
// Parameterized constructor to assign parameter values to instance variable
public MyNode(String data, MyNode node)
{
value = data;
nextLink = node;
}// End of parameterized constructor
// Method to set the link to the node object passed as parameter
public void setNextLink(MyNode node)
{
nextLink = node;
}// End of method
// Method to assign data passed as parameter to instance variable
public void setData(String data)
{
value = data;
}// End of method
// Method to return link reference
public MyNode getNextLink()
{
return nextLink;
}// End of method
// Method to return data stored in node
public String getData()
{
return value;
}// End of method
}// End of MyNode class
// Defines a class LinkedListNode to generate linked link list
class LinkedListNode
{
// Object to point to beginning of the node
protected MyNode start;
// Object to point to end of the node
protected MyNode end;
// Default constructor to initialize data member
public LinkedListNode()
{
start = null;
end = null;
}// End of default constructor
// Method to return true if start is null otherwise return false
public boolean isEmpty()
{
return start == null;
}// End of method
// Method to insert a node
public void insert(String word)
{
// Create a new node dynamically using parameterized constructor
MyNode newNode = null;
// Checks if start is null then first node in the linked list
if(start == null)
{
// Creates the new node using parameterized constructor
newNode = new MyNode(word, null);
// Assigns new node to start
start = newNode;
// End is pointing to start though there is only one node
end = start;
}// End of if condition
// Otherwise more than one node available
else
{
// Sets the found status to -1 for not found
int found = -1;
// Declares a temporary node pointing to start
MyNode temp = start;
// Loops till end of the linked list
while (temp != null)
{
// Checks if current node data is equals to parameter word
if(temp.getData().compareTo(word) == 0)
{
// Then sets the found status to 0 for duplicate
found = 0;
// Displays the duplicate word
System.out.println("Duplicate word " + word + " not allowed.");
break;
}// End of if condition
// Move to next node
temp = temp.getNextLink();
}// End of while loop
// Checks if found status is -1 then word is not duplicate add the word
if(found == -1)
{
// Creates the new node using parameterized constructor
newNode = new MyNode(word, null);
// Current end node points to new node
end.setNextLink(newNode);
// end is pointing to new node
end = newNode;
}
}// End of else
}// End of method
// Method to display the linked list node values
public void display()
{
// Checks if start is null then no node in the linked list
if (start == null)
{
System.out.print("empty ");
return;
}// End of if condition
// Otherwise nodes are available
// Checks start next node link is equals to null then there is only one node
if (start.getNextLink() == null)
{
// Display the node data
System.out.println(start.getData() );
// Return from the function because there is no more node available
return;
}// End of if condition
// Otherwise more than one node available
// Declares a temporary node pointing to start
MyNode temp = start;
// Display the node data by calling the method getData()
System.out.print(start.getData()+ "->");
// Move to next node by calling getNextLink() method
temp = start.getNextLink();
// Loops till end of the linked list
while (temp.getNextLink() != null)
{
// Displays the current node value
System.out.print(temp.getData()+ "->");
// Move to next node
temp = temp.getNextLink();
}// End of while loop
// Displays last node value
System.out.print(temp.getData()+ " ");
}// End of method
}// End of LinkedListNode class
// Driver class LinkedListUniqueWords definition
public class LinkedListUniqueWords
{
// main method definition
public static void main(String[] args)
{
// Scanner class object created
Scanner sc = new Scanner(System.in);
// Creates an object of the class LinkedListNode using default constructor
LinkedListNode ll = new LinkedListNode();
// To store phrase
String phrase;
// Accepts a phrase from the user
System.out.print(" Enter a phrase: ");
phrase = sc.nextLine();
// Split the phrase to words based on the space
String words[] = phrase.split(" ");
// Loops till length of the words
for(int x = 0; x < words.length; x++)
// Calls the method to insert the words
ll.insert(words[x]);
// Calls the method to display the words
ll.display();
sc.close();
}// End of main function
}// End of class
Sample Output:
Enter a phrase: cat cat dog mouse dog horse
Duplicate word cat not allowed.
Duplicate word dog not allowed.
cat->dog->mouse->horse
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.