I need help with this java program, it is already been done need few changes to
ID: 3856565 • Letter: I
Question
I need help with this java program, it is already been done need few changes to it. i am providing the program needed for this.
Question needs to be solved:
implement a function to check if a linked list is a palindrome.
--------------------------------------------------------------------------------------------------------------
Driver.java
//import ch06.lists.*;
//import support.*;
public class Driver
{
public static void main(String[] args)
{ //page 469 #51
System.out.println("page 469 #51 testing removeAll() ");
RefUnsortedList<String> myList;
myList = new RefUnsortedList<String>();
myList.add("3");
myList.add("4");
myList.add("3");
myList.add("5");
myList.add("3");
myList.add("6");
myList.add("3");
myList.add("3");
myList.add("7");
System.out.println("myList: "+myList);
myList.removeAll1("3");
System.out.println("myList: "+myList);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
LLNode.java
class LLNode<T>
{
private T info;
private LLNode<T> next;
public void setLink(LLNode<T> node)
{
next = node;
}
public LLNode<T> getLink()
{
return next;
}
public LLNode(T inf)
{
info = inf;
}
public T getInfo()
{
return info;
}
}
class RefUnsortedList<T>
{
protected int numElements;
protected LLNode<T> currentPos;
protected boolean found;
protected LLNode<T> location;
protected LLNode<T> previous;
protected LLNode<T> list;
public RefUnsortedList()
{
numElements = 0;
list = null;
currentPos = null;
}
public void add(T element)
{
LLNode<T> newNode = new LLNode<T>(element);
newNode.setLink(list);
list = newNode;
numElements++;
}
protected void find(T target)
{
location = list;
found = false;
while (location != null)
{
if (location.getInfo().equals(target))
{
found = true;
return;
}
else
{
previous = location;
location = location.getLink();
}
}
}
public int removeAll1(T target)
{
location = list;
found = false;
int cnt=0;
while (location.getLink() != null)
{
if (location.getLink().getInfo().equals(target))
{
location.setLink(location.getLink().getLink());
cnt++;
}
else
{
location = location.getLink();
}
}
if(list.getInfo().equals(target))
{
list = list.getLink();
cnt++;
}
return cnt;
}
public int size()
{
return numElements;
}
public boolean contains (T element)
{
find(element);
return found;
}
public boolean remove (T element)
{
find(element);
if (found)
{
if (list == location)
list = list.getLink();
else
previous.setLink(location.getLink());
numElements--;
}
return found;
}
public int removeAll(T ele)
{
int count =0;
while(remove(ele))
count++;
return count;
}
public T get(T element)
{
find(element);
if (found)
return location.getInfo();
else
return null;
}
public String toString()
{
LLNode<T> currNode = list;
String listString =" List:";
while (currNode != null)
{
listString = listString + " " + currNode.getInfo() + " ";
currNode = currNode.getLink();
}
return listString;
}
public void reset()
{
currentPos = list;
}
public T getNext()
{
T next = currentPos.getInfo();
if (currentPos.getLink() == null)
currentPos = list;
else
currentPos = currentPos.getLink();
return next;
}
}
Explanation / Answer
//import ch06.lists.*;
//import support.*;
public class Driver
{
public static void main(String[] args)
{ //page 469 #51
System.out.println("page 469 #51 testing removeAll() ");
RefUnsortedList<String> myList;
myList = new RefUnsortedList<String>();
myList.add("3");
myList.add("4");
myList.add("3");
myList.add("5");
myList.add("3");
myList.add("6");
myList.add("3");
myList.add("3");
myList.add("7");
System.out.println("myList: "+myList);
myList.removeAll1("3");
System.out.println("myList: "+myList);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
LLNode.java
class LLNode<T>
{
private T info;
private LLNode<T> next;
public void setLink(LLNode<T> node)
{
next = node;
}
public LLNode<T> getLink()
{
return next;
}
public LLNode(T inf)
{
info = inf;
}
public T getInfo()
{
return info;
}
}
class RefUnsortedList<T>
{
protected int numElements;
protected LLNode<T> currentPos;
protected boolean found;
protected LLNode<T> location;
protected LLNode<T> previous;
protected LLNode<T> list;
public RefUnsortedList()
{
numElements = 0;
list = null;
currentPos = null;
}
public void add(T element)
{
LLNode<T> newNode = new LLNode<T>(element);
newNode.setLink(list);
list = newNode;
numElements++;
}
protected void find(T target)
{
location = list;
found = false;
while (location != null)
{
if (location.getInfo().equals(target))
{
found = true;
return;
}
else
{
previous = location;
location = location.getLink();
}
}
}
public int removeAll1(T target)
{
location = list;
found = false;
int cnt=0;
while (location.getLink() != null)
{
if (location.getLink().getInfo().equals(target))
{
location.setLink(location.getLink().getLink());
cnt++;
}
else
{
location = location.getLink();
}
}
if(list.getInfo().equals(target))
{
list = list.getLink();
cnt++;
}
return cnt;
}
public int size()
{
return numElements;
}
public boolean contains (T element)
{
find(element);
return found;
}
public boolean remove (T element)
{
find(element);
if (found)
{
if (list == location)
list = list.getLink();
else
previous.setLink(location.getLink());
numElements--;
}
return found;
}
public int removeAll(T ele)
{
int count =0;
while(remove(ele))
count++;
return count;
}
public T get(T element)
{
find(element);
if (found)
return location.getInfo();
else
return null;
}
public String toString()
{
LLNode<T> currNode = list;
String listString =" List:";
while (currNode != null)
{
listString = listString + " " + currNode.getInfo() + " ";
currNode = currNode.getLink();
}
return listString;
}
public void reset()
{
currentPos = list;
}
public T getNext()
{
T next = currentPos.getInfo();
if (currentPos.getLink() == null)
currentPos = list;
else
currentPos = currentPos.getLink();
return next;
}
}