Question

In: Computer Science

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++

Solutions

Expert Solution

//since you haven't mentioned more aboout list
//i have created list class.
//see the list.h for the methods that are implemented.
//every method that performs on list object are present in list.h
//dont' confuse.

--------------- list.h ------------

#ifndef __list_h
#define __list_h
#include<iostream>
using namespace std;
class list
{
   public:
       int *array;
       int maxSize;
       int length;
       list(int size)
       {
           //check if size is greater than 0 if negative
           //size is given set to default size;
           size = size>0?size: 5;
           maxSize = size;
           array = new int[maxSize];
           length = 0;
       }
       //overload the operator [] to access the array elements.
       int operator[] (int ind)
       {
           if(!isEmpty() && ind >= 0 && ind < length)
           {
               return array[ind];
           }
           else
           {
               cout<<"\nError: Index out of range."<<endl;
               return -1;
           }
          
       }
       //push the value if there is space and
       //insert in sorted order.
       void push(int val)
       {
           if(length < maxSize)
           {
               int i;
               for(i = length-1; (i>=0 && array[i] > val);i--)
               {
                   array[i+1] = array[i];
               }
               array[i+1] = val;
               length++;
           }
           else
           {
               cout<<"\nList is full\n";
           }
       }
       //remove element at given index.
       void remove(int ind)
       {
           if(!isEmpty() && ind >= 0 && ind < length)
           {
               for(int i = ind;i<length-1;i++)
               {
                   array[i] = array[i+1];
               }
               length--;
           }
       }
       //remove all occurances of val in list.
       void removeAll(int val)
       {
           if(length > 0)
           {
               int ind = indexOf(val);
               if(ind != -1)
               {
                   while(array[ind] != val)
                   {
                       remove(ind);
                   }
               }
               else
               {
                   cout<<"\nValue:"<<val<<" not found"<<endl;
               }
           }
           else
           {
               cout<<"\nList is empty"<<endl;
           }
       }
       //print the array elements.
       void print()
       {
           cout<<"\nList of Size: "<<length<<"\n"<<endl;
           if(length == 0)
           {
               cout<<"\n{}"<<endl;
               return;
           }
           cout<<"{ ";
           for(int i = 0;i<length;i++)
           {
               cout<<array[i];
               if(i<length-1)
               {
                   cout<<" , ";
               }
           }
           cout<<" }\n";
       }
       //returns the index of given value
       //it uses binary search to locate the
       //the values since array is in sorted order.
       int indexOf(int val)
       {
           int left = 0;
           int right = length-1;
           int middle;
          
           while(left <=right)
           {
               middle = left + (right - left) / 2;
               if(array[middle] == val)
               {
                   return middle;
               }
               if(array[middle] < val)
               {
                   left = middle + 1;
               }
               else
               {
                   right = middle - 1;
               }
           }
           return -1;
       }
       //returns the total elements in array.
       int size()
       {
           return length;
       }
       //returns if array is empty or not.
       bool isEmpty()
       {
           return length == 0?true:false;
       }
       //clears the values in array and sets the length to 0.
       void clear()
       {
           for(int i = 0;i<length;i++)
           {
               array[i] = 0;
           }
           length = 0;
       }};
#endif

//--------------------------- main.cpp ----------------------------

//since you haven't mentioned more aboout list
//i have created list class.
//see the list.h for the methods that are implemented.
//every method that performs on list object are present in list.h
//dont' confuse.

#include "list.h"
#include<iostream>

using namespace std;

