Skip to content
Snippets Groups Projects
Commit 1ad0fb5c authored by Amaury Vanoorenberghe's avatar Amaury Vanoorenberghe :scroll:
Browse files

EX2 FIX - La classe tp2.MyList implémente maintenant Iterable<E> via la classe...

EX2 FIX - La classe tp2.MyList implémente maintenant Iterable<E> via la classe tp2.MyList.MyIterator
parent cea342ae
No related branches found
No related tags found
No related merge requests found
package tp2;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class MyList<E> {
public class MyList<E> implements Iterable<E> {
protected E element;
protected MyList<E> previous, next;
......@@ -38,12 +40,12 @@ public class MyList<E> {
return true;
}
public boolean add(int index, E item) {
public void add(int index, E item) {
MyList<E> here = subset(index);
if (here.isEmpty()) {
here.element = item;
return true;
return;
}
MyList<E> newNext = new MyList<E>(here.element, here, here.next);
......@@ -51,8 +53,6 @@ public class MyList<E> {
here.next = newNext;
here.element = item;
return true;
}
public E remove(int index) {
......@@ -200,4 +200,29 @@ public class MyList<E> {
return sb.toString();
}
@Override
public Iterator<E> iterator() {
return new MyIterator(this);
}
private class MyIterator implements Iterator<E> {
private MyList<E> here;
public MyIterator(MyList<E> list) {
here = list;
}
@Override
public boolean hasNext() {
return here != null && !here.isEmpty();
}
@Override
public E next() {
E item = here.element;
here = here.next;
return item;
}
}
}
package tp2;
import java.util.Iterator;
//import java.util.List;
import java.util.NoSuchElementException;
public class MyListOld<E> implements /*List<E>,*/ Iterable<E> {
protected MyListOld<E> previous, next;
protected E element;
public MyListOld() {
this(null, null, null);
next = new MyListOld<E>(this, null, null);
}
public MyListOld(MyListOld<E> previous, MyListOld<E> next, E element) {
this.element = element;
this.previous = previous;
this.next = next;
}
public boolean isEmpty() {
return element == null;
}
public boolean hasPrevious() {
return previous != null && !previous.isEmpty();
}
public boolean hasNext() {
return next != null && !next.isEmpty();
}
//@Override
public int size() {
//return !isEmpty() ? 1 + (hasNext() ? next.size() : 0) : 0;
MyListOld<E> l = this;
int count = !isEmpty() ? 1 : 0;
while (l.hasNext()) {
l = l.next;
++count;
}
return count;
}
//@Override
public void clear() {
next = getGuard();
element = null;
}
private MyListOld<E> getGuard() {
MyListOld<E> l = this;
while (l.next != null) {
l = l.next;
}
return l;
}
//@Override
public E get(int i) {
MyListOld<E> l = this;
for (; i> 0; --i) {
if (!l.hasNext()) {
throw new IndexOutOfBoundsException(i);
}
l = l.next;
}
return l.element;
}
//@Override
public int indexOf(Object o) {
int idx = 0;
for (E e : this) {
if (e.equals(o)) {
return idx;
}
++idx;
}
return -1;
}
//@Override
public boolean contains(Object o) {
return indexOf(o) != -1;
}
//@Override
public int lastIndexOf(Object o) {
int idx = size();
MyListOld<E> l = getGuard();
while (l.hasPrevious()) {
l = l.previous;
--idx;
if (l.element.equals(o)) {
return idx;
}
}
return -1;
}
//@Override
public boolean add(E element) {
MyListOld<E> l = getGuard();
l.element = element;
l.next = new MyListOld<E>(l, null, null);
return true;
}
public void add(int index, E element) {
MyListOld<E> here = this;
for (int i = 0; i < index; ++i) {
if (!here.hasNext()) {
throw new IndexOutOfBoundsException(index);
}
here = here.next;
}
MyListOld<E> temp = new MyListOld<E>();
temp.previous = here;
temp.next = here.next;
here.next = temp;
temp.element = here.element;
here.element = element;
}
//@Override
public E remove(int index) {
MyListOld<E> here = this;
for (int i = 0; i < index; ++i) {
if (!here.hasNext()) {
throw new IndexOutOfBoundsException(index);
}
here = here.next;
}
return here.remove();
}
private E remove() {
E temp = element;
element = next.element;
next = next.next;
return temp;
}
//@Override
public boolean remove(Object o) {
MyListOld<E> l = this;
while (l.hasNext()) {
if (l.element.equals(o)) {
l.remove();
return true;
}
}
return false;
}
//@Override
public String toString() {
StringBuilder sb = new StringBuilder("[");
Iterator<E> li = iterator();
if (li.hasNext()) {
sb.append(li.next().toString());
}
while (li.hasNext()) {
sb.append(String.format(", %s", li.next()));
}
sb.append("]");
return sb.toString();
}
//@Override
public Iterator<E> iterator() {
return new MyIterator(this);
}
private class MyIterator implements Iterator<E> {
private MyListOld<E> list;
public MyIterator(MyListOld<E> list) {
this.list = list;
}
@Override
public boolean hasNext() {
return list != null && list.next != null;
}
@Override
public E next() {
if(list.isEmpty() || !hasNext()) {
throw new NoSuchElementException();
}
E result = list.element;
list = list.next;
return result;
}
public boolean hasPrevious() {
return list != null && !list.hasPrevious();
}
@SuppressWarnings("unused")
public E previous() {
if(!hasPrevious()) {
throw new NoSuchElementException();
}
list = list.previous;
return list.element;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment