Alredy given generic Node class and all of the file checking is alredy done. Nod
ID: 3751817 • Letter: A
Question
Alredy given generic Node class and all of the file checking is alredy done.
Node class is a generic LInked List class with basic methods. getvalue setvalue getnext setnext getprev setprev listtostring listtostringbackwords
The text file being brought in is something like this: 1 0 + 2 4 - 7 6 + 3 2 / * * + print
Reply with your email and i can send you more info if u need. Thanks.
Need to fill in the Process method
public static Node fileToNodeQueue(String filename) throws IOException
{
Scanner sc = new Scanner(new File(filename));
// create two node variables.
// one for storing the head location.
Node<String> head = null;
// one to maintain the previous node information.
Node<String> prev = null;
// run the code inside till sc runs out of string tokens.
while(sc.hasNext())
{
// for every string token from scanner.
// create a node.
Node<String> node = new Node<String>();
// set the value obtained from the scanner.
node.setValue(sc.next());
// set the next as null for now.
node.setNext(null);
// set the prev from the stored prev variable.
// in case of first node, null will be stored.
node.setPrev(prev);
// if prev node is not null, (if this is not the first node)
// then modify the previous node's next to the current node.
if(prev != null) prev.setNext(node);
// set the current node as previous node as we move on to the next token.
prev = node;
if(head == null)
{
// if this is the 1st node, then store the head's reference.
head = node;
}
}
// return the head of the list.
return head;
}
public Node<String> process(Node<String> input, int numSymbols)
{
//Given an input queue of symbols process the number of symbols
//specified (numSymbols) and update the progStack and symbols
//variables appropriately to reflect the state of the "computer"
//(see below the "do not edit" line for these variables).
//Return the remaining queue items.
//For example, if input is the head of a linked list 3 -> 2 -> +
//and numSymbols=2, you would push 3 and push 2, then return the linked
//list with just the + node remaining.
return null;
}
Explanation / Answer
Answer:
{
Scanner Sc = new Scanner (new File(filename));
// create two node variables.
// one for storing the head location.
Node<String> head = null;
// one to maintain the previous node information.
Node<String> prev = null;
// run the code inside till sc runs out of string tokens.
while(sc.hasNext())
{
// for every string token from scanner.
// create a node.
Node<String> node = new Node<String>();
// set the value obtained from the scanner.
node.setValue(sc.next());
// set the next as null for now.
node.setNext(null);
// set the prev from the stored prev variable.
// in case of first node, null will be stored.
node.setPrev(prev);
// if prev node is not null, (if this is not the first node)
// then modify the previous node's next to the current node.
if (prev!= null) prev.setNext(node);
// set the current node as previous node as we move on to the next token.
prev = node;
if(head == null)
{
// if this is the 1st node, then store the head's reference.
head = node;
}
}
// return the head of the list.
return head;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.