Error with code? import java.util.*; public class HelpLists { public void revers
ID: 3819128 • Letter: E
Question
Error with code?
import java.util.*;
public class HelpLists
{
public void reverse(RecLinkedList strings)
{
RecLinkedList.ListIterator iterator =strings.createListIterator();
Stack<String> myStk=new Stack<String>();
while(iterator.hasNext())
{
String tpp=iterator.next();
myStk.push(tpp);
}
System.out.println("Elements in the RecLinkedList are:");
while(!myStk.isEmpty())
{
System.out.println(myStk.pop());
}
System.out.println();
}
public void FIFO(RecLinkedList strings)
{
RecLinkedList.ListIterator iterator=strings.createListIterator();
Queue<String> myQue=new LinkedList<String>();
while(iterator.hasNext())
{
String tpp=iterator.next();
myQue.add(tpp);
}
System.out.println("Elements in the RecLinkedList are:");
Iterator myItt=myQue.iterator();
while(myItt.hasNext())
{
System.out.println((String)myItt.next());
}
}
}
import java.util.*;
public class RecLinkedList
{
private static class RecLinkNode
{
private String myRecNodeItem;
private RecLinkNode myRecNodeNext;
private RecLinkNode(String myValue)
{
myRecNodeItem = myValue;
myRecNodeNext = null;
}
private RecLinkNode(String myValue, RecLinkNode myNextRef)
{
myRecNodeItem = myValue;
myRecNodeNext = myNextRef;
}
}
protected RecLinkNode myRecListHead;
public RecLinkedList()
{
myRecListHead = null;
}
private int size(RecLinkNode myCurrRecList) {
if (myCurrRecList == null) {
return 0;
}
return 1 + size (myCurrRecList.myRecNodeNext);
}
public int size() {
return size(myRecListHead);
}
private RecLinkNode insertAtRecList(RecLinkNode myRecNode, String myValue) {
if (myRecNode == null) {
return new RecLinkNode(myValue);
}
myRecNode.myRecNodeNext = insertAtRecList(myRecNode.myRecNodeNext, myValue);
return myRecNode;
}
public void add(String myValue) {
myRecListHead = insertAtRecList(myRecListHead, myValue);
}
private RecLinkNode insertRecListByPos(RecLinkNode myRecNode, int skipp, String myValue) {
if (skipp == 0) {
return new RecLinkNode(myValue, myRecNode);
}
if (myRecNode == null) {
return null;
}
myRecNode.myRecNodeNext = insertRecListByPos(myRecNode.myRecNodeNext, skipp - 1, myValue);
return myRecNode;
}
public void add(int insertPosVal, String myValue)
{
myRecListHead = insertRecListByPos(myRecListHead, insertPosVal, myValue);
}
private RecLinkNode remove(RecLinkNode myRecNode, String myValue)
{
if (myRecNode == null)
{
System.out.println("not found");
return myRecNode;
}
if (myRecNode.myRecNodeItem.equals(myValue))
{
System.out.println("Name " + myValue + " was found in the list then it was removed");
return myRecNode.myRecNodeNext;
}
myRecNode.myRecNodeNext = remove(myRecNode.myRecNodeNext, myValue);
return myRecNode;
}
public void remove(String myValue)
{
myRecListHead = remove(myRecListHead, myValue);
}
public void replace(String myValue,String myNewValue) {
replace(myRecListHead, myValue,myNewValue);
}
private void replace(RecLinkNode myRecNode, String myValue,String myNewValue) {
if (myRecNode == null) {
return ;
}
if (myRecNode.myRecNodeItem.equals(myValue)) {
myRecNode.myRecNodeItem=myNewValue;
}
replace(myRecNode.myRecNodeNext, myValue,myNewValue);
}
public String getFirst()
{
RecLinkNode tpp= myRecListHead;
return getFirst(tpp);
}
private String getFirst(RecLinkNode tpp)
{
if(tpp==null)
return null;
else
return (String)tpp.myRecNodeItem;
}
public String getLast()
{
RecLinkNode tpp= myRecListHead;
return getLast(tpp);
}
private String getLast(RecLinkNode tpp)
{
if(tpp==null)
return null;
else if(tpp.myRecNodeNext==null)
return (String)tpp.myRecNodeItem;
else
return getLast(tpp.myRecNodeNext);
}
private String toString(RecLinkNode myRecNode) {
if (myRecNode == null)
{
return "";
}
if (myRecNode.myRecNodeNext == null)
{
return myRecNode.myRecNodeItem.toString();
}
return myRecNode.myRecNodeItem.toString() + " ==> " + toString(myRecNode.myRecNodeNext);
}
public String toString() {
return toString(myRecListHead);
}
public ListIterator createListIterator()
{
return new ListIterator();
}
public class ListIterator implements Iterator
{
private RecLinkNode myCurr;
private RecLinkNode myPrev;
private int myIndx=0;
public ListIterator()
{
myCurr=myRecListHead;
myPrev=null;
}
public void reset()
{
myCurr=myRecListHead;
myPrev=null;
}
public boolean hasNext()
{
return myCurr!=null;
}
public String next()
{
if(!hasNext())
throw new NoSuchElementException();
String tpp=myCurr.myRecNodeItem;
myPrev=myCurr;
myCurr=myCurr.myRecNodeNext;
myIndx++;
return tpp;
}
public void remove()
{
if(!hasNext())
throw new NoSuchElementException();
if(myCurr==myRecListHead)
{
myCurr=myCurr.myRecNodeNext;
myRecListHead=myCurr;
}
else
{
myPrev.myRecNodeNext=myCurr.myRecNodeNext;
myCurr=myPrev.myRecNodeNext;
}
}
public void add(String value)
{
if(myRecListHead==null)
{
myRecListHead=new RecLinkNode(value,null);
myCurr=myRecListHead;
myPrev=null;
}
else if(myCurr==myRecListHead)
{
myPrev=new RecLinkNode(value,myRecListHead);
myRecListHead=myPrev;
}
else
{
myPrev.myRecNodeNext=new RecLinkNode(value,myPrev);
myPrev=myPrev.myRecNodeNext;
}
}
public boolean hasPrevious()
{
if(myPrev!=null)
return true;
return false;
}
public String previous()
{
if(hasPrevious())
return myPrev.myRecNodeItem;
return null;
}
public void set(String newValue)
{
if(!hasNext())
throw new NoSuchElementException();
else
myCurr.myRecNodeItem=newValue;
}
}
}
import java.io.*;
import java.lang.*;
import java.util.*;
public class StudentLinkedList
{
public static void main(String args[]) throws FileNotFoundException
{
/**
* LinkedList to be used in storing and handling students’ data
*/
RecLinkedList students = new RecLinkedList();
/**
* inputFile The name of the scanner to open studentData.text and handle
* it*/
Scanner inputFile = new Scanner(new File("studentData.txt"));
/** Creating a ListIterator object */
RecLinkedList.ListIterator iterator=students.createListIterator();
/** helper, creates an object of class HelpLists */
HelpLists helper = new HelpLists();
int location;
/** loop to add every element in the file to LinkedList students */
while (inputFile.hasNextLine()) {
students.add(inputFile.next());
}
/** closes inputFile */
inputFile.close();
/** prints the first student in the list */
System.out.println("Last element in the list is: " + students.getLast());
/** prints the last student in the file */
System.out.println("First element in the list is: " + students.getFirst());
/** asks for user input for a name to remove from the list */
System.out.println("Enter a student name to be removed from the list");
/** Creates a new scanner to read user input */
Scanner userInput = new Scanner(System.in);
/** nameToRemove, user input name to remove from the list */
String nameToRemove = userInput.next();
/**
* loop to find if the student name is there to be removed if found
* removes the name and prints the name that was removed else prints an
* error message
*/
students.remove(nameToRemove);
System.out.println(" The size of the list after the removal is: " + students.size());
/** closes scanner used to open user input */
userInput.close();
/** prints contents of LinkedList students using an enhanced for loop */
System.out.println(" Displaying list contents using enhanced for loop");
System.out.println(students.toString());
/** Creates a iterator Reference to linkedList students */
iterator = students.createListIterator();
/** loop to iterate 3 times though the list */
int itrNum = 0;
while (itrNum < 3) {
iterator.next();
itrNum++;
}
/** prints the element healed by the iterator */
System.out.println("The current student after iterating three times is: " + iterator.previous());
/**
* adds Edward to the list and displays the content of the list after
* adding the name
*/
iterator.next();
iterator.add("Edward");
System.out.println("Displaying list Contents after adding 'Edward' :");
System.out.println(students.toString());
/**
* sets the current iterator to George and displays the content of the
* list
*/
iterator.previous();
iterator.set("George");
System.out.println("Displaying List Contents after setting the current element to George: ");
System.out.println(students.toString());
/** Displays list after previous then remove */
iterator.previous();
iterator.remove();
System.out.println("Displaying list after previous then remove: ");
System.out.println(students.toString());
}
}
studentData.txt
AbdulAllah
Bonnie
Daniel
Gary
Jared
Lydia
Matt
Mike
Robert
Sean
Explanation / Answer
import java.util.*;
public class HelpLists
{
public void reverse(RecLinkedList strings)
{
RecLinkedList.ListIterator iterator =strings.createListIterator();
Stack<String> myStk=new Stack<String>();
while(iterator.hasNext())
{
String tpp=iterator.next();
myStk.push(tpp);
}
System.out.println("Elements in the RecLinkedList are:");
while(!myStk.isEmpty())
{
System.out.println(myStk.pop());
}
System.out.println();
}
public void FIFO(RecLinkedList strings)
{
RecLinkedList.ListIterator iterator=strings.createListIterator();
Queue<String> myQue=new LinkedList<String>();
while(iterator.hasNext())
{
String tpp=iterator.next();
myQue.add(tpp);
}
System.out.println("Elements in the RecLinkedList are:");
Iterator myItt=myQue.iterator();
while(myItt.hasNext())
{
System.out.println((String)myItt.next());
}
}
}
import java.util.*;
public class RecLinkedList
{
private static class RecLinkNode
{
private String myRecNodeItem;
private RecLinkNode myRecNodeNext;
private RecLinkNode(String myValue)
{
myRecNodeItem = myValue;
myRecNodeNext = null;
}
private RecLinkNode(String myValue, RecLinkNode myNextRef)
{
myRecNodeItem = myValue;
myRecNodeNext = myNextRef;
}
}
protected RecLinkNode myRecListHead;
public RecLinkedList()
{
myRecListHead = null;
}
private int size(RecLinkNode myCurrRecList) {
if (myCurrRecList == null) {
return 0;
}
return 1 + size (myCurrRecList.myRecNodeNext);
}
public int size() {
return size(myRecListHead);
}
private RecLinkNode insertAtRecList(RecLinkNode myRecNode, String myValue) {
if (myRecNode == null) {
return new RecLinkNode(myValue);
}
myRecNode.myRecNodeNext = insertAtRecList(myRecNode.myRecNodeNext, myValue);
return myRecNode;
}
public void add(String myValue) {
myRecListHead = insertAtRecList(myRecListHead, myValue);
}
private RecLinkNode insertRecListByPos(RecLinkNode myRecNode, int skipp, String myValue) {
if (skipp == 0) {
return new RecLinkNode(myValue, myRecNode);
}
if (myRecNode == null) {
return null;
}
myRecNode.myRecNodeNext = insertRecListByPos(myRecNode.myRecNodeNext, skipp - 1, myValue);
return myRecNode;
}
public void add(int insertPosVal, String myValue)
{
myRecListHead = insertRecListByPos(myRecListHead, insertPosVal, myValue);
}
private RecLinkNode remove(RecLinkNode myRecNode, String myValue)
{
if (myRecNode == null)
{
System.out.println("not found");
return myRecNode;
}
if (myRecNode.myRecNodeItem.equals(myValue))
{
System.out.println("Name " + myValue + " was found in the list then it was removed");
return myRecNode.myRecNodeNext;
}
myRecNode.myRecNodeNext = remove(myRecNode.myRecNodeNext, myValue);
return myRecNode;
}
public void remove(String myValue)
{
myRecListHead = remove(myRecListHead, myValue);
}
public void replace(String myValue,String myNewValue) {
replace(myRecListHead, myValue,myNewValue);
}
private void replace(RecLinkNode myRecNode, String myValue,String myNewValue) {
if (myRecNode == null) {
return ;
}
if (myRecNode.myRecNodeItem.equals(myValue)) {
myRecNode.myRecNodeItem=myNewValue;
}
replace(myRecNode.myRecNodeNext, myValue,myNewValue);
}
public String getFirst()
{
RecLinkNode tpp= myRecListHead;
return getFirst(tpp);
}
private String getFirst(RecLinkNode tpp)
{
if(tpp==null)
return null;
else
return (String)tpp.myRecNodeItem;
}
public String getLast()
{
RecLinkNode tpp= myRecListHead;
return getLast(tpp);
}
private String getLast(RecLinkNode tpp)
{
if(tpp==null)
return null;
else if(tpp.myRecNodeNext==null)
return (String)tpp.myRecNodeItem;
else
return getLast(tpp.myRecNodeNext);
}
private String toString(RecLinkNode myRecNode) {
if (myRecNode == null)
{
return "";
}
if (myRecNode.myRecNodeNext == null)
{
return myRecNode.myRecNodeItem.toString();
}
return myRecNode.myRecNodeItem.toString() + " ==> " + toString(myRecNode.myRecNodeNext);
}
public String toString() {
return toString(myRecListHead);
}
public ListIterator createListIterator()
{
return new ListIterator();
}
public class ListIterator implements Iterator
{
private RecLinkNode myCurr;
private RecLinkNode myPrev;
private int myIndx=0;
public ListIterator()
{
myCurr=myRecListHead;
myPrev=null;
}
public void reset()
{
myCurr=myRecListHead;
myPrev=null;
}
public boolean hasNext()
{
return myCurr!=null;
}
public String next()
{
if(!hasNext())
throw new NoSuchElementException();
String tpp=myCurr.myRecNodeItem;
myPrev=myCurr;
myCurr=myCurr.myRecNodeNext;
myIndx++;
return tpp;
}
public void remove()
{
if(!hasNext())
throw new NoSuchElementException();
if(myCurr==myRecListHead)
{
myCurr=myCurr.myRecNodeNext;
myRecListHead=myCurr;
}
else
{
myPrev.myRecNodeNext=myCurr.myRecNodeNext;
myCurr=myPrev.myRecNodeNext;
}
}
public void add(String value)
{
if(myRecListHead==null)
{
myRecListHead=new RecLinkNode(value,null);
myCurr=myRecListHead;
myPrev=null;
}
else if(myCurr==myRecListHead)
{
myPrev=new RecLinkNode(value,myRecListHead);
myRecListHead=myPrev;
}
else
{
myPrev.myRecNodeNext=new RecLinkNode(value,myPrev);
myPrev=myPrev.myRecNodeNext;
}
}
public boolean hasPrevious()
{
if(myPrev!=null)
return true;
return false;
}
public String previous()
{
if(hasPrevious())
return myPrev.myRecNodeItem;
return null;
}
public void set(String newValue)
{
if(!hasNext())
throw new NoSuchElementException();
else
myCurr.myRecNodeItem=newValue;
}
}
}
import java.io.*;
import java.lang.*;
import java.util.*;
public class StudentLinkedList
{
public static void main(String args[]) throws FileNotFoundException
{
/**
* LinkedList to be used in storing and handling students’ data
*/
RecLinkedList students = new RecLinkedList();
/**
* inputFile The name of the scanner to open studentData.text and handle
* it*/
Scanner inputFile = new Scanner(new File("studentData.txt"));
/** Creating a ListIterator object */
RecLinkedList.ListIterator iterator=students.createListIterator();
/** helper, creates an object of class HelpLists */
HelpLists helper = new HelpLists();
int location;
/** loop to add every element in the file to LinkedList students */
while (inputFile.hasNextLine()) {
students.add(inputFile.next());
}
/** closes inputFile */
inputFile.close();
/** prints the first student in the list */
System.out.println("Last element in the list is: " + students.getLast());
/** prints the last student in the file */
System.out.println("First element in the list is: " + students.getFirst());
/** asks for user input for a name to remove from the list */
System.out.println("Enter a student name to be removed from the list");
/** Creates a new scanner to read user input */
Scanner userInput = new Scanner(System.in);
/** nameToRemove, user input name to remove from the list */
String nameToRemove = userInput.next();
/**
* loop to find if the student name is there to be removed if found
* removes the name and prints the name that was removed else prints an
* error message
*/
students.remove(nameToRemove);
System.out.println(" The size of the list after the removal is: " + students.size());
/** closes scanner used to open user input */
userInput.close();
/** prints contents of LinkedList students using an enhanced for loop */
System.out.println(" Displaying list contents using enhanced for loop");
System.out.println(students.toString());
/** Creates a iterator Reference to linkedList students */
iterator = students.createListIterator();
/** loop to iterate 3 times though the list */
int itrNum = 0;
while (itrNum < 3) {
iterator.next();
itrNum++;
}
/** prints the element healed by the iterator */
System.out.println("The current student after iterating three times is: " + iterator.previous());
/**
* adds Edward to the list and displays the content of the list after
* adding the name
*/
iterator.next();
iterator.add("Edward");
System.out.println("Displaying list Contents after adding 'Edward' :");
System.out.println(students.toString());
/**
* sets the current iterator to George and displays the content of the
* list
*/
iterator.previous();
iterator.set("George");
System.out.println("Displaying List Contents after setting the current element to George: ");
System.out.println(students.toString());
/** Displays list after previous then remove */
iterator.previous();
iterator.remove();
System.out.println("Displaying list after previous then remove: ");
System.out.println(students.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.