Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Union

The union operation for MultisetAsLinkedList class requires the merging of two ordered, linked lists as shown in Program gif. We have assumed that the smallest element contained in a multiset is found at the head of the linked list and the largest is at the tail.

   program27794
Program: MultisetAsLinkedList class union method.

The union method computes its result as follows: The main loop of the program (lines 17-30) traverses the linked lists of the two operands, in each iteration appending the smallest remaining element to the result. Once one of the lists has been exhausted, the remaining elements in the other list are simply appended to the result (lines 31-34). The total running time for the union method is O(m+n), where tex2html_wrap_inline66438 and tex2html_wrap_inline66440.


next up previous contents index

Bruno Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.