Question

When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses...

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<T> {
   Node<T> itsFirstNode;
   Node<T> itsLastNode;
   private int size;

   public LinkedList() {
       itsFirstNode = null;
       itsLastNode = null;  
       size = 0;
   }

   public Iterator<T> getIterator() {
       return new Iterator(this);
   }

   // THIS WILL NEED TO BE MODIFIED FOR DOUBLY-LINKED LIST
   public void add(T element) {

       Node<T> node = new Node(element);

       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<T> newNode = new Node(element);
       Node<T> current = itsFirstNode;

       while (current.getNextNode() != null ) {

           if (counter == index - 1 )
               break;

           current = current.getNextNode();
           counter++;
       }
       newNode.setNextNode(current.getNextNode());
       Node<T> temp=current.getNextNode();//temp node is next node of current node
       temp.setPriorNode(newNode);
       current.setNextNode(newNode);
       newNode.setPriorNode(current);
       size++;  
   }

   public T get(int index) {

       int counter = 0;
       Node<T> current = itsFirstNode;

       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<T> head = itsFirstNode;
       boolean res = false;

       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<T> head = itsFirstNode;
       int index = 0;

       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<T> and return reference to current node*/
   public Node<T> iteratorAt(T element) {

       Node<T> head = itsFirstNode;

       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<T> current = itsFirstNode;

       if (size != 0 ) {

           while (current.getNextNode() != null ) {
               returnVal += current.toString();
               returnVal += "\n";
               current = current.getNextNode();
           }
           returnVal += current.toString();
       }
       return returnVal;
   } // end toString

   class Node<T> {
       private T data;
       private Node<T> itsNext;
       private Node<T> itsPrior;

       public Node(T data) {
           itsNext = null;
           itsPrior = null;
           this.data = data;
       }

       public T getData() {
           return this.data;
       }

       public Node<T> getNextNode() {
           return itsNext;
       }

       public Node<T> getPriorNode() {
           return itsPrior;
       }

       public void setNextNode(Node<T> next) {
           itsNext = next;
       }

       public void setPriorNode(Node<T> prior) {
           itsPrior=prior;
       }

       public String toString() {
           return data.toString();
       }

   } // end of Node<T>

} // end LinkedList<T> class

-------------------------------------------------------------------------------------------------------------------------------

public class Iterator<T> {

    private LinkedList<T> myList;
    private LinkedList<T>.Node<T> myCurrentNode;

    public Iterator(LinkedList<T> list) {
        myList = list;
        myCurrentNode = myList.itsFirstNode;
    }

    // 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<T> ckass

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The problem is solved. I have modified LinkedList class and highlighed changes made

public class LinkedList<T> {
Node<T> itsFirstNode;
Node<T> itsLastNode;
private int size;
public LinkedList() {
itsFirstNode = null;
itsLastNode = null;
size = 0;
}
public Iterator<T> getIterator() {
return new Iterator<T>(this);
}
// THIS WILL NEED TO BE MODIFIED FOR DOUBLY-LINKED LIST
public void add(T element) {
Node<T> node = new Node<T>(element);
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<T> newNode = new Node<T>(element);
Node<T> current = itsFirstNode;
while (current.getNextNode() != null ) {
if (counter == index - 1 )
break;
current = current.getNextNode();
counter++;
}
newNode.setNextNode(current.getNextNode());
Node<T> temp=current.getNextNode();//temp node is next node of current node
temp.setPriorNode(newNode);
current.setNextNode(newNode);
newNode.setPriorNode(current);
size++;
}
public T get(int index) {
int counter = 0;
Node<T> current = itsFirstNode;
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<T> head = itsFirstNode;
boolean res = false;
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<T> head = itsFirstNode;
int index = 0;
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<T> and return reference to current node*/
public Node<T> iteratorAt(T element) {
Node<T> head = itsFirstNode;
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<T> current = itsFirstNode;
if (size != 0 ) {
while (current.getNextNode() != null ) {
returnVal += current.toString();
returnVal += "\n";
current = current.getNextNode();
}
returnVal += current.toString();
}
return returnVal;
} // end toString
class Node<T> {
private T data;
private Node<T> itsNext;
private Node<T> itsPrior;
public Node(T data) {
itsNext = null;
itsPrior = null;
this.data = data;
}
public T getData() {
return this.data;
}
public Node<T> getNextNode() {
return itsNext;
}
public Node<T> getPriorNode() {
return itsPrior;
}
public void setNextNode(Node<T> next) {
itsNext = next;
}
public void setPriorNode(Node<T> prior) {
itsPrior=prior;
}
public String toString() {
return data.toString();
}
} // end of Node<T>
} // end LinkedList<T> class

Add a comment
Know the answer?
Add Answer to:
When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • (1) Implement the countKey(T element) method, which should return a count of the number of times...

    (1) Implement the countKey(T element) method, which should return a count of the number of times that the given key (the element) is found in the list. (2) Implement the indexOf(T element) method, which is similar as the indexOf method in the String class. It returns the index (the position starting from the head node) of the first occurrence of the given element, or -1, if the element does not occur in the list. You will need to track the...

  • Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(),...

    Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 {    private Node head, tail; private int size;    public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...

  • Improve the speed of public T get(int i) by having it work backward from the end...

    Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> {    private static class Node<T> {        public Node<T> prev, next;...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • public class LinkedList {          // The LinkedList Node class    private class Node{...

    public class LinkedList {          // The LinkedList Node class    private class Node{               int data;        Node next;               Node(int gdata)        {            this.data = gdata;            this.next = null;        }           }       // The LinkedList fields    Node head;       // Constructor    LinkedList(int gdata)    {        this.head = new Node(gdata);    }...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test...

    Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test class: Remember your header with name, date, and assignment. Also include class names that will be tested. Psuedocode (level 0 or mixture of level 0 and algorithm [do not number steps]) is required if main() contains more than simple statements (for example, your program includes constructs for decisions (if/else), loops, and methods. For Secondary class(es): Include a JavaDoc comment that describes the purpose of...

  • In Java You may add any classes or methods to the following as you see fit in order to complete t...

    In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...

  • Complete LinkedListCollection.java with following methods: public LinkedListCollection(); public LinkedListCollection(int size); public boolean isEmpty(); public int size();...

    Complete LinkedListCollection.java with following methods: public LinkedListCollection(); public LinkedListCollection(int size); public boolean isEmpty(); public int size(); // Return number of elements in the Collection. public String toString(); public boolean add(T element); // Add an element into the Collection, return true if successful public boolean remove(T target); // Remove the target from Collection, return true if successful public boolean removeAll(T target); // Remove all occurrences of Target, return if successful public void removeDuplicate(); // Remove duplicated element(s) public boolean equals(LinkedListCollection that);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT