Hallo, wir haben die Aufgabe eine doppelt verkettete Liste zu machen
die habe ich soweit hinbekommen
jetzt brauch ich aber noch einen Iterator und muss das über Iterable implementieren
z.B. für eine For each schleife
Ich weiß aber nicht wie man das genau macht
Also uns wurde gesagt wir brauchen dazu eine eigene neue Iterator Klasse usw
aber ich weiß nicht wie das aussehn muss -.-
Hier mein bis jetziger Code, ohne Iterable Implementierung
die habe ich soweit hinbekommen
jetzt brauch ich aber noch einen Iterator und muss das über Iterable implementieren
z.B. für eine For each schleife
Ich weiß aber nicht wie man das genau macht
Also uns wurde gesagt wir brauchen dazu eine eigene neue Iterator Klasse usw
aber ich weiß nicht wie das aussehn muss -.-
Hier mein bis jetziger Code, ohne Iterable Implementierung
Java:
package lists.doublechained;
import java.util.Iterator;
import lists.ListElem;
/**
*
*/
public class LinkedList<T extends Comparable<T>> implements Iterable<T>
{
/**
* Head element of the list
*/
private ListElem<T> head;
/**
* Tail/Last element of the list
*/
private ListElem<T> tail;
/**
* Number of entries stored in the list
*/
public int size;
public LinkedList() {
this.head = null;
this.tail = null;
this.size = 0;
}
/**
* adds a new element to the list's head
*
* @param data
* the content to add
*/
public void addFirst(T data) {
ListElem<T> newElem = new ListElem<T>();
newElem.data = data;
newElem.next = head;
head = newElem;
size++;
}
/**
* adds a new element to the list's tail
*
* @param data
* the content to add
*/
public void addLast(T data) {
ListElem<T> newElem = new ListElem<T>();
newElem.data = data;
newElem.prev = tail;
tail = newElem;
size++;
}
/**
* returns the element located at the given position
*
* @param index
* the given position
* @return the element contained at the given position or null if given
* index does not exist.
*/
public T get(int index) {
if (index >= size || index < 0) {
return null;
} else {
ListElem<T> current = head;
while (index-- > 0) {
current = current.next;
}
return current.data;
}
}
/**
* removes and returns the first element of the list
*
* @return the first element if one exists, null otherwise
*/
public T removeFirst() {
T result = null;
if(size < 2){
result = null;
}
else
{
result = head.data;
head = head.next;
size--;
}
return result;
}
/**
* removes and returns the last element of the list
*
* @return the last element if one exists, null otherwise
*/
public T removeLast() {
T result = null;
if(size < 2){
result = null;
}
else
{
result = tail.data;
tail = tail.prev;
size--;
}
return result;
}
/**
* removes and returns the element located at the given position
*
* @param index
* the given position
* @return the element contained at the given position or null if given
* index does not exist.
*/
public T remove(int index) {
T result = null;
if(index < 0 || index >= size){
result = null;
}else{
if(index == 0){
result = head.data;
head = head.next;
size--;
}else{
ListElem<T> current = head;
while(index-- > 1){
current = current.next;
}
result = current.next.data;
current.next = current.next.next;
size--;
}
}
return result;
}
/**
* @return whether this list is empty or not
*/
public boolean isEmpty() {
return this.size == 0;
}
/**
* @return the list's size
*/
public int size() {
return this.size;
}
}
Zuletzt bearbeitet: