/* * add (S, x): * Adds the element x to a set S, if it is not present already. * No return value. */ static void add(ref SetList S, int x) { /* if the element is already in the list, * or if it is equal or less than zero, * it will not be added to the list */ if (is_element_of(x, S) || x <= 0) { return; } /* Creates a new node * and fills it with value x */ SetNode add = new SetNode(); add.data = x; /* If the list is empty, * add it as first node in the list */ if (is_empty(S)) { S.first = add; } /* If the list isn't empty, * Iterate through list until the end * and add the new node */ else { SetNode node = S.first; while (node.next != null) { node = node.next; } node.next = add; } }
/* * union( S, T ): * returns the "union" of sets S and T */ static SetList union(SetList S, SetList T) { SetNode node_s = new SetNode(); node_s = S.first; SetNode node_t = new SetNode(); node_t = T.first; /* New SetList container for resulting set */ SetList union = new SetList(); /* adds elements of set S to the union set */ while (node_s != null) { /* Potential duplicates are already handled by the add() procedure */ add(ref union, node_s.data); /* Go to next iteration */ node_s = node_s.next; } /* Add all elements from set T to union set */ while (node_t != null) { add(ref union, node_t.data); node_t = node_t.next; } /* Return the final list */ return union; }