Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please code in c++, please read carefully as one misstep is huge, focus bolded c

ID: 3756277 • Letter: P

Question

Please code in c++, please read carefully as one misstep is huge, focus bolded content

------------------

Build the adjList by processing the each line of the input file adding nodes to the adjacency list
for a particular vertex in sorted order. You will use the find_mf function to obtain an iterator
that will be passed to the list::insert() method(please reference c++ website) along with the value to insert. Note, you are
NOT permitted to use any sorting algorithms or the list::sort() method.

adjList/adjancency list:

#include
#include
#include

using std::cout;
using std::endl;
using std::cin;
using std::list;
using std::vector;


int main()
{
vector > adjList;
list mtLst; // an empty list

for (int i=0; i<4; ++i)
{
adjList.push_back(mtLst); // push an empty list onto the adjList
for (int j=0; j<=i; ++j)
adjList[i].push_back(j); // populate the list just pushed onto the vector

}

return 0;
}

input file:
0 5 1 2 6
1 0
2 0
3 5 4
4 3 6 5
5 3 0 4
6 4 0
7 8
8 7
9 12 10 11
10 9
11 9 12
12 11 9

(This is what I came up with as the find_mf function had to returns an iterator to the first node in the list that is greater than the value of the object being passed in. )

find_mf function:

list::iterator mf(list::iterator start, list::iterator stop, int x);
template <class Iter>
int mf( Iter start, Iter stop )
{
int x = 1;

while(start != stop)
{
if (*start > x)
start++;
}

return start;
}

-----------------
Here is the desired output from this:
list0 0 1 2 5 6
list1 0 1
list2 0 2
list3 3 4 5
list4 3 4 5 6
list5 0 3 4 5
list6 0 4 6
list7 7 8
list8 7 8
list9 9 10 11 12
list10 9 10
list11 9 11 12
list12 9 11 12

Explanation / Answer

#include<bits/stdc++.h>
using namespace std;

list<int>:: iterator find_mf(list<int> mylist,int a)
{
list<int>:: iterator itr;
for(itr=mylist.begin();itr!=mylist.end();itr++)
{
if((*itr)>a)
break;
}
return itr;
}
int main()
{
int n,m,a;
cout<<"Enter the number of lists: ";
cin>>n;
vector <list<int> > adjList(n);

for (int i=0; i<n; ++i)
{
list<int> mtLst;
cout<<"Enter the number of elements in list "<<i<<endl;
cin>>m;
for (int j=0; j<m; ++j)
{
cout<<"Enter the element "<<j<<endl;
cin>>a;
if(j==0)
mtLst.push_back(a);
else
{
list<int>:: iterator p= find_mf(mtLst,a);
mtLst.insert(p,a);
}
}
adjList.push_back(mtLst);
}
vector <list<int> > :: iterator it;
for (it=adjList.begin(); it!= adjList.end(); ++it)
{
list<int> :: iterator itr;
for(itr=it->begin(); itr != it->end(); itr++)
cout<<*(itr)<<" ";
cout<<endl;
}

return 0;
}