Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. class SortedIntegerSet {
  2. private SetNode first, last;
  3. private int numItems;
  4.  
  5. public SortedIntegerSet() {
  6. last = new SetNode(0, null); // Problem, hier wird schon ein Eintrag erstellt
  7. first = last;
  8.  
  9. numItems = 0;
  10. }
  11.  
  12. public int size() {
  13. return numItems;
  14. }
  15.  
  16. public boolean isEmpty() {
  17. return (first == last);
  18. }
  19.  
  20. public int insert(int data) {
  21. SetNode cur = first;
  22. for (int i = 0; i < numItems; i++) {
  23. if (cur.next.data < data)
  24. cur = cur.next;
  25. else {
  26. cur.next = new SetNode(data, cur.next);
  27. numItems++;
  28. return 0;
  29. }
  30. }
  31. last.next = new SetNode(data, null);
  32. last = last.next;
  33. numItems++;
  34. return 0;
  35. }
  36.  
  37. public String toString() {
  38. SetNode cur = first;
  39. String ret = "";
  40. for (int i = 0; i < numItems; i++) {
  41. ret += cur.data + " ";
  42. cur = cur.next;
  43. }
  44. return ret;
  45. }
  46. }
  47.  
  48. class SetNode {
  49. int data;
  50. SetNode next;
  51.  
  52. public SetNode(int data, SetNode next) {
  53. this.data = data;
  54. this.next = next;
  55. }
  56. }
  57.  
  58.  
  59.  
  60. class Main {
  61. public static void main(String[] args) {
  62. SortedIntegerSet sis = new SortedIntegerSet();
  63. sis.insert(9);
  64. sis.insert(10);
  65. sis.insert(4);
  66. sis.insert(7);
  67. System.out.println(sis);
  68.  
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement