APL/DEV 3.2/TP03/Chaine/ListChain.java
2022-10-21 16:11:26 +02:00

124 lines
2.6 KiB
Java

import java.util.*;
public class ListChain<E> implements Collection<E> {
ChainLink<E> firstElement;
ChainLink<E> lastElement;
@Override
public boolean add(E e) {
if (firstElement == null) {
firstElement = new ChainLink<E>(e);
lastElement = firstElement;
} else {
ChainLink<E> newElement = new ChainLink<E>(e);
lastElement.setNext(newElement);
lastElement = newElement;
}
return true;
}
@Override
public boolean addAll(Collection<? extends E> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public void clear() {
firstElement = null;
lastElement = null;
}
@Override
public boolean contains(Object o) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public Iterator<E> iterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean remove(Object o) {
return false;
}
public boolean remove(int n) {
ChainLink<E> prev = firstElement;
for (int i = 0; i < n-1; i++) {
if (prev.getNext() != null) {
prev = prev.getNext();
} else {
return false;
}
}
ChainLink<E> toRemove = prev.getNext();
if (toRemove.getNext() == null) return false;
prev.setNext(toRemove.getNext());
toRemove = null;
return true;
}
public E get(int n) {
ChainLink<E> e = firstElement;
for (int i = 0; i < n; i++) {
if (e.getNext() != null) {
e = e.getNext();
}
}
return e.getValue();
}
@Override
public boolean removeAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object[] toArray() {
// TODO Auto-generated method stub
return null;
}
@Override
public <T> T[] toArray(T[] a) {
// TODO Auto-generated method stub
return null;
}
}