C++ Program to Implement Forward_List in STL

«
»
This C++ Program demonstrates implementation of Forward_List in STL.

Here is source code of the C++ Program to demonstrate Forward_List in STL. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Forward_List in Stl
  3.  */
  4. #include <iostream>
  5. #include <forward_list>
  6. #include <string>
  7. #include <cstdlib>
  8. using namespace std;
  9. int main()
  10. {
  11.     forward_list<int> fl, fl1 = {5, 6, 3, 2, 7};
  12.     forward_list<int>::iterator it;
  13.     int choice, item;
  14.     while (1)
  15.     {
  16.         cout<<"\n---------------------"<<endl;
  17.         cout<<"Forward_List Implementation in Stl"<<endl;
  18.         cout<<"\n---------------------"<<endl;
  19.         cout<<"1.Insert Element at the Front"<<endl;
  20.         cout<<"2.Delete Element at the Front"<<endl;
  21.         cout<<"3.Front Element of Forward List"<<endl;
  22.         cout<<"4.Resize Forward List"<<endl;
  23.         cout<<"5.Remove Elements with Specific Values"<<endl;
  24.         cout<<"6.Remove Duplicate Values"<<endl;
  25.         cout<<"7.Reverse the order of elements"<<endl;
  26.         cout<<"8.Sort Forward List"<<endl;
  27.         cout<<"9.Merge Sorted Lists"<<endl;
  28.         cout<<"10.Display Forward List"<<endl;
  29.         cout<<"11.Exit"<<endl;
  30.         cout<<"Enter your Choice: ";
  31.         cin>>choice;
  32.         switch(choice)
  33.         {
  34.         case 1:
  35.             cout<<"Enter value to be inserted at the front: ";
  36.             cin>>item;
  37.             fl.push_front(item);
  38.             break;
  39.         case 2:
  40.             item = fl.front();
  41.             fl.pop_front();
  42.             cout<<"Element "<<item<<" deleted"<<endl;
  43.             break;
  44.         case 3:
  45.             cout<<"Front Element of the Forward List: ";
  46.             cout<<fl.front()<<endl;
  47.             break;
  48.         case 4:
  49.             cout<<"Enter new size of Forward List: ";
  50.             cin>>item;
  51.             if (item <= fl.max_size())
  52.                 fl.resize(item);
  53.             else
  54.                 fl.resize(item, 0);
  55.             break;
  56.         case 5:
  57.             cout<<"Enter element to be deleted: ";
  58.             cin>>item;
  59.             fl.remove(item);
  60.             break;
  61.         case 6:
  62.             fl.unique();
  63.             cout<<"Duplicate Items Deleted"<<endl;
  64.             break;
  65.         case 7:
  66.             fl.reverse();
  67.             cout<<"Forward List reversed"<<endl;
  68.             break;
  69.         case 8:
  70.             fl.sort();
  71.             cout<<"Forward List Sorted"<<endl;
  72.             break;
  73.         case 9:
  74.             fl1.sort();
  75.             fl.sort();
  76.             fl.merge(fl1);
  77.             cout<<"Forward List Merged after sorting"<<endl;
  78.             break;
  79.         case 10:
  80.             cout<<"Elements of Forward List:  ";
  81.             for (it = fl.begin(); it != fl.end(); it++)
  82.                 cout<<*it<<"  ";
  83.             cout<<endl;
  84.             break;
  85.         case 11:
  86.             exit(1);
  87.         break;
  88.         default:
  89.             cout<<"Wrong Choice"<<endl;
  90.         }
  91.     }
  92.     return 0;
  93. }

advertisement
$ g++ forward_list.cpp
$ a.out
 
---------------------
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

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn