Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SortedIntegerSet {
- private SetNode first, last;
- private int numItems;
- public SortedIntegerSet() {
- last = new SetNode(0, null); // Problem, hier wird schon ein Eintrag erstellt
- first = last;
- numItems = 0;
- }
- public int size() {
- return numItems;
- }
- public boolean isEmpty() {
- return (first == last);
- }
- public int insert(int data) {
- SetNode cur = first;
- for (int i = 0; i < numItems; i++) {
- if (cur.next.data < data)
- cur = cur.next;
- else {
- cur.next = new SetNode(data, cur.next);
- numItems++;
- return 0;
- }
- }
- last.next = new SetNode(data, null);
- last = last.next;
- numItems++;
- return 0;
- }
- public String toString() {
- SetNode cur = first;
- String ret = "";
- for (int i = 0; i < numItems; i++) {
- ret += cur.data + " ";
- cur = cur.next;
- }
- return ret;
- }
- }
- class SetNode {
- int data;
- SetNode next;
- public SetNode(int data, SetNode next) {
- this.data = data;
- this.next = next;
- }
- }
- class Main {
- public static void main(String[] args) {
- SortedIntegerSet sis = new SortedIntegerSet();
- sis.insert(9);
- sis.insert(10);
- sis.insert(4);
- sis.insert(7);
- System.out.println(sis);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement