Wednesday, 5 October 2016

C++ program to implement List using STL


/*
Problem Statement: C++ program to implement List using STL
Author: Pavan R Jaiswal

Options: 
  1: Add Element At Front
  2: Add Element At Back
  3: Delete Element From Front
  4: Delete Element From Back
  5: Display List
*/

#include "iostream"
#include "list"

using namespace std;

class ListSTL{
 public:
  list<int> addElementAtFront(list<int>);
  list<int> addElementAtBack(list<int>);
  list<int> deleteElementFromFront(list<int>);
  list<int> deleteElementFromBack(list<int>);
  void displayList(list<int>);
};

list<int> ListSTL :: addElementAtFront(list<int> lt){
 
 int element;
 cout<<"\n Enter element: ";
 cin >> element;
 lt.push_front(element);
 cout<<"\nElement added at front\n";
 return lt;
}

list<int> ListSTL :: addElementAtBack(list<int> lt){
 
 int element;
 cout<<"\n Enter element: ";
 cin >> element;
 lt.push_back(element);
 cout<<"\nElement added at back\n";
 return lt;
}


list<int> ListSTL :: deleteElementFromFront(list<int> lt){
 int element;
 if(lt.empty()){
  cout<<"\nSorry. List is empty";
 }else{
  element = lt.front(); 
  lt.pop_front();
  cout<<"\nElement "<< element<<" deleted from front\n";
 }
 return lt;
}

list<int> ListSTL :: deleteElementFromBack(list<int> lt){
 int element;
 if(lt.empty()){
  cout<<"\nSorry. List is empty";
 }else{
  element = lt.back(); 
  lt.pop_back();
  cout<<"\nElement "<< element<<" deleted from back\n";
 }
 return lt;
}

void ListSTL :: displayList(list<int> lt){
 list<int> :: iterator it;
 if(lt.empty()){
  cout<<"\nSorry. List is empty ";
 }else{
  cout<<"\nDisplaying list: \n";
  for(it= lt.begin(); it != lt.end(); it++ ){
   cout<<"\t"<<*it;
  }
 }
}

int main(){
 ListSTL ob;
 int choice;
 char che;
 list<int> lt;
 do{
  cout<<"\n\t\t List Implementation Using STL\n";
  cout<<"\n1: Add Element At Front";
  cout<<"\n2: Add Element At Back";
  cout<<"\n3: Delete Element From Front";
  cout<<"\n4: Delete Element From Back";
  cout<<"\n5: Display List";
  cout<<"\n6: Exit";
  cout<<"\n Enter your choice: ";
  cin>>choice;
  switch(choice){
   case 1:
    lt = ob.addElementAtFront(lt);
   break;

   case 2:
    lt = ob.addElementAtBack(lt);
   break;

   case 3:
    lt = ob.deleteElementFromFront(lt);
   break;

   case 4:
    lt = ob.deleteElementFromBack(lt);
   break;

   case 5:
    ob.displayList(lt);
   break;
   
   default:
    cout<<"\nInvalid choice. Try again";
   break; 
  
  }
  cout<<"\nDo you wish to continue [y/n]: ";
  cin>>che;
 }while(che == 'y' || che =='Y');
 return 0;
}

/*
 ####### OUTPUT ######
[pavan@localhost Programs]$ g++ ListSTL.cpp -o ListOp
[pavan@localhost Programs]$ ./ListOp 

   List Implementation Using STL

1: Add Element At Front
2: Add Element At Back
3: Delete Element From Front
4: Delete Element From Back
5: Display List
6: Exit
 Enter your choice: 1

 Enter element: 10

Element added at front

Do you wish to continue [y/n]: y

   List Implementation Using STL

1: Add Element At Front
2: Add Element At Back
3: Delete Element From Front
4: Delete Element From Back
5: Display List
6: Exit
 Enter your choice: 2

 Enter element: 20

Element added at back

Do you wish to continue [y/n]: y

   List Implementation Using STL

1: Add Element At Front
2: Add Element At Back
3: Delete Element From Front
4: Delete Element From Back
5: Display List
6: Exit
 Enter your choice: 2

 Enter element: 5

Element added at back

Do you wish to continue [y/n]: n
[pavan@localhost Programs]$ 
[pavan@localhost Programs]$ g++ ListSTL.cpp -o ListOp
[pavan@localhost Programs]$ ./ListOp 

   List Implementation Using STL

1: Add Element At Front
2: Add Element At Back
3: Delete Element From Front
4: Delete Element From Back
5: Display List
6: Exit
 Enter your choice: 1

 Enter element: 10

Element added at front

Do you wish to continue [y/n]: y

   List Implementation Using STL

1: Add Element At Front
2: Add Element At Back
3: Delete Element From Front
4: Delete Element From Back
5: Display List
6: Exit
 Enter your choice: 2

 Enter element: 20

Element added at back

Do you wish to continue [y/n]: y

   List Implementation Using STL

1: Add Element At Front
2: Add Element At Back
3: Delete Element From Front
4: Delete Element From Back
5: Display List
6: Exit
 Enter your choice: 2

 Enter element: 30

Element added at back

Do you wish to continue [y/n]: y

   List Implementation Using STL

1: Add Element At Front
2: Add Element At Back
3: Delete Element From Front
4: Delete Element From Back
5: Display List
6: Exit
 Enter your choice: 5

Displaying list: 
 10 20 30
Do you wish to continue [y/n]: y

   List Implementation Using STL

1: Add Element At Front
2: Add Element At Back
3: Delete Element From Front
4: Delete Element From Back
5: Display List
6: Exit
 Enter your choice: 3

Element 10 deleted from front

Do you wish to continue [y/n]: y

   List Implementation Using STL

1: Add Element At Front
2: Add Element At Back
3: Delete Element From Front
4: Delete Element From Back
5: Display List
6: Exit
 Enter your choice: 4

Element 30 deleted from back

Do you wish to continue [y/n]: y

   List Implementation Using STL

1: Add Element At Front
2: Add Element At Back
3: Delete Element From Front
4: Delete Element From Back
5: Display List
6: Exit
 Enter your choice: 5

Displaying list: 
 20
Do you wish to continue [y/n]: n
[pavan@localhost Programs]$ 

*/

No comments: