Friday 24 November 2017

C++ Program to Implement List in STL


Code:

#include      iostream
#include      list
#include      string
#include      cstdlib
using namespace std;
int main()
{
    int myints[] = {5, 6, 3, 2, 7};
    list l, l1 (myints, myints + sizeof(myints) / sizeof(int));
    list::iterator it;
    int choice, item;
    while (1)
    {
        cout<<"\n---------------------"<
        cout<<"List Implementation in Stl"<
        cout<<"\n---------------------"<
        cout<<"1.Insert Element at the Front"<
        cout<<"2.Insert Element at the End"<
        cout<<"3.Delete Element at the Front"<
        cout<<"4.Delete Element at the End"<
        cout<<"5.Front Element of List"<
        cout<<"6.Last Element of the List"<
        cout<<"7.Size of the List"<
        cout<<"8.Resize List"<
        cout<<"9.Remove Elements with Specific Values"<
        cout<<"10.Remove Duplicate Values"<
        cout<<"11.Reverse the order of elements"<
        cout<<"12.Sort Forward List"<
        cout<<"13.Merge Sorted Lists"<
        cout<<"14.Display Forward List"<
        cout<<"15.Exit"<
        cout<<"Enter your Choice: ";
        cin>>choice;
        switch(choice)
        {
        case 1:
            cout<<"Enter value to be inserted at the front: ";
            cin>>item;
            l.push_front(item);
            break;
        case 2:
            cout<<"Enter value to be inserted at the end: ";
            cin>>item;
            l.push_back(item);
            break;
        case 3:
            item = l.front();
            l.pop_front();
    cout<<"Element "<
            break;
        case 4:
            item = l.back();
            l.pop_back();
    cout<<"Element "<
            break;
        case 5:
    cout<<"Front Element of the List: ";
    cout<
            break;
        case 6:
            cout<<"Last Element of the List: ";
            cout<
            break;
        case 7:
            cout<<"Size of the List: "<
            break;
        case 8:
            cout<<"Enter new size of the List: ";
            cin>>item;
            if (item <= l.size())
                l.resize(item);
            else
                l.resize(item, 0);
            break;
        case 9:
            cout<<"Enter element to be deleted: ";
            cin>>item;
            l.remove(item);
            break;
        case 10:
            l.unique();
            cout<<"Duplicate Items Deleted"<
            break;
        case 11:
            l.reverse();
            cout<<"List reversed"<
            break;
        case 12:
            l.sort();
            cout<<"List Sorted"<
            break;
        case 13:
            l1.sort();
            l.sort();
            l.merge(l1);
            cout<<"Lists Merged after sorting"<
            break;
        case 14:
            cout<<"Elements of the List:  ";
            for (it = l.begin(); it != l.end(); it++)
                cout<<*it<<"  ";
            cout<
            break;
case 15:
            exit(1);
    break;
        default:
            cout<<"Wrong Choice"<
        }
    }
    return 0;
}


Output:

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 1
Enter value to be inserted at the front: 5

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 1
Enter value to be inserted at the front: 6

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 1
Enter value to be inserted at the front: 7

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 1
Enter value to be inserted at the front: 8

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 2
Enter value to be inserted at the end: 4

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 2
Enter value to be inserted at the end: 3

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 2
Enter value to be inserted at the end: 2

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 7 
Size of the List: 7

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List:  8  7  6  5  4  3  2  

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 5
Front Element of the List: 8

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 6
Last Element of the List: 2

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 9
Enter element to be deleted: 7

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List:  8  6  5  4  3  2  

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 1
Enter value to be inserted at the front: 8

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 2
Enter value to be inserted at the end: 2

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List:  8  8  6  5  4  3  2  2  

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 10
Duplicate Items Deleted

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List:  8  6  5  4  3  2  

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 12
List Sorted

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List:  2  3  4  5  6  8  

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 13
Lists Merged after sorting

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List:  2  2  3  3  4  5  5  6  6  7  8  

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 9
Enter element to be deleted: 7

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List:  2  2  3  3  4  5  5  6  6  8  

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 11
List reversed

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List:  8  6  6  5  5  4  3  3  2  2  

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 10
Duplicate Items Deleted

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List:  8  6  5  4  3  2  

---------------------
List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 15


------------------
(program exited with code: 1)
Press return to continue



More C++ Programs:



















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...