Tan Elaine
0
Q:

list in cpp

#include <iostream> 
#include <list> 
#include <iterator> 
using namespace std; 
  
//function for printing the elements in a list 
void showlist(list <int> g) 
{ 
    list <int> :: iterator it; 
    for(it = g.begin(); it != g.end(); ++it) 
        cout << '\t' << *it; 
    cout << '\n'; 
} 
  
int main() 
{ 
  
    list <int> gqlist1, gqlist2; 
  
  
    for (int i = 0; i < 10; ++i) 
    { 
        gqlist1.push_back(i * 2); 
        gqlist2.push_front(i * 3); 
    } 
    cout << "\nList 1 (gqlist1) is : "; 
    showlist(gqlist1); 
  
    cout << "\nList 2 (gqlist2) is : "; 
    showlist(gqlist2); 
  
    cout << "\ngqlist1.front() : " << gqlist1.front(); 
    cout << "\ngqlist1.back() : " << gqlist1.back(); 
  
    cout << "\ngqlist1.pop_front() : "; 
    gqlist1.pop_front(); 
    showlist(gqlist1); 
  
    cout << "\ngqlist2.pop_back() : "; 
    gqlist2.pop_back(); 
    showlist(gqlist2); 
  
    cout << "\ngqlist1.reverse() : "; 
    gqlist1.reverse(); 
    showlist(gqlist1); 
  
    cout << "\ngqlist2.sort(): "; 
    gqlist2.sort(); 
    showlist(gqlist2); 
  
    return 0; 
  
} 
2
//code by Soumyadeep Ghosh
//ig: @soumyadepp

#include <bits/stdc++.h>

using namespace std;

void display_list(list<int>li)
{
  //auto variable to iterate through the list
  for(auto i:li)
  {
    cout<<i<<" ";
  }
}
int main()
{
  //definition
  list<int>list_1;
  int n,x;
  cin>>n;
  //taking input and inserting using insert function
  for(int i=0;i<n;i++)
  {
    cin>>x;
    list_1.insert(x);
  }
  //if list is not empty display it
  if(list_1.empty()==false)
  {
    display_list(list_1);
  }
  list_1.sort(); //sorts the list
  list_1.reverse(); //reverses the list
  list_1.pop_back(); //deletes last element of the list
  list_1.pop_front(); //deletes the first element of the list
  
  display_list(list_1);  //function to display the list
  
  
  return 0;
}
//in addition , you can use nested lists such as list<list<int>> or list<vector<list>> etc
0
template < class T, class Alloc = allocator<T> > class list;
0

New to Communities?

Join the community