Friday 24 November 2017

C++ Program to Implement Forward List in STL


Code:

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


Output:

---------------------
Forward_List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Delete Element at the Front
3.Front Element of Forward List
4.Resize Forward List
5.Remove Elements with Specific Values
6.Remove Duplicate Values
7.Reverse the order of elements
8.Sort Forward List
9.Merge Sorted Lists
10.Display Fo10rward List
11.Exit
Enter your Choice: 1
Enter value to be inserted at the front: 2
---------------------
Forward_List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Delete Element at the Front
3.Front Element of Forward List
4.Resize Forward List
5.Remove Elements with Specific Values
6.Remove Duplicate Values
7.Reverse the order of elements
8.Sort Forward List
9.Merge Sorted Lists
10.Display Fo10rward List
11.Exit
Enter your Choice: 1
Enter value to be inserted at the front: 3
---------------------
Forward_List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Delete Element at the Front
3.Front Element of Forward List
4.Resize Forward List
5.Remove Elements with Specific Values
6.Remove Duplicate Values
7.Reverse the order of elements
8.Sort Forward List
9.Merge Sorted Lists
10.Display Fo10rward List
11.Exit
Enter your Choice: 1
Enter value to be inserted at the front: 4
---------------------
Forward_List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Delete Element at the Front
3.Front Element of Forward List
4.Resize Forward List
5.Remove Elements with Specific Values
6.Remove Duplicate Values
7.Reverse the order of elements
8.Sort Forward List
9.Merge Sorted Lists
10.Display Fo10rward List
11.Exit
Enter your Choice: 1
Enter value to be inserted at the front: 5
---------------------
Forward_List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Delete Element at the Front
3.Front Element of Forward List
4.Resize Forward List
5.Remove Elements with Specific Values
6.Remove Duplicate Values
7.Reverse the order of elements
8.Sort Forward List
9.Merge Sorted Lists
10.Display Fo10rward List
11.Exit
Enter your Choice: 1
Enter value to be inserted at the front: 6
---------------------
Forward_List Implementation in Stl

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

---------------------
Forward_List Implementation in Stl

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

---------------------
Forward_List Implementation in Stl

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

---------------------
Forward_List Implementation in Stl

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

---------------------
Forward_List Implementation in Stl

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

---------------------
Forward_List Implementation in Stl

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

---------------------
Forward_List Implementation in Stl

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

---------------------
Forward_List Implementation in Stl

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

---------------------
Forward_List Implementation in Stl

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

---------------------
Forward_List Implementation in Stl

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

---------------------
Forward_List Implementation in Stl

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

---------------------
Forward_List Implementation in Stl

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

---------------------
Forward_List Implementation in Stl

---------------------
1.Insert Element at the Front
2.Delete Element at the Front
3.Front Element of Forward List
4.Resize Forward List
5.Remove Elements with Specific Values
6.Remove Duplicate Values
7.Reverse the order of elements
8.Sort Forward List
9.Merge Sorted Lists
10.Display Fo10rward List
11.Exit
Enter your Choice: 5
Enter element to be deleted: 6
---------------------
Forward_List Implementation in Stl

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

---------------------
Forward_List Implementation in Stl

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

------------------
(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...