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.
/*
* C++ Program to Implement List in Stl
*/
#include <iostream>
#include <list>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
int myints[] = {5, 6, 3, 2, 7};
list<int> l, l1 (myints, myints + sizeof(myints) / sizeof(int));
list<int>::iterator it;
int choice, item;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"List Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element at the Front"<<endl;
cout<<"2.Insert Element at the End"<<endl;
cout<<"3.Delete Element at the Front"<<endl;
cout<<"4.Delete Element at the End"<<endl;
cout<<"5.Front Element of List"<<endl;
cout<<"6.Last Element of the List"<<endl;
cout<<"7.Size of the List"<<endl;
cout<<"8.Resize List"<<endl;
cout<<"9.Remove Elements with Specific Values"<<endl;
cout<<"10.Remove Duplicate Values"<<endl;
cout<<"11.Reverse the order of elements"<<endl;
cout<<"12.Sort Forward List"<<endl;
cout<<"13.Merge Sorted Lists"<<endl;
cout<<"14.Display Forward List"<<endl;
cout<<"15.Exit"<<endl;
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 "<<item<<" deleted"<<endl;
break;
case 4:
item = l.back();
l.pop_back();
cout<<"Element "<<item<<" deleted"<<endl;
break;
case 5:
cout<<"Front Element of the List: ";
cout<<l.front()<<endl;
break;
case 6:
cout<<"Last Element of the List: ";
cout<<l.back()<<endl;
break;
case 7:
cout<<"Size of the List: "<<l.size()<<endl;
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"<<endl;
break;
case 11:
l.reverse();
cout<<"List reversed"<<endl;
break;
case 12:
l.sort();
cout<<"List Sorted"<<endl;
break;
case 13:
l1.sort();
l.sort();
l.merge(l1);
cout<<"Lists Merged after sorting"<<endl;
break;
case 14:
cout<<"Elements of the List: ";
for (it = l.begin(); it != l.end(); it++)
cout<<*it<<" ";
cout<<endl;
break;
case 15:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
$ 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.
Related Posts:
- Practice Computer Science MCQs
- Check Programming Books
- Check Data Structure Books
- Apply for Computer Science Internship
- Practice Design & Analysis of Algorithms MCQ