Hallo zusammen,
ich habe die Aufgabe bekommen eine doppelt verkettete Liste zu implementieren.
Wir haben folgenden Code als Vorlage bekommen:
Ich habe bereits versucht die "add" methode zu schreiben, jedoch fügt er das Element immer ganz hinten an die Liste, nicht an Position 0, falls diese übergeben wird.
Wo liegt bei "add" mein Fehler?
ich habe die Aufgabe bekommen eine doppelt verkettete Liste zu implementieren.
Wir haben folgenden Code als Vorlage bekommen:
Java:
/**
* Class representing a list of int values based on double linking
*/
public class MyDoubleLinkedList {
/**
* the header element of this list linking to the first and last element *
*/
private final DEntry header;
/** the number of elements in the list */
private int size;
public MyDoubleLinkedList() {
header = new DEntry(0, null, null);
header.next = header;
header.previous = header;
size = 0;
}
/**
* Returns the number of elements in this list
*
* @return the number of elements in this list
*/
public int size() {
return size;
}
/**
* Returns the value at the position specified
*
* @param position the 0 based position of the value to return
* @return the value at the passed position, -1 if unsuccessful. This means that
* this implementation is not working properly if -1 is stored in the
* list
*/
public int get(int position) {
// TODO: please put your code here
return 0;
}
/**
* Returns object of type DEntry at the position specified
*
* @param position the 0 based position of the value to return
* @return the object at the passed position, -1 if unsuccessful. This means
* that this implementation is not working properly if -1 is stored in
* the list
*/
public DEntry getEntry(int position) {
// TODO: please put your code here
return null;
}
/**
* Adds a new element into the list at the position specified
*
* @param position the 0 based position at which to add the passed value
* @param value the value to add
* @return 0 if adding was successful, -1 if not
*/
public int add(int position, int value) {
// TODO: please put your code here
if (position < 0) {
return -1;
}
DEntry temp = header;
int i = 0;
if (position == 0) {
temp = header;
} else {
while (i < position) {
temp = temp.next;
i++;
}
}
DEntry listEntry = new DEntry(value, null, null);
listEntry.next = temp;
listEntry.previous = temp.previous;
temp.previous.next = listEntry;
temp.previous = listEntry;
return 0;
}
/**
* Removes an element at the position specified from the list
*
* @param position the 0 based position of the value to remove
* @return value of the removed entry if removing was successful, -1 if not
*/
public int remove(int position) {
// TODO: please put your code here
size--;
return 0;
}
/**
* Searches for the first occurrence of the passed value in the list
*
* @param value the value to search
* @return the position of the value in the list, -1 if not found
*/
public int indexOf(int value) {
// TODO: please put your code here
return 0;
}
/**
* Prints the numbers in the list to console
*/
public void print() {
System.out.print("List: ");
DEntry result = header.next;
while (result != header) {
System.out.print(result.data + ", ");
result = result.next;
}
System.out.println();
}
/**
* A single list entry for double linking.
*/
@SuppressWarnings("unused")
class DEntry {
/** the data element represented by this entry */
private final int data;
/** reference to the previous element in the list */
private DEntry previous;
/** reference to the next element in the list */
private DEntry next;
/**
* @param data the data object this entry represents
* @param previous reference to the previous element in the list
* @param next reference to the next element in the list
*/
public DEntry(int data, DEntry previous, DEntry next) {
this.data = data;
this.previous = previous;
this.next = next;
}
}
}
Ich habe bereits versucht die "add" methode zu schreiben, jedoch fügt er das Element immer ganz hinten an die Liste, nicht an Position 0, falls diese übergeben wird.
Wo liegt bei "add" mein Fehler?