/import Keyboard; import java.io.*; import java.util.*; public class ListExample
ID: 3683603 • Letter: #
Question
/import Keyboard;
import java.io.*;
import java.util.*;
public class ListExample
{
public static void main (String [] args)
{
//local variables
ListNodes nameList = new ListNodes(); //List of Names
int value = 0; //While Loop sentinal
String word; //one line read from file
StringTokenizer inLine; //tokenized string
String lastName; //Names extracted from file
String firstName;
String inPutFile = "test.txt"; //file name
/************ Open File and place data in List ************************/
try
{
//open the input stream
FileReader fRead = new FileReader(inPutFile);
BufferedReader bRead = new BufferedReader (fRead);
//get data
//read in the first line of the file
word = bRead.readLine();
//while not yet at the end of the file
while(word != null)
{
//parse the new line 1 word at a time
inLine = new StringTokenizer(word);
//get first name
firstName = inLine.nextToken();
//get last name
lastName = inLine.nextToken();
//debug as names are extracted from external file
//System.out.print(firstName + " " + lastName);
//add to list
//create new node and add data
nameList.insert(lastName, firstName);
//get the next word in the current line
word = bRead.readLine();
}//end while word
//close the input file
bRead.close ();
/* Display Menu Options */
while(value != 4)
{
if(value != 4)
{
//Display a menu
System.out.print("***************************************** ");
System.out.print("*1. Insert a name * ");
System.out.print("*2. Display List * ");
System.out.print("*3. Delete Name * ");
System.out.print("*4. Exit * ");
System.out.println("***************************************** ");
System.out.print("Enter Selection here: ");
value = Keyboard.readInt();
switch(value)
{
case 1: //Insert a single name
{
System.out.print(" Enter a First Name: ");
firstName = Keyboard.readString();
System.out.print(" Enter " + firstName + "'s Last Name: ");
lastName = Keyboard.readString();
//create new node and add data
nameList.insert(lastName, firstName);
}
break;
case 2: //Display list of names
{
//print out the list
System.out.println(" ");
nameList.printString();
System.out.println(" ");
}
break;
case 3: //
{
System.out.print(" Enter a Last Name: ");
lastName = Keyboard.readString();
//create new node and add data
nameList.deleteNode(lastName);
}
break;
case 4:
{
System.out.println("Exiting... ");
}
break;
default:
{
System.out.print("Enter a value 1 - 4");
}
break;
}
}//end if value != 4
}//end while
}//end try
catch(IOException exception)
{
System.out.println(exception.getMessage());
}//end catch
}//end main method
}//end ListClass
------------------------------------------
public class ListNodes
{
//Instance data
//Nodes I use to traverse the list
Node head; //points to first node
Node first; //temp position for first node
Node current; //node currently being processed
Node prev; //node previously processed
//Fields of the node
String firstName; //Student's first name
String lastName; //Student's last name
Node next; //next node in list
public ListNodes()
{
head = null; //empty list
}
public void printString( ) //print out the list
{
first = head; //store head of list
System.out.println(" ");
for (current = first; current != null; current = current.getNext())
System.out.println(current.getFirst() + " " + current.getLast());
System.out.println(" ");
}
public void deleteNode (String lastN)
{
first = head;
current = first;
if (first != null) //list has nodes
{
//check first node
if (lastN.compareToIgnoreCase(first.getLast()) == 0)
{
first = first.getNext();
}
else //check rest of list
{
while(current.getNext() != null && lastN.compareToIgnoreCase(current.getLast()) != 0)
{
prev = current;
current = current.getNext();
}
//name not found
if (current.getNext() == null && lastN.compareToIgnoreCase(current.getLast()) != 0)
{
System.out.println("Name not found");
}
//name found at end of list
else if ((current.getNext() == null && lastN.compareToIgnoreCase(current.getLast()) == 0))
{
prev.setNext (null);
}
//name found within list
else //if ( (lastN.compareToIgnoreCase(current.getLast())) = 0 )
{
prev.setNext (current.getNext());
current.setNext (null);
}
}//end else
}//end if first
head = first; //update head to new first of list
}//end delete
public void insert (String firstN, String lastN)
{
first = head; //store head of list
Node newData = new Node (lastN, firstN); //place data in new node
if (first == null) //if list is empty
{
first = newData; //first points to new node
}
else //list is not empty
{
//does new data come first
if (first.getLast().compareToIgnoreCase(newData.getLast()) > 0)
{
//insert new data at front of list and return as head
newData.setNext(first); //new node points to first node
first = newData; //head now points to new node
}// end current.getLast
else //insert in list
{
prev = first;
current = first.getNext();
while (current != null &&
current.getLast().compareToIgnoreCase(newData.getLast()) < 0) //Stop at position
{
prev = current;
current = current.getNext();
}//end while current
//if new data goes at end of list
if (current == null)
prev.setNext(newData);
else //new data goes in the middle of the list
{
newData.setNext(current);
prev.setNext(newData);
}//end else middle of list
}//end else insert in list
}//end else not empty list
head = first; //update head of list
}//end insert
}//end class
------------------------------------
public class Node
{
/********* Instance Data **************/
private String item1; //Data field 1
private String item2; //Data field 2
private Node next; //Link to next node
/********** Contructors **************/
public Node()
//New empty node - no data, points to NULL
{
next = null;
}
public Node(String newItem1, String newItem2)
// New node with data that points to NULL
{
item1 = newItem1;
item2 = newItem2;
next = null;
} // end constructor
public Node(String newItem1, String newItem2, Node nextNode)
// New node with data that links to Next Node
{
item1 = newItem1;
item2 = newItem2;
next = nextNode;
} // end constructor
/************ Methods ***************/
//Method to set the value of the instance variable from Class ListInfo.
//Postcondition: instance variable = x;
public void setString(String newItem1, String newItem2)
{
item1 = newItem1;
item2 = newItem2;
}
//Method to return the value of the instance variable from Class ListInfo.
//Postcondition: The value of item is returned.
public String getFirst()
{
return item1;
}
//Method to return the value of the instance variable from Class ListInfo.
//Postcondition: The value of item is returned.
public String getLast()
{
return item2;
}
public void setNext(Node changeNext)
{
next = changeNext;
}
public Node getNext()
{
return next;
}
} // end class Node
Explanation / Answer
Open the text file and read each line as a String and place that String object into a LinkedList. Print all of the lines in the LinkedList in reverse order.
Helping Code:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.