//since i have used list class defined can't use const in function declaration.
list intersection(list &L1, list &L2)
{
   //get the sizes of two lists passed.
   int size1 = L1.size();
   int size2 = L2.size();
  
   //we have to compare the list with smaller elements
   //to the other.
   //other wise it will take time
   if(size1<size2)
   {
       //if list 1 has lesser lemenets
       //create res list of size1.
       list res(size1);
       //iterate through list one elements
       for(int i =0;i<size1;i++)
       {
           //first check if i_th value of L1 is not in result list
           //if not present then
           // also check for presence of i_th value in L2 .
           if(res.indexOf(L1[i]) == -1 && L2.indexOf(L1[i])!= -1)
           {
               //if both conditions satisfy insert L1[i] to the result array
               res.push(L1[i]);
           }
       }
       //finally return the result.
       return res;
   }
   else
   {
       //result list
       list res(size2);
       //iterate through list 2(smaller one)
       for(int i =0;i<size2;i++)
       {
           //if check for list2 value in result is not already present.
           //and list2 value in list1
           //then add to the result list.
           if(res.indexOf(L2[i]) == -1 && L1.indexOf(L2[i])!= -1)
           {
               res.push(L2[i]);
           }
       }
       //return it.
       return res;
   }

}
int main()
{
   int len = 10;
  
   list one(len);
   list two(len);
   //Test - 1
   //list one is larger with 10 elements 1 - 10
   //list two smaller with 5 elements 0 - 4
   for(int i = 1;i<=len;i++)
   {
       one.push(i);
   }
   for(int i= 0;i<len/2;i++)
   {
       two.push(i);
   }
   cout<<"\n---------------- Test 1 ----------------------\n";
   cout<<"\n---- List 1 --------\n";
   one.print();
   cout<<"\n---- List 2 --------\n";
   two.print();
   //get the intersection of them it should be 1 - 4.
   list common = intersection(one,two);
   cout<<"\n---------Intersection------\n";
   common.print();
  
   //clear both the list values.
   one.clear();
   two.clear();
  
  
   cout<<"\n---------------- Test 2 ----------------------\n";
   for(int i = len/2;i<=len;i++)
   {
       one.push(i);
   }
   for(int i= 0;i<len;i++)
   {
       two.push(i);
   }
  
   cout<<"\n---- List 1 --------\n";
   one.print();
   cout<<"\n---- List 2 --------\n";
   two.print();
   //get the intersection of them it should be 5,6,7,8,9.
   common = intersection(one,two);
   cout<<"\n---------Intersection------\n";
   common.print();
  
   //clear both the list values.
   one.clear();
   two.clear();

   cout<<"\n---------------- Test 3 ----------------------\n";
   for(int i = len/2;i<len-1;i++)
   {
       one.push(i);
       one.push(i);
   }
   //to check sorted insertion.
   one.push(1);
   for(int i= 0;i<len;i++)
   {
       two.push(i);
   }
  
   cout<<"\n---- List 1 --------\n";
   one.print();
   cout<<"\n---- List 2 --------\n";
   two.print();
   //get the intersection of them it should be 1,5,6,7,8.
   common = intersection(one,two);
   cout<<"\n---------Intersection------\n";
   common.print();
  
   //clear both the list values.
   one.clear();
   two.clear();

   return 0;
}

//-------------- OUTPUT --------------

//PLEASEEEEEEEEEEEEEE LIKE THE ANSWER......


Related Solutions

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?
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
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??
create a function to merge and concatenate two linked list l1 =[1,2,4] and l2=[1,3,4]. dont modify...
create a function to merge and concatenate two linked list l1 =[1,2,4] and l2=[1,3,4]. dont modify l2 (the nodes inserted in l1 should be copies of l2 class Node(object): def __init__(self, data): self.data = data self.next = None    class LinkedList(object): def print_list(self): cur_node = self.head while cur_node: print(cur_node.data) cur_node = cur_node.next def __init__(self): self.head = None;    def append(self, data): new_node = Node(data) if self.head is None: self.head = new_node return last_node = self.head while last_node.next: last_node = last_node.next...
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...
For the Poincare plane, find two lines L1 and L2 and a point P off each...
For the Poincare plane, find two lines L1 and L2 and a point P off each such that through P there are exactly two lines parallel to both L1 and L2.
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:...
Derive Lens equation of two convex lenses (L1 and L2) separated by 5 cm in Air....
Derive Lens equation of two convex lenses (L1 and L2) separated by 5 cm in Air. Write down the System Matrix equation and define each matrix elements.
designing a boiler in a chemical plant which is operated on twolevel L1 and L2 and...
designing a boiler in a chemical plant which is operated on twolevel L1 and L2 and two temeprature settings T1 and T2 ....L1: start inlet valve for fluid to flow in tank , L2 : stop inlet valve, T1 and T@ are the safe temperature range for some operation consider you as system engineer depending on follwing > promram development safe systes commiossioning fault finding sytem documentation What are the necessary action and feature you will add to task for...
. Observation data for the three L1, L2 and L3 locations are presented in the table...
. Observation data for the three L1, L2 and L3 locations are presented in the table as below L1 L2 L3 5 3 6 7 2 8 7 5 6 13 11 9 10 using α = 5%, test whether the conditions at the three locations are the same?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT