Implementing Set Operations with Sorted Linked Lists in C, Assignments of Data Structures and Algorithms

The implementation details of various set operations, including intersection, union, difference, and subset, using sorted linked lists in c. The concept behind each operation and provides the corresponding code snippets. It is intended for students or developers who want to understand or implement these data structures.

Typology: Assignments

Pre 2010

Uploaded on 08/30/2009

koofers-user-7dt-1
koofers-user-7dt-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
worksheet 13: Sorted List Sets Name:
Worksheet 13: Sorted List Sets
An earlier lesson examined sorted vectors. Remember that there were two
important reasons to keep vectors in order. One is so we could use binary search
to perform a fast searching operation. But the second was that two sorted vectors
could be rapidly merged into a third.
There is no equivalent to binary search for a list. That is, even if a list is ordered,
searching still requires walking through the entire list. But the idea that two lists
can be rapidly merged into a third is still applicable. As with the vector, simply
walk down both collections in sequence, at each step copying the smallest
element into a new list:
QuickTime™ and a
TIFF (LZW) decompressor
are needed to see this picture.
We can use this insight to make a much faster Set abstraction. Recall from
Lesson 24 that the naive implementations of set union, intersection and
difference are each O(n2). In this lesson you will make a container that is much
faster. We will build on the list abstraction you created in Lessons 34 and 36.
The simplest operation to understand is intersection. To form an intersection
simply walk through the two lists, forming a new list that will contain the
intersection. If an element is found in both, add it the new intersection list.
Otherwise if an element is found in only one of the collections, ignore it.
An Active Learning Approach to Data Structures using C 1
pf3
pf4

Partial preview of the text

Download Implementing Set Operations with Sorted Linked Lists in C and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Worksheet 13: Sorted List Sets

An earlier lesson examined sorted vectors. Remember that there were two important reasons to keep vectors in order. One is so we could use binary search to perform a fast searching operation. But the second was that two sorted vectors could be rapidly merged into a third. There is no equivalent to binary search for a list. That is, even if a list is ordered, searching still requires walking through the entire list. But the idea that two lists can be rapidly merged into a third is still applicable. As with the vector, simply walk down both collections in sequence, at each step copying the smallest element into a new list: QuickTime™ and a TIFF (LZW) decompressor are needed to see this picture. We can use this insight to make a much faster Set abstraction. Recall from Lesson 24 that the naive implementations of set union, intersection and difference are each O(n^2 ). In this lesson you will make a container that is much faster. We will build on the list abstraction you created in Lessons 34 and 36. The simplest operation to understand is intersection. To form an intersection simply walk through the two lists, forming a new list that will contain the intersection. If an element is found in both, add it the new intersection list. Otherwise if an element is found in only one of the collections, ignore it.

Only slightly more complex is set union. Remember that elements in a union must be unique. If a value is found in only one of the collections add it to the union. If it is found in both, add only one copy to the union. A complication to the union code is that when the first loop finishes you still need to copy the remainder of the elements from the other list to the collection. A set difference is the elements from one set that are not found in the second. Forming this is similar to intersection. Finally, to determine if a set is a subset of another walk down both lists in parallel. If you ever find a value that is in the first set but not in the second, then return false. If you finish looping over the first set and have not yet returned false, then return true. Complete the implementation of the SortedLinkedListSet based on these ideas. Remember that each operation produces a new LinkedList to hold the result. As a very last step, the list header is changed to point to the new list, rather than the original.

/* assumes list1 and list2 are sorted and result is empty */ struct ptr1 = list1->frontSentinel->next; struct ptr2 = list2->frontSentinel->next; while (ptr1 != list1->backSentinel && ptr2 != list2->backSentinel) { if (LT(ptr1->value, ptr2->value)) { } else if (EQ(ptr1->value, ptr2->value)) { } else { } } } int listSubset (struct list *list1, struct list list2) { / assumes list1 and list2 are sorted and result is empty */ struct ptr1 = list1->frontSentinel->next; struct ptr2 = list2->frontSentinel->next; while (ptr1 != list1->backSentinel && ptr2 != list2->backSentinel) { if (LT(ptr1->value, ptr2->value)) { } else if (EQ(ptr1->value, ptr2->value)) { } else { } }