Question

In: Computer Science

Given two sorted lists, L1 and L2, write an efficient C++ code to compute L1 ∩...

Given two sorted lists, L1 and L2, write an efficient C++ code to compute L1 ∩ L2 using only the basic STL list operations. What is the running time of your algorithm?

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>

/* Link list node */
struct Node {
   int data;
   struct Node* next;
};

struct Node* sortedIntersect(
   struct Node* a,
   struct Node* b)
{
   /* base case */
   if (a == NULL || b == NULL)
       return NULL;

   /* If both lists are non-empty */

   /* advance the smaller list and call recursively */
   if (a->data < b->data)
       return sortedIntersect(a->next, b);

   if (a->data > b->data)
       return sortedIntersect(a, b->next);

   // Below lines are executed only
   // when a->data == b->data
   struct Node* temp
       = (struct Node*)malloc(
           sizeof(struct Node));
   temp->data = a->data;

   /* advance both lists and call recursively */
   temp->next = sortedIntersect(a->next, b->next);
   return temp;
}

/* UTILITY FUNCTIONS */
/* Function to insert a node at
the beginging of the linked list */
void push(struct Node** head_ref, int new_data)
{
   /* allocate node */
   struct Node* new_node
       = (struct Node*)malloc(
           sizeof(struct Node));

   /* put in the data */
   new_node->data = new_data;

   /* link the old list off the new node */
   new_node->next = (*head_ref);

   /* move the head to point to the new node */
   (*head_ref) = new_node;
}

/* Function to print nodes in a given linked list */
void printList(struct Node* node)
{
   while (node != NULL) {
       printf("%d ", node->data);
       node = node->next;
   }
}

/* Driver program to test above functions*/
int main()
{
   /* Start with the empty lists */
   struct Node* a = NULL;
   struct Node* b = NULL;
   struct Node* intersect = NULL;

   /* Let us create the first sorted
   linked list to test the functions
   Created linked list will be
   1->2->3->4->5->6 */
   push(&a, 6);
   push(&a, 5);
   push(&a, 4);
   push(&a, 3);
   push(&a, 2);
   push(&a, 1);

   /* Let us create the second sorted linked list
   Created linked list will be 2->4->6->8 */
   push(&b, 8);
   push(&b, 6);
   push(&b, 4);
   push(&b, 2);

   /* Find the intersection two linked lists */
   intersect = sortedIntersect(a, b);

   printf("\n Linked list containing common items of a & b \n ");
   printList(intersect);

   return 0;
}


Related Solutions

1) Equations for two lines L1 and L2 are given. Find the angle between L1 and...
1) Equations for two lines L1 and L2 are given. Find the angle between L1 and L2. L1: ? = 7 + 2?, ? = 8 − 4?, ? = −9 + ? L2: ? = −8 − ?, ? = 3 − 3?, ? = 4 + 3? 2) Find polar form of complex number z : ?) ? = 4√3 − 4? ?) ? = 2√3 − 2i
1. Write a Racket function (set-equal? L1 L2) that tests whether L1 and L2 are equal....
1. Write a Racket function (set-equal? L1 L2) that tests whether L1 and L2 are equal. Two sets are equal if they contain exactly the same members, ignoring ordering (or in other words, two sets are equal if they are a subset of each other). For example (set-equal? '(1 (2 3)) '((3 2) 1)) ---> #t (set-equal? '(1 2 3) '((3 2)1)) ---> #f (set-equal? '(1 2 3) '((1 2 3))) ---> #f 2. Two common operations on sets are...
Given two lists, write python code to print “True” if the two lists have at least...
Given two lists, write python code to print “True” if the two lists have at least one common element. For example, x = [1,2,3], y=[3,4,5], then the program should print “True” since there is a common element 3.
how do i construct a scatterplot if L1 lists negative values and L2 has values such...
how do i construct a scatterplot if L1 lists negative values and L2 has values such as 0.00339156, 0.00326318, 0.00313725 ? Do i enter the L2 values as exponents??
Write a program to compute intersection of two sorted array of integers and compute the CPU...
Write a 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 NO HASH SET
what are Sorted lists and their efficiency? in c++ and data structures
what are Sorted lists and their efficiency? in c++ and data structures
Recall that given a line L1 in the plane with slope m, a second line L2...
Recall that given a line L1 in the plane with slope m, a second line L2 is perpendicular to L1 if and only if the slope of L2 is − 1 m . Now consider the function h(x) = x^2 + 3/2x + 3. Among all of the lines tangent to the graph of h(x), there is exactly one which is perpendicular to the line given by y = −2x + 7. Write the equation of that line tangent to...
Using pseudocode or C++ code, write a function to compute and return the product of two...
Using pseudocode or C++ code, write a function to compute and return the product of two sums. The first is the sum of the elements of the first half of an array A. The second is the sum of the elements of the second half of A. The function receives A and N ≥ 2, the size of A, as parameters. (in c++)
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
(a) Find the cosine of the angle between the lines L1 and L2 whose vector equations are given below:
(a) Find the cosine of the angle between the lines L1 and L2 whose vector equations are given below: L1 : ~r1(t) = [1, 1, 1] + t[1, 2, 3] L2 : ~r2(t) = [1, 1, 1] + t[−1, 4, 2]. (b) Find the equation of the plane that contains both L1 and L2.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT