Handledning/hjälp med remove(int index) i enkellänkad lista
Hej! Jag gör en egen enkellänkad lista i java men har fastnat. Är det någon som vet hur jag ska ordna så att det här testfallet assertEquals(4, list.size()); går igenom? list.add("A"); funkar men den noden kommer inte med när size() körs. Det har antagligen något med den sista noden last att göra i funktionen public E remove(int index). Säg till om någon mer kodbit behövs.
import java.util.Iterator;
public class ALDAListTest {
// These two methods are the only places in the code that mentions the name
// of your class.
private static ALDAList<String> createNewList() {
return new MinAldaList<String>();
}
private static ALDAList<Integer> createIntegerList() {
return new MinAldaList<Integer>();
}
private ALDAList<String> list = createNewList();
private void testField(java.lang.reflect.Field f) {
assertTrue("All attributes should (probably) be private ",
java.lang.reflect.Modifier.isPrivate(f.getModifiers()));
assertFalse("There is no reason to use any arrays on this assignment", f.getType().isArray());
assertFalse("There is (probably) not any reason to use any static attributes",
java.lang.reflect.Modifier.isStatic(f.getModifiers()));
for (Class<?> i : f.getType().getInterfaces()) {
assertFalse(
"You should implement the functionality yourself, not use any of the list implementations already available",
i.getName().startsWith("java.util.List"));
}
@Before
public void setUp() {
list.add("First");
list.add("Second");
list.add("Third");
list.add("Fourth");
list.add("Fifth");
}
@Test
public void testRemoveAtEnd() {
list.remove(4);
assertEquals(4, list.size());
list.remove(3);
assertEquals(3, list.size());
assertEquals("[First, Second, Third]", list.toString());
list.add("A");
assertEquals(4, list.size());
}
/**
* This is the list interface you should implement. It is a simplified version
* of <a href="http://docs.oracle.com/javase/7/docs/api/java/util/List.html">
* java .util.List</a> where you will find the documentation to the methods.
* Note though that remove(element) and contains(element) takes a parameter of
* type E, and not Object in this version. Also note that add(element) in the
* original version returns a boolean which we ignore.
*
* Do NOT rename this interface!
*
* @author Henrik
*/
public interface ALDAList<E> extends Iterable<E> {
public void add(E element);
public void add(int index, E element);
public E remove(int index);
public boolean remove(E element);
public E get(int index);
public boolean contains(E element);
public int indexOf(E element);
public void clear();
public int size();
}
import java.util.Iterator;
public class MinAldaList<E> implements ALDAList<E>{
private static class Node<E> {
E data;
Node next;
public Node(E data){
this.data = data;
}
}
private Node first;
private Node last;
@Override
public void add(E data) {
// Konstruktorn behövs inte eftersom first och last är null per default
if (first == null) {
first = new Node<E>(data);
last = first;
}
else {
last.next = new Node<E>(data);
last = last.next;
}
}
@Override
public int size() {
int size = 0;
for(Node temp=first; temp!=null; temp=temp.next){
size++;
}
return size;
}
@Override
public E remove(int index) {
Node<E> temp = first;
Node<E> saved = temp;
Node<E> tempinnan = temp;
if (index == 0){
first = temp.next;
return temp.data;
}
for (int i = 0; i<=index; i++){
if (i == index){
saved = temp;
tempinnan.next = temp.next;
}
tempinnan = temp;
temp = temp.next;
}
return saved.data;
}