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

Given two sorted lists L1 and L2, write a procedure to compute L1∪L2 using only the...
Given two sorted lists L1 and L2, write a procedure to compute L1∪L2 using only the basic list operations. Pseudo-code is acceptable.
The intersection of two lists L1 and L2, L1 ∩ L2, is defined as the list...
The intersection of two lists L1 and L2, L1 ∩ L2, is defined as the list containing elements in both L1 and L2 only. Given two sorted lists L1 and L2, write a function, called intersection, to compute L1 ∩ L2 using only the basic list operations. The intersection function is defined as follows template list intersection( const list & L1, const list & L2); C++
Problem 1: You have two sorted lists of integers, L1 and L2. You know the lengths...
Problem 1: You have two sorted lists of integers, L1 and L2. You know the lengths of each list, L1 has length N1 and L2 has length N2. Design a linear running time complexity algorithm (ONLY PSEUDOCODE) to output a sorted list L1 ∧ L2 (the intersection of L1 and L2). Important Notes: For this problem, you don’t need to submit any implementation in Java. Only the pseudocode of your algorithm is required. Pseudocode is a simple way of writing...
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
Given lists L1, L2 and L3, delete all the nodes having even number in info part...
Given lists L1, L2 and L3, delete all the nodes having even number in info part from the list L1 and insert into list L2 and all the nodes having odd numbers into list L3. Code needed in java.
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.
Given two sorted linked lists, merge them into a third sorted linked list. If an element...
Given two sorted linked lists, merge them into a third sorted linked list. If an element is present in both the lists, it should occur only once in the third list. Code needed in java.
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??
in C++ Given two integer arrays sorted in the ascending order, code the function SortArrays to...
in C++ Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #include <iostream> using namespace std; void SortArrays (int a[], int b[], int c[], int size); void printArray(int m[], int length); const int NUM = 5; int main() { int arrA[NUM] = {-2, 31, 43, 55, 67};...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT