Commit 152e3895 authored by Adam Blank's avatar Adam Blank
Browse files

after lecture06

parent 42a06346
Showing with 95 additions and 0 deletions
+95 -0
import java.util.Iterator;
public interface ICollection<E> extends Iterable<E> {
public void add(E e);
public void clear();
public Iterator<E> iterator();
default public boolean contains(E e) {
for (E item : this) {
if (e == null ? item == null : e.equals(item)) {
return true;
}
}
return false;
}
public int size();
default public boolean isEmpty() {
return this.size() == 0;
}
}
\ No newline at end of file
public class LinkedList<E> implements IList<E> {
private Node<E> head;
private int size;
private static class Node<E> {
public final E data;
public Node<E> next;
public Node(E data) {
this(data, null);
}
public Node(E data, Node<E> next) {
this.data = data;
this.next = next;
}
}
@Override
public int size() {
return this.size;
}
@Override
public void set(int idx, E elem) {
if (idx >= this.size || idx < 0) {
throw new IllegalArgumentException();
}
}
@Override
public E get(int idx) {
return null;
}
@Override
public void clear() {
this.head = null;
this.size = 0;
}
@Override
public void add(int idx, E elem) {
if (idx > this.size || idx < 0) {
throw new IllegalArgumentException();
}
}
@Override
public void add(E elem) {
if (this.head == null) {
this.head = new Node<>(elem);
}
else {
// Write only the else in C16
// I AM ABOUT TO WRITE CODE THAT DOESN'T WORK
// BEWARE!!
/*
Node<E> curr = this.head;
while (curr.next != null) {
curr = curr.next;
}
curr.next = new Node<>(elem);
*/
Node<E> curr;
for (curr = this.head; curr.next != null; curr = curr.next) {
}
}
this.size++;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment