Standard Template Library – Lists (C++)

/*    Google standard template library to use list container and using C++ implement following member functions of list class: empty, insert,merge,reverse,sort    */    #include<iostream>    #include<list>    using namespace std;    int main()    {        list<int> list1,list2;            list<int>::iterator p;            char ch;            int a;            do            {                cout<<"\n***************MENU****************\n";                    cout<<"\n1.Create a list\n";                    cout<<"\n2.Display a list\n";                    cout<<"\n3.Insert an element\n";                    cout<<"\n4.Check if list is empty\n";                    cout<<"\n5.Reverse the list\n";                    cout<<"\n6.Sort the list\n";                    cout<<"\n7.Merge two lists\n";                    cout<<"\n************************************\n";                    cout<<"\n Enter your choice(1-7):\n";                    cin>>a;                    switch(a)                    {                    case 1:                        int n,a;                            cout<<"\n Enter the number of elements you wish to enter:\n";                            cin>>n;                            cout<<"\nEnter your elements:\n";                            for(int i=0;i<n;i++)                            {                                cin>>a;                                    list1.push_back(a);                            }                            cout<<"\n List created!\n";                            break;                    case 2:                                                    for(p=list1.begin();p!=list1.end();++p)                            {                                cout<<*p<<" ";                            }                            break;                    case 3:                        int b;                            cout<<"Enter the new element:";                            cin>>b;                            list1.push_back(b);                            cout<"\n Generating new list......\n";                            cout<<"\n Displaying new list.......\n";                            for(p=list1.begin();p!=list1.end();++p)                            {                                cout<<*p<<" ";                            }                            break;                    case 4:                        if(list1.empty())                            {                                cout<<"\n The list is empty.";                            }                            else                            {                                cout<<"\n The list is not empty.";                            }                            break;                    case 5:                        list1.reverse();                                                        for(p=list1.begin();p!=list1.end();++p)                            {                                cout<<*p<<" ";                            }                            break;                    case 6:                        list1.sort();                                                       for(p=list1.begin();p!=list1.end();++p)                            {                                cout<<*p<<" ";                            }                            break;                    case 7:                        int ele;                            cout<<"\n Enter the number of elements you wish to enter:\n";                            cin>>n;                            cout<<"\nEnter your elements:\n";                            for(int i=0;i<n;i++)                            {                                cin>>ele;                                list2.push_back(ele);                            }                            list1.merge(list2);                                          for(p=list1.begin();p!=list1.end();++p)                            {                                cout<<*p<<" ";                            }                                                       break;                    }                    cout<<"\n Do you want to continue?(y/n):\n";                    cin>>ch;                }while(ch=='y');    }

View Article Page
Download