You are on page 1of 4

#include<iostream>

#include<list>

#include<iterator>

#include<algorithm>

using namespace std;

int main(){

int arr[]={2,5,3,9,7};

list<int> mylist (arr,arr+5);

list<int>::iterator it;

//********** Print the first element of the list***********

//it=mylist.begin();

//cout<<*it; //2

//*********** Print all elements of the list ****************

/*for(it=mylist.begin();it!=mylist.end();it++){

cout<<*it<<" ";

} */

//*********** Incrementing Point*****************

/*

it=mylist.begin();

it++; // Increment the it,so It will point the next element

cout<<*it<<endl; //5

it++; // Increment again,so it will point next next element

cout<<*it<<endl; //3
*/

//***********Inserting element before any element of list*********** //

/*

it=mylist.begin();

it++; // it will be pointing now to second element 5

mylist.insert(it,7); // Insert 7 before the second element 5

for(it=mylist.begin();it!=mylist.end();it++){

cout<<*it<<" "; // 2 7 5 3 9 7

*/

//***********Inserting element before any particular element of list*********** //

/*

it=find(mylist.begin(),mylist.end(),9); //Searching 9 in the list and IT will be

//pointed to the list.

cout<<*it<<endl; //9

mylist.insert(it,4); // Insert an element before 9

for(it=mylist.begin();it!=mylist.end();it++){

cout<<*it<<" "; // 2 5 3 4 9 7 //4 is inserted before 9

*/

//*****************Checking element is in the list or not*******************

/*
it=find(mylist.begin(),mylist.end(),22); //There is no element 22 in the list so it will be

//pointed to the end of the list.

if(it==mylist.end()){

cout<<"Not Found.";

}else{

cout<<"Found.";

*/

/*************************** Erase element*********************************/

/*

it=mylist.begin();

mylist.erase(it); // Erase the first element from the beginning

for(it=mylist.begin();it!=mylist.end();it++){

cout<<*it<<" "; 5 3 9 7

*/

/*

it=find(mylist.begin(),mylist.end(),3); // it* will point tho the 3

mylist.erase(it); // 3 will be erased from the list

for(it=mylist.begin();it!=mylist.end();it++){

cout<<*it<<" "; 2 5 9 7

*/

/**************Check list is empty or Not*******************/


/*

if(mylist.empty()){

cout<<"Empty";

}else{

cout<<"Filled";

*/

/********************More Functions************************/

/*

cout<<mylist.front()<<endl; //2

cout<<mylist.back()<<endl; //7

mylist.pop_front(); // Pop any element from the front

mylist.pop_back(); // Pop any element from the last

mylist.push_back(); // Push any elemnet at the lat

mylist.push_front(); // Push any element at the front

*/

You might also like