C++ Program to Implement List in STL

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

Here is source code of the C++ Program to demonstrate 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 List in Stl
  3.  */
  4. #include <iostream>
  5. #include <list>
  6. #include <string>
  7. #include <cstdlib>
  8. using namespace std;
  9. int main()
  10. {
  11.     int myints[] = {5, 6, 3, 2, 7};
  12.     list<int> l, l1 (myints, myints + sizeof(myints) / sizeof(int));
  13.     list<int>::iterator it;
  14.     int choice, item;
  15.     while (1)
  16.     {
  17.         cout<<"\n---------------------"<<endl;
  18.         cout<<"List Implementation in Stl"<<endl;
  19.         cout<<"\n---------------------"<<endl;
  20.         cout<<"1.Insert Element at the Front"<<endl;
  21.         cout<<"2.Insert Element at the End"<<endl;
  22.         cout<<"3.Delete Element at the Front"<<endl;
  23.         cout<<"4.Delete Element at the End"<<endl;
  24.         cout<<"5.Front Element of List"<<endl;
  25.         cout<<"6.Last Element of the List"<<endl;
  26.         cout<<"7.Size of the List"<<endl;
  27.         cout<<"8.Resize List"<<endl;
  28.         cout<<"9.Remove Elements with Specific Values"<<endl;
  29.         cout<<"10.Remove Duplicate Values"<<endl;
  30.         cout<<"11.Reverse the order of elements"<<endl;
  31.         cout<<"12.Sort Forward List"<<endl;
  32.         cout<<"13.Merge Sorted Lists"<<endl;
  33.         cout<<"14.Display Forward List"<<endl;
  34.         cout<<"15.Exit"<<endl;
  35.         cout<<"Enter your Choice: ";
  36.         cin>>choice;
  37.         switch(choice)
  38.         {
  39.         case 1:
  40.             cout<<"Enter value to be inserted at the front: ";
  41.             cin>>item;
  42.             l.push_front(item);
  43.             break;
  44.         case 2:
  45.             cout<<"Enter value to be inserted at the end: ";
  46.             cin>>item;
  47.             l.push_back(item);
  48.             break;
  49.         case 3:
  50.             item = l.front();
  51.             l.pop_front();
  52. 	    cout<<"Element "<<item<<" deleted"<<endl;
  53.             break;
  54.         case 4:
  55.             item = l.back();
  56.             l.pop_back();
  57. 	    cout<<"Element "<<item<<" deleted"<<endl;
  58.             break;
  59.         case 5:
  60. 	    cout<<"Front Element of the List: ";
  61. 	    cout<<l.front()<<endl;
  62.             break;
  63.         case 6:
  64.             cout<<"Last Element of the List: ";
  65.             cout<<l.back()<<endl;
  66.             break;
  67.         case 7:
  68.             cout<<"Size of the List: "<<l.size()<<endl;
  69.             break;
  70.         case 8:
  71.             cout<<"Enter new size of the List: ";
  72.             cin>>item;
  73.             if (item <= l.size())
  74.                 l.resize(item);
  75.             else
  76.                 l.resize(item, 0);
  77.             break;
  78.         case 9:
  79.             cout<<"Enter element to be deleted: ";
  80.             cin>>item;
  81.             l.remove(item);
  82.             break;
  83.         case 10:
  84.             l.unique();
  85.             cout<<"Duplicate Items Deleted"<<endl;
  86.             break;
  87.         case 11:
  88.             l.reverse();
  89.             cout<<"List reversed"<<endl;
  90.             break;
  91.         case 12:
  92.             l.sort();
  93.             cout<<"List Sorted"<<endl;
  94.             break;
  95.         case 13:
  96.             l1.sort();
  97.             l.sort();
  98.             l.merge(l1);
  99.             cout<<"Lists Merged after sorting"<<endl;
  100.             break;
  101.         case 14:
  102.             cout<<"Elements of the List:  ";
  103.             for (it = l.begin(); it != l.end(); it++)
  104.                 cout<<*it<<"  ";
  105.             cout<<endl;
  106.             break;
  107. 	case 15:
  108.             exit(1);
  109. 	    break;
  110.         default:
  111.             cout<<"Wrong Choice"<<endl;
  112.         }
  113.     }
  114.     return 0;
  115. }

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

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