Verstehe das Thema noch nicht ganz und finde auhc nicht viel dazu.
HAbe eine erste Implemtentierung gemacht, die natürlich nicht funktioniert.
Welche Sachen sollte ich beachten?
HAbe eine erste Implemtentierung gemacht, die natürlich nicht funktioniert.
Welche Sachen sollte ich beachten?
Code:
import a01.AbstractPerson;
import a01.Person;
import a02.List;
public class LinkedList implements List {
protected Node head;
protected Node tail;
int size;
public LinkedList() {
this.tail = new Node(null, tail);
this.head = new Node(null, tail);
this.size = 0;
}
protected class IteratorImpl implements Iterator<Node> {
public Node actual;
public IteratorImpl(Node actual) {
this.actual = actual;
}
public boolean hasNext() {
return actual!=null;
}
public Person next() {
return actual.getPerson();
}
public IteratorImpl iterator() {
IteratorImpl iterator=new IteratorImpl(head.next);
return iterator;
}
}
protected class Node {
public Node next;
public Person person;
public Node(Person person, Node next) {
this.next = next;
this.person = person;
}
public Node getNext() {
return this.next;
}
public Person getPerson() {
return this.person;
}
}
public int clear() {
int zähle = 0;
Node node = head;
while (node != tail) {
zähle++;
node = node.getNext();
}
head.next = tail;
return zähle;
}
public Person get(int i) {
if (i < 0) {
throw new IndexOutOfBoundsException();
}
Node node = head;
while (i-- > 0) {
if (node.next == tail) {
throw new IndexOutOfBoundsException();
}
node = node.getNext();
}
return node.person;
}
[COLOR="YellowGreen"]//Mittels Iterator Liste durchlaufen und an Stelle i einfügen
Ist das richtig oder vollkommen falsch?
Sollte ich es mit iterator() machen?Aber wo wende ich den an?Speziell?[/COLOR]
public void insert( int i,AbstractPerson person) {
if (i <= 0) {
throw new IndexOutOfBoundsException();
} else {
IteratorImpl iterate =new IteratorImpl(head);
int stelle = 0;
while (iterate.hasNext()) {
stelle++;
if (stelle == i) {
Node g = new Node((Person) person, iterate.actual.next);
iterate.actual.next = g;
}
}
}
size++;
}
public void remove(int i) {
if (i < 0) {
throw new IndexOutOfBoundsException();
} else if (i == 0) {
System.out.println("Entfernen ist nicht möglich");
} else {
Node node = head;
int stelle = 0;
while (node != tail) {
stelle++;
if (stelle == i) {
node.next = node.getNext().getNext();
}
node = node.getNext();
}
}
size--;
}
public void reverse() {
int i = 0;
Node prev = head;
Node actualNode = head;
Node nextNode = actualNode.next;
while (i <= size) {
prev = actualNode;
actualNode = nextNode;
nextNode = actualNode.next;
actualNode.next = prev;
i++;
}
nextNode = head;
head = tail;
tail = nextNode;
tail.next = prev;
}
public int size() {
return size;
}
public String toString() {
String string = "[";
Node node = head.next;
while (node != null && node.getPerson() != null) {
string += node.getPerson().toString();
node = node.getNext();
if (node != null && node.getPerson() != null) {
string += " ; ";
}
}
return string + "]";
}
public static void main(String[] args) {
LinkedList alpha = new LinkedList();
alpha.insert(1, new Person("Tobias", "Salomon", 1985, "12423"));
alpha.insert(2, new Person("Melanie", "Mieritz", 1987, "10117"));
alpha.insert(2, new Person("Antje", "Roloff", 1980, "12567"));
alpha.insert(1, new Person("Jennifer", "Hübner", 1986, "12478"));
System.out.println(alpha.size());
System.out.println(alpha);
alpha.reverse();
System.out.println(alpha);
System.out.println(alpha.get(2));
alpha.remove(2);
System.out.println(alpha);
System.out.println(alpha.clear());
System.out.println(alpha);
}
}