12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import java.util.Iterator;
-
- class SortertEnkelListe<E extends Comparable & Lik> implements AbstraktSortertEnkelListe<E> {
- public class Node {
- public Node next;
- public E val;
-
- Node(E val) {
- this.val = val;
- }
- }
-
- Node first;
-
- public void settInn(E val) {
- Node nw = new Node(val);
-
- if (first == null) {
- first = nw;
- return;
- }
-
- Node prev = null;
- Node curr = first;
- while (curr != null) {
- if (nw.val.compareTo(curr.val) <= 0) {
- nw.next = curr;
- if (prev == null)
- first = nw;
- else
- prev.next = nw;
- return;
- }
- prev = curr;
- curr = curr.next;
- }
-
- //Since we've not returned yet, this node is the biggest in the list
- prev.next = nw;
- }
-
- public E hent(String key) {
- Node n = first;
-
- while (n != null) {
- if (n.val.samme(key))
- return n.val;
-
- n = n.next;
- }
-
- return null;
- }
-
- public Iterator<E> iterator() {
- return new Iterator<E>() {
- private Node n = first;
-
- public boolean hasNext() {
- return n != null;
- }
-
- public E next() {
- E val = n.val;
- n = n.next;
- return val;
- }
- };
- }
- }
|