Question
When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for
When compiling the LinkedList and Iterator class, the following error is being produced:
Note: LinkedList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
Any suggestions?
public class LinkedList
public LinkedList() { itsFirstNode = null; itsLastNode = null; size = 0; }
public Iterator
// THIS WILL NEED TO BE MODIFIED FOR DOUBLY-LINKED LIST public void add(T element) {
Node
if (itsFirstNode == null) { itsFirstNode = node; itsLastNode = node; } else { itsLastNode.setNextNode(node); node.setPriorNode(itsLastNode);//prior node set from new node to last node itsLastNode = node; } size++; }
// THIS WILL NEED TO BE MODIFIED FOR DOUBLY-LINKED LIST public void add(T element, int index) {
int counter = 0; Node
while (current.getNextNode() != null ) {
if (counter == index - 1 ) break;
current = current.getNextNode(); counter++; } newNode.setNextNode(current.getNextNode()); Node
public T get(int index) {
int counter = 0; Node
while (current.getNextNode() != null ) {
if (counter == index) break;
current = current.getNextNode(); counter++; } return current.getData(); }
// returns true if element is in the list, false if not public boolean contains(T element) {
Node
if (itsFirstNode == null) return res;
else {
while (head != null) {
if(head.getData() == element){ res = true; break; } head.setNextNode(head.getNextNode()); } } return res; }
// returns the index of the element if it is in the list, -1 if not found public int indexOf(T element) {
Node
if(itsFirstNode == null) return -1;
else {
while (head != null) {
if (head.getData() == element) { break; } head.setNextNode(head.getNextNode()); index++; } } return index; }
// returns an Iterator at the location of the element if it is in the list // returns the null reference if the element is not found /* we cannot get java iterator to an user defined object unless implementing iterable interface*/ /* therefore i changed return type to Node
Node
if (itsFirstNode == null) return null;
else {
while (head != null) {
if (head.getData() == element) { break; } head.setNextNode(head.getNextNode()); } } return head; }
public String toString() {
String returnVal = ""; Node
if (size != 0 ) {
while (current.getNextNode() != null ) { returnVal += current.toString(); returnVal += " "; current = current.getNextNode(); } returnVal += current.toString(); } return returnVal; } // end toString
class Node
public Node(T data) { itsNext = null; itsPrior = null; this.data = data; }
public T getData() { return this.data; }
public Node
public Node
public void setNextNode(Node
public void setPriorNode(Node
public String toString() { return data.toString(); }
} // end of Node
} // end LinkedList
-------------------------------------------------------------------------------------------------------------------------------
public class Iterator
private LinkedList
public Iterator(LinkedList
// return true if there is a "next" element, otherwise returns false public boolean hasNext() { if (myCurrentNode != null) return true; return false; }
// return true if there is a "prior" element, otherwise returns false public boolean hasPrior() { if (myCurrentNode.getNextNode() != null) return true; return false; }
// returns the "next" node (really the current one) and // moves the Iterator forward by one node public T next() { T data = myCurrentNode.getData(); myCurrentNode = myCurrentNode.getNextNode(); return data; }
// returns the "prior" node (really the current one) and // moves the Iterator backward by one node public T prior() { T data = myCurrentNode.getData(); myCurrentNode = myCurrentNode.getPriorNode(); return data; }
// Sets this iterator to point to the last Node in the list public void setToEnd() { while (myCurrentNode.getNextNode() != null) myCurrentNode = myCurrentNode.getNextNode(); }
} //end Iterator
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started