High School

Given two sorted lists, L1 and L2, write a procedure to compute L1 ∩ L2 (intersection) using only the basic list operations.

Given two sorted lists, L1 and L2, write a procedure to compute L1 ∪ L2 (union) using only the basic list operations.

Answer :

To compute L1 ∩ L2 (intersection) using basic list operations, you can follow this procedure:

1. Initialize an empty list called "intersection" to store the common elements.
2. Iterate through the elements in L1 using a loop.
3. For each element in L1, check if it is also present in L2.
4. If the element is present in both lists, add it to the "intersection" list.
5. Continue this process for all elements in L1.
6. Return the "intersection" list as the result.

To compute L1 ∪ L2 (union) using basic list operations, follow this procedure:

1. Initialize a new list called "union" and add all elements from L1.
2. Iterate through the elements in L2 using a loop.
3. For each element in L2, check if it is not already present in the "union" list.
4. If the element is not present in the "union" list, add it.
5. Continue this process for all elements in L2.
6. Return the "union" list as the result.

These procedures use only basic list operations like iteration, comparison, and adding elements to a list.

To compute L1 ∩ L2 using only the basic list operations, we can implement the following procedure,1. Create an empty result list, L3.
2. Initialize two pointers, p1 and p2, to the beginning of L1 and L2, respectively.
3. While both p1 and p2 are not at the end of their respective lists, compare the values at p1 and p2.
4. If the values are equal, add the value to L3 and increment both pointers.
5. If the value at p1 is less than the value at p2, increment p1.
6. If the value at p2 is less than the value at p1, increment p2.
7. Once one of the pointers reaches the end of its list, the intersection has been found.
8. Return L3 as the result.

To compute L1 ∪ L2 using only the basic list operations, we can implement the following procedure:

1. Create an empty result list, L3.
2. Initialize two pointers, p1 and p2, to the beginning of L1 and L2, respectively.
3. While both p1 and p2 are not at the end of their respective lists, compare the values at p1 and p2.
4. If the value at p1 is less than the value at p2, add the value at p1 to L3 and increment p1.
5. If the value at p2 is less than the value at p1, add the value at p2 to L3 and increment p2.
6. If the values are equal, add the value to L3 once and increment both pointers.
7. Once one of the pointers reaches the end of its list, add the remaining values in the other list to L3.
8. Return L3 as the result.

To know more about value click here

brainly.com/question/30760879

#SPJ11