Using standard template library (STL) list container implement following member functions of list class: empty, insert, merge, reverse, sort, Unique, using iterator.
#include<iostream>
#include<algorithm>
#include<list>
using namespace std;
int main()
{
char ans;
list<int>l1,l2;
int ch;
int temp;
list<int>::iterator v;
cout<<"\n1: Accept";
cout<<"\n2: Display";
cout<<"\n3:Merging list";
cout<<"\n4:Sorting lists";
cout<<"\n 5:Unique lists";
cout<<"\n6:Reverse list";
cout<<"\n7: Clear lists";
do
{
cout<<"\nEnter the choice";
cin>>ch;
switch(ch)
{
case 1: //Accept list
{
cout<<"\nAccept lists list1 and list2";
cout<<"\nEnter elements";
for(int i=0;i<6;i++)
{
cin>>temp;
l1.push_back(temp);
}
cout<<"\nEnter list 2 elements";
for(int i=0;i<6;i++)
{
cin>>temp;
l2.push_back(temp);
}
}
break;
case 2: //Display list
{
cout<<"\nDisplay lists";
cout<<"\nList1 elements are";
for(v=l1.begin();v!=l1.end();v++)
{
cout<<"\t"<<*v;
}
cout<<"\nSize of list1\n"<<l1.size();
cout<<"\nDisplay list2 elements\n";
for(v=l2.begin();v!=l2.end();v++)
{
cout<<"\t"<<*v;
}
cout<<"\nSize of list2\n"<<l2.size();
for(v=l1.begin();v!=l1.end();v++)
{
cout<<"\t"<<*v;
}
}
break;
case 3: //Merging list
cout<<"\nMerging lists\n";
l1.merge(l2);
break;
case 4: //sorting list
cout<<"\nsorting list\n";
l1.sort();
break;
case 5: //Unique list
cout<<"\nunique list\n";
l1.unique();
break;
case 6: //Reverse list
cout<<"\nReverse list\n";
l1.reverse();
break;
case 7: //Clear list
cout<<"\nClear list\n";
l1.clear();
break;
}
cout<<"\nDo you want to continue?";
cin>>ans;
}while(ans=='y'||ans=='Y');
return 0;
}
------------------------------------------------------------------------
Output
[student@localhost Moddle]$ g++ list.cpp
[student@localhost Moddle]$ ./a.out
1: Accept
2: Display
3:Merging list
4:Sorting lists
5:Unique lists
6:Reverse list
7: Clear lists
Enter the choice1
Accept lists list1 and list2
Enter elements1
2
3
2
1
4
Enter list 2 elements2
6
9
7
4
0
Do you want to continue?y
Enter the choice5
unique list
Do you want to continue?y
Enter the choice2
Display lists
List1 elements are 1 2 3 2 1 4
Size of list1
6
Display list2
2 6 9 7 4 0
Size of list2
6
1 2 3 2 1 4
Do you want to continue?n
[student@localhost Moddle]$
#include<algorithm>
#include<list>
using namespace std;
int main()
{
char ans;
list<int>l1,l2;
int ch;
int temp;
list<int>::iterator v;
cout<<"\n1: Accept";
cout<<"\n2: Display";
cout<<"\n3:Merging list";
cout<<"\n4:Sorting lists";
cout<<"\n 5:Unique lists";
cout<<"\n6:Reverse list";
cout<<"\n7: Clear lists";
do
{
cout<<"\nEnter the choice";
cin>>ch;
switch(ch)
{
case 1: //Accept list
{
cout<<"\nAccept lists list1 and list2";
cout<<"\nEnter elements";
for(int i=0;i<6;i++)
{
cin>>temp;
l1.push_back(temp);
}
cout<<"\nEnter list 2 elements";
for(int i=0;i<6;i++)
{
cin>>temp;
l2.push_back(temp);
}
}
break;
case 2: //Display list
{
cout<<"\nDisplay lists";
cout<<"\nList1 elements are";
for(v=l1.begin();v!=l1.end();v++)
{
cout<<"\t"<<*v;
}
cout<<"\nSize of list1\n"<<l1.size();
cout<<"\nDisplay list2 elements\n";
for(v=l2.begin();v!=l2.end();v++)
{
cout<<"\t"<<*v;
}
cout<<"\nSize of list2\n"<<l2.size();
for(v=l1.begin();v!=l1.end();v++)
{
cout<<"\t"<<*v;
}
}
break;
case 3: //Merging list
cout<<"\nMerging lists\n";
l1.merge(l2);
break;
case 4: //sorting list
cout<<"\nsorting list\n";
l1.sort();
break;
case 5: //Unique list
cout<<"\nunique list\n";
l1.unique();
break;
case 6: //Reverse list
cout<<"\nReverse list\n";
l1.reverse();
break;
case 7: //Clear list
cout<<"\nClear list\n";
l1.clear();
break;
}
cout<<"\nDo you want to continue?";
cin>>ans;
}while(ans=='y'||ans=='Y');
return 0;
}
------------------------------------------------------------------------
Output
[student@localhost Moddle]$ g++ list.cpp
[student@localhost Moddle]$ ./a.out
1: Accept
2: Display
3:Merging list
4:Sorting lists
5:Unique lists
6:Reverse list
7: Clear lists
Enter the choice1
Accept lists list1 and list2
Enter elements1
2
3
2
1
4
Enter list 2 elements2
6
9
7
4
0
Do you want to continue?y
Enter the choice5
unique list
Do you want to continue?y
Enter the choice2
Display lists
List1 elements are 1 2 3 2 1 4
Size of list1
6
Display list2
2 6 9 7 4 0
Size of list2
6
1 2 3 2 1 4
Do you want to continue?n
[student@localhost Moddle]$
Comments
Post a Comment