C++. Please leave comments explaining. Thank you!
Given two unsorted STL lists X and P , write a valid C++ function intersection( X , P ) that returns a new list that contains the elements common to both X and P .
For example, if X = 5, 2, 1, 4 and P = 4, 5, 7; your algorithm should return a new list containing 5 and 4 (order is not important).
Also specify the running time of your algorithm using Big-Oh notation. Hint: As the lists are unsorted, a straightforward algorithm would need a double for loop.
#include<bits/stdc++.h>
using namespace std;
list<int> intersection(list<int>&
x,list<int>& p)
{
list<int> r;
for (std::list<int>::iterator it = x.begin(); it != x.end();
it++)
{
for (std::list<int>::iterator it1= p.begin();
it1 != p.end(); it1++)
{
if(*it==*it1)
r.push_back(*it);
}
}
return r;
}
int main(int argc, char const *argv[])
{
list<int> x;
x.push_back(1);
x.push_back(3);
x.push_back(2);
list<int> p;
p.push_back(1);
p.push_back(3);
list<int> r=intersection(x,p);
for (std::list<int>::iterator it = r.begin();
it != r.end(); it++)
{
cout<<*it<<" ";
}
return 0;
}
===================================================================
akshay@akshay-Inspiron-3537:~$ g++ interse.cpp
akshay@akshay-Inspiron-3537:~$ ./a.out
1 3
C++. Please leave comments explaining. Thank you! Given two unsorted STL lists X and P ,...
2. Given two unsorted STL lists X and P, write a valid C++ function intersection(X, P) that returns a new list that contains the elements common to both X and P. For example, if X = 5, 2, 1, 4 and P = 4, 5, 7; your algorithm should return a new list containing 5 and 4 (order is not important). Also specify the running time of your algorithm using Big-Oh notation. Hint: As the lists are unsorted, a straightforward...
c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked list: template<class ItemType> struct NodeType { ItemType item; NodeType* next; }; template<class ItemType> class UList { public: UList(); // default constrctor UList(const UList &x); // we implement copy constructor with deep copy UList& operator = (UList &x); // equal sign operator with deep copy bool IsThere(ItemType item) const; // return true of false to indicate if item is...
MUST USE C++ PLEASE READ THE QUESTION CAREFULLY ADD COMMENTS AND EXPLAIN THE CODES PLEASE. Given the following skeleton of an unsorted list class that uses an unsorted linked list: template < class ItemType > struct NodeType { ItemType item; NodeType* next; }; template < class ItemType > class UList { public: UList(); // default constrctor UList(const UList &x); // we implement copy constructor with deep copy UList& operator = (UList &x); // equal sign...
LAB 13 Please use correct division of code into .h and .cpp files and Comments in code explain what's being done. Thanks you very much for your help Create an STL vector of 10 integers and store initial values (0 to 9 is fine). Use the std::random_shuffle algorithm to shuffle the list. Print the list. (use an iterator in a regular for loop) Use the std::sort algorithm to sort the list. Print the list again. (use a range-based for loop)...
Suppose we are given two sorted arrays (nondecreasing from index 1 to index n) X[1] · · · X[n] and Y [1] · · · Y [n] of integers. For simplicity, assume that n is a power of 2. Problem is to design an algorithm that determines if there is a number p in X and a number q in Y such that p + q is zero. If such numbers exist, the algorithm returns true; otherwise, it returns false....
Please solve in python and provide comments explaining your codes. Your output must be the same as the example and result given below in the images. This task is to write a very basic text editor. It allows you to add, delete and modify lines of text. Use a list of strings to store the lines, with each list element being one line. The elements of the list are modified according the commands given by the user. The editor repeatedly: 1. displays...
Please help with this, and leave comments explainging the code you wrote, thank you for your help Write a Lottery class that simulates a 6-number lottery (e.g. "Lotto"). The class should have an array of six integers named lotteryNumbers, and another array of six integers named userLotteryPicks. The class' constructor should use the Random class to generate a unique random number in the range of 1 to 60 for each of the 6 elements in the lotteryNumbers array. Thus, there...
IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...
Help with programming in C++ Phase 1 is complete, please help with phase 2. Thank you. Phase 1 You must design 3 algorithms, and provide both a flow chart and pseudo code for the algorithms. Algorithm descriptions: Given an integer parameter named current_number and two constant global variables: const int MIN_NUMBER = 1; const int MAX_NUMBER = 8; Create an algorithm named forward, that will advance ONE value through a sequence of numbers 1, 2, 3 ... MAX_NUMBER. In...
Please help with all four questions regarding LISP Programming. Thank you. Please answer all questions with output plesase. LISP Programming Assignment It is a good idea to start this assignment early; Lisp programming, while not inherently difficult, often seem somewhat foreign at first, particularly when it comes to recursion and list manipulation. This assignment is loosely based on material by Dr. Henri Casanova. Problem #1 Define a function that takes two arguments and returns the greater of the two. Problem...