Answer to Question #258996 in Python for lemhs

Question #258996

Implement LinkedList using STL in c++. [#include <list> ,#include <iterator>]

Create two lists L1 and L2 of length 10, filling with the values of multiples of 2 and 3 respectively in L1 and L2. (hint: use push_back())

a. Show the contents of the list using iterators

b. Reverse the list L1 and display its contents (reverse())

c. Sort the list L2 and display its contents (sort())

d. Remove the element from both the sides of L1 and display its contents (pop_front() ,pop_back())

e. Add the elements to both the front and back sides of L2 and display its contents.


1
Expert's answer
2021-10-30T23:55:19-0400
#include <bits/stdc++.h>
#include <iostream>
#include <list>
#include <iterator>
#define ll long long


using namespace std;


void show(list <int> l){
	list <int> :: iterator it; 
	for(it = l.begin(); it != l.end(); it++){
		cout<<*it<<" ";
	}
	
}	




int main(){
	//Create two lists L1 and L2 of length 10, filling with the values of 
	//multiples of 2 and 3 respectively in L1 and L2. (hint: use push_back())
	list <int> L1;
	list <int> L2;
	int length=10;
	for(int i=0; i<length; i++){
		L1.push_back(i*2);
		L2.push_back(i*3); 
	}
	
	// Show the contents of the list using iterators
	cout<<"List 1 content:\n";
	show(L1);
	cout<<"\n";
	cout<<"List 2 content:\nn";
	show(L2);
	cout<<"\n";
	
	//b. Reverse the list L1 and display its contents (reverse())
	L1.reverse();
	show(L1);
	cout<<"\n";
	
	//c. Sort the list L2 and display its contents (sort())
	L2.sort();
	show(L2);
	cout<<"\n";
	
	//d. Remove the element from both the sides of L1 and 
	//display its contents (pop_front() ,pop_back())
	L1.pop_front();
	L1.pop_back();
	
	//e. Add the elements to both the front and back sides of L2 and display its contents.
	L2.push_back(1);
	L2.push_front(2);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog