Question

Given two sorted lists, L1 and L2, write a procedure to compute L1 ∩ L2 using...

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

Homework Answers

Answer #1

This question asks you to write a function that returns the elements common to both sorted list.The following function achieves this:

List Intersect( List L1, List L2 )

{
Position L1_pos, L2_pos, Result_pos;
List Result;
L1_pos = L1->Next;
L2_pos = L2->Next;
Result_pos = Result = MakeEmpty();
while ( L1_pos != NULL && L2_pos != NULL )
{
if ( L1_pos->Element < L2_pos->Element )
L1_pos = L1_pos->Next;
else if ( L1_pos->Element > L2_pos->Element )
L2_pos = L2_pos->Next;
else
{
Insert( L1_pos->Element , Result , Result_pos );
L1_pos = L1_pos->Next;
L2_pos = L2_pos->Next;
Result_pos = Result_pos->Next;
}
}
return Result;
}

For the two linked lists L1 and L2, we assume that the length of L1 is n1 and the length
of L2 is n2. Then the running time of the above procedure is O(n1 + n2).

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Given two sorted lists, L1 and L2, write a procedure to compute L1 ∪ L2 using...
Given two sorted lists, L1 and L2, write a procedure to compute L1 ∪ L2 using only the basic list operations.
Q2) (1 point) Given two lists, L1 of length n and L2 of length m. We...
Q2) (1 point) Given two lists, L1 of length n and L2 of length m. We say that L2 is a subsequence of L1 if we can remove elements from L1 to produce L2. This means that there exists indices i1 < ... < im such that L1[ij] = L2[j] for each j. Design an algorithm that detects if L2 is a subsequence of L1 and outputs the indices i1,....,im if L2 is a subsequence of L1. a.Provide the pseudo-code...
Consider a firm using quantities L1 and L2 of two kinds of labour as its only...
Consider a firm using quantities L1 and L2 of two kinds of labour as its only inputs in order to produce output Q=L1+L2. Thus, each unit of labour produces one unit of output. Suppose that we also have two segmented labor markets, with the following inverse labor supply functions. w1=α1+β1L1 w2=α2+β2L2 which shows the wage that must be paid to attract a given labor supply. Assume that the firm is competitive and take price of output P as given. (α1,...
Write a function intersect(L, M) that consumes two sorted lists of distinct integers L and M,...
Write a function intersect(L, M) that consumes two sorted lists of distinct integers L and M, and returns a sorted list that contains only elements common to both lists. You must obey the following restrictions: No recursion or abstract list functions, intersect must run in O(n) where n is the combined length of the two parameters. Example: intersect([3, 7, 9, 12, 14], [1, 2, 5, 7, 10, 11, 12]) => [7, 12] Hint: As the title hints at, you are...
ABC ltd is a circuit board producer and produces two products, L1 & L2. The company...
ABC ltd is a circuit board producer and produces two products, L1 & L2. The company currently uses traditional costing system for making business decisions. However, recently the company is considering a move towards the Activity-Based Costing (ABC) system. As one of the company’s accountant’s you have been asked to prepare a statement comparing both the costing methods for the next company board meeting. Your supervisor has given you the following quarterly data: - Total Indirect Costs for the quarter:...
In Python, implement a function that takes in two sorted lists and merges them into one...
In Python, implement a function that takes in two sorted lists and merges them into one list, the new list must be sorted. The function MUST run in O(m+n), where m is the length of list 1 and n the length of list 2. In other words, the function cannot do more than one pass on list 1 and list 2.
You are given a list, L, and another list, P, containing integers sorted in ascending order....
You are given a list, L, and another list, P, containing integers sorted in ascending order. The operation printLots(L, P) will print the elements in L that are in positions specified by P. For instance, if P = 1, 3, 4, 6, the elements in positions 1, 3, 4, and 6 in L are printed. Write the procedure printLots(L, P). You may use only the public STL container operations. What is the running time of your procedure?
Project 1 - NodeList write in c++ with 5 files: main.cpp List.h List.cpp ListNode.h ListNode.cpp Building...
Project 1 - NodeList write in c++ with 5 files: main.cpp List.h List.cpp ListNode.h ListNode.cpp Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3,...
write program to compute intersection of two sorted array of integers and compute the CPU time...
write program to compute intersection of two sorted array of integers and compute the CPU time for different sets of unsigned integers generated by a random number generator test this using the same data sets: atleast 3 of size 1000 integers, atleast 3 of size 10000 integers, atleast 3 of size 100000 integers, atleast 3 of one million integers and atleast 3 of size 10 million integers DONT FORGET CPU TIME FOR EACH ONE Java
In C++ You are given two arrays each of which is sorted. Write a method called...
In C++ You are given two arrays each of which is sorted. Write a method called mergeIt that takes the two arrays and merges them into one array. For instance, if you had 5, 9, 11 and 4, 6, 7 then you need to merge it to 4,5,6,7,9,11.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT