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.
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 myList; myList = new RefUnsortedList(); 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 { private T info; private LLNode next; public void setLink(LLNode node) { next = node; } public LLNode getLink() { return next; } public LLNode(T inf) { info = inf; } public T getInfo() { return info; } } class RefUnsortedList { protected int numElements; protected LLNode currentPos; protected boolean found; protected LLNode location; protected LLNode previous; protected LLNode list; public RefUnsortedList() { numElements = 0; list = null; currentPos = null; } public void add(T element)
{ LLNode newNode = new LLNode(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 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; } }