I got an assignment and needed to created a linkedlist class, i did a little bit
ID: 3552361 • Letter: I
Question
I got an assignment and needed to created a linkedlist class, i did a little bit by myself but i get stuck with the set() method and listiterator() method. I need someone to finish for me.
here is what i got so far:
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
public class MyLinkedList<Product> {
private Node<Product> head = null;
private Node<Product> tail = null;
private Node<Product> temp = null;
private int counter = 0;
public MyLinkedList() {
}
public int size(){return counter;}
public Product get(int position){
assert (position >= 0 && position < size());
temp = head;
for(int i = 0; i < position; i++) temp = temp.next;
return temp.element;
}
public void add(Product element) {
if (head == null) {
head = tail = new Node<Product>();
head.element = element;
head.next = tail;
tail = head;
}else{
tail.next = new Node<Product>();
tail = tail.next;
tail.element = element;
}
counter++;
}
/*public void set(int position, Product element) {
if (position == size()) {
add(element);
return;
} else if (position == 0) {
}
}*/
public void set(int inLayer, Product element) {
if (head == null) {
head = tail = new Node<Product>();
head.element = element;
head.next = tail;
tail = head;
}else{
tail.next = new Node<Product>();
tail = tail.next;
tail.element = element;
}
counter++;
}
public ListIterator<Product> iterator() {
return ListIterator<Product>();
}
Node<Product> current = head;
boolean hasNext() {
return (current.next != null);
}
Product next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
current = current.next;
return current.value;
}
Explanation / Answer
listiterator code is clear, i think. You don't need to make any change there, just change names according to your declarations & requirement.
For set method, It is similar to get method, in get method you go to particular index & then return element stored at that index,
for set method similarly, you have to reach at given index "inlayer" & instead of returning value from there, set value of that index to given value. You have to also take care of size zero list.
It will look like:
public void set(int inlayer, product ele){
assert (position >= 0 && position < size());
temp = head;
for(int i = 0; i < position; i++) temp = temp.next;
temp.element = ele;
}
I think this will be helpful for you.
listiterator method:
private class MyListIterator implements ListIterator<E> {
private Node current;
// your own class is going to give this out
public MyListIterator(Node current) { this.current = current; }
// current will never be null
public boolean hasNext() { return current.next != null; }
public boolean hasPrevious() { return current.prev != null; }
public E next() {
if(!hasNext()) { return null; }
current = current.next;
return current.data;
}
public E previous() {
if(!hasPrevious()) { return null; }
current = current.prev;
return current.data;
}
}
set() method
Your set() method ignores the index parameter that is passed in. As written it just modifies the item at the head of the list.
You will have to implement set() similarily to get() - find the item with the specified index, then set that item's element.
You need to move current forward index times before setting the value. Look at how you are doing it in get and use similar logic
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.