University stuff.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SortertEnkelListe.java 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import java.util.Iterator;
  2. class SortertEnkelListe<E extends Comparable & Lik> implements AbstraktSortertEnkelListe<E> {
  3. public class Node {
  4. public Node next;
  5. public E val;
  6. Node(E val) {
  7. this.val = val;
  8. }
  9. }
  10. Node first;
  11. public void settInn(E val) {
  12. Node nw = new Node(val);
  13. if (first == null) {
  14. first = nw;
  15. return;
  16. }
  17. Node prev = null;
  18. Node curr = first;
  19. while (curr != null) {
  20. if (nw.val.compareTo(curr.val) <= 0) {
  21. nw.next = curr;
  22. if (prev == null)
  23. first = nw;
  24. else
  25. prev.next = nw;
  26. return;
  27. }
  28. prev = curr;
  29. curr = curr.next;
  30. }
  31. //Since we've not returned yet, this node is the biggest in the list
  32. prev.next = nw;
  33. }
  34. public E hent(String key) {
  35. Node n = first;
  36. while (n != null) {
  37. if (n.val.samme(key))
  38. return n.val;
  39. n = n.next;
  40. }
  41. return null;
  42. }
  43. public Iterator<E> iterator() {
  44. return new Iterator<E>() {
  45. private Node n = first;
  46. public boolean hasNext() {
  47. return n != null;
  48. }
  49. public E next() {
  50. E val = n.val;
  51. n = n.next;
  52. return val;
  53. }
  54. };
  55. }
  56. }