C++ STL List | Practice | GeeksforGeeks

Implement different operation on List A i.e. adding an element in front and end, removing an element from the front and end, sorting elements, reversing the list and printing the list.
Input:
The first line of input contains an integer T denoting the no of test cases.For each test case, the first line of input contains an integer Q denoting the no of queries. Then in the next line are Q space separated queries.
A query can be of eight types 
1 x (Adds an element x to the list A at the end and print list A)
2 (Sorts the list A in ascending order and print list A )
3 (Reverses the list A and print list A)
4 (Prints the size of the list A)
5 (Prints space-separated values of the list)
6 (Remove an element from the back of the list and print list A )
7(Remove an element from the front of the list and print list A)
8 x (Adds element x in front of the list and print list A)
Output:
The output for each test case will be according to the query that is performed and if the list is empty output is -1. 
Constraints:
1<=T<=100
1<=Q<=100
Example:
Input:
1
8
1 5
8 1
3
4
5
6
1 6
7
Output:
5
1 5
5 1 
2
5 1
5
5 6
6
0
sagargupta25265 months ago
Can i use cbegin
+1
Purushottam Bca8 months ago
Purushottam Bca
void print(list<int> &A){    if(A.empty()) cout<<"-1";    else     {        for(auto i=A.begin();i!=A.end();i++)             cout<<*i<<" ";    }    cout<<"\n";}void remove_from_back(list<int> &A){if(A.empty())     cout<<"-1";else     A.pop_back();}void remove_from_front(list<int> &A){if(A.empty())     cout<<"-1";else     A.pop_front();}void add_to_list(list<int> &A,int x){    A.push_back(x);}void sort_list(list<int> &A){    if(A.empty())         cout<<"-1";    else         A.sort();}void reverse_list(list<int> &A){if(A.empty())     cout<<"-1";else     A.reverse();}int size_of_list(list<int> &A){    return A.size();}void add_from_front(list<int> &A,int x){    A.push_front(x);}
0
Purushottam Bca8 months ago
Purushottam Bca
NOTE -> READ OUTPUT FORMAT CAREFULLY
0
Sukesh Seth1 year ago
Sukesh Seth
/*User function Template for C++You are required to complete below methods*//*prints space separated elements of list A*/void print(list<int> &A){    if(A.empty()){        cout << -1 << "\n";        return;    }    for(auto x: A)        cout << x << " ";    cout << "\n";}/*remove element from back of list A*/void remove_from_back(list<int> &A){   A.pop_back();   return;}/*remove element from front of list A*/void remove_from_front(list<int> &A){   A.pop_front();   return;}/*inserts an element x at the back of the list A */void add_to_list(list<int> &A,int x){    A.emplace_back(x);    return;}/*sort the list A in ascending order*/void sort_list(list<int> &A){    A.sort();    return;}/*reverses the list A*/void reverse_list(list<int> &A){    A.reverse();    return;}/*returns the size of the list  A */int size_of_list(list<int> &A){    return A.size();}/*inserts an element x at the front of the list A*/void add_from_front(list<int> &A,int x){    A.emplace_front(x);    return;}
0
GauriK1 year ago
GauriK
void print(list<int> &A)why we use &A???
0
Amar Prakash1 year ago
Amar Prakash
void print(list<int> &A){    if(A.empty())    {        cout<<"-1";    }    else    {        for(auto x: A)        {            cout<<x<<" ";="" }="" }="" cout<<endl;="" your="" code="" here="" }="" *remove="" element="" from="" back="" of="" list="" a*="" void="" remove_from_back(list<int=""> &A){    if(A.empty())    {        cout<<"-1";    }    else    {      A.pop_back();
    }
   //Your code here}/*remove element from front of list A*/void remove_from_front(list<int> &A){    if(A.empty())    {        cout<<"-1";    }    else    {       A.pop_front();    }   //Your code here}
/*inserts an element x at the back of the list A */void add_to_list(list<int> &A,int x){    A.push_back(x);//Your code here}
/*sort the list A in ascending order*/void sort_list(list<int> &A){    if(A.empty())    {        cout<<"-1";    }    else    {        A.sort();    }//Your code here}
/*reverses the list A*/void reverse_list(list<int> &A){    if(A.empty())    {        cout<<"-1";    }    else    {      A.reverse();    }//Your code here}
/*returns the size of the list  A */int size_of_list(list<int> &A){    return A.size();    cout<<endl; your="" code="" here="" }="" *inserts="" an="" element="" x="" at="" the="" front="" of="" the="" list="" a*="" void="" add_from_front(list<int=""> &A,int x){     A.push_front(x);//Your code here}
0
Yash Parmar2 years ago
Yash Parmar
void print(list<int> &A){list<int> :: iterator i;if(A.empty()) cout<<"-1";else {for(i=A.begin();i!=A.end();i++) cout<<*i<<" ";}cout<<"\n";}/*remove element from back of list A*/void remove_from_back(list<int> &A){   if(A.empty()) cout<<"-1";   else A.pop_back();}/*remove element from front of list A*/void remove_from_front(list<int> &A){   if(A.empty()) cout<<"-1";   else A.pop_front();}/*inserts an element x at the back of the list A */void add_to_list(list<int> &A,int x){A.push_back(x);}/*sort the list A in ascending order*/void sort_list(list<int> &A){if(A.empty()) cout<<"-1";else A.sort();}/*reverses the list A*/void reverse_list(list<int> &A){if(A.empty()) cout<<"-1";else A.reverse();}/*returns the size of the list  A */int size_of_list(list<int> &A){     return A.size();
}/*inserts an element x at the front of the list A*/void add_from_front(list<int> &A,int x){A.push_front(x);}
0
Anurag Parothia2 years ago
Anurag Parothia
Someone please helpWrong Answer. !!!
Possibly your code doesn't work correctly for multiple test-cases (TCs).
The first test case where your code failed:
Input:208 568321 951732721 193
void print(list<int> &A){//Your code herefor (auto it = A.begin(); it != A.end(); ++it) {    cout<<*it<<" ";}cout<<endl; }="" *remove="" element="" from="" back="" of="" list="" a*="" void="" remove_from_back(list<int=""> &A){   //Your code here   A.pop_back();}/*remove element from front of list A*/void remove_from_front(list<int> &A){   //Your code here   if(A.empty()) cout<<"-1";   A.pop_front();}/*inserts an element x at the back of the list A */void add_to_list(list<int> &A,int x){//Your code hereA.push_back(x);}/*sort the list A in ascending order*/void sort_list(list<int> &A){//Your code hereA.sort();}/*reverses the list A*/void reverse_list(list<int> &A){//Your code hereA.reverse();}/*returns the size of the list  A */int size_of_list(list<int> &A){//Your code herereturn A.size();}/*inserts an element x at the front of the list A*/void add_from_front(list<int> &A,int x){//Your code hereA.push_front(x);}
0
Deepak Yadav4 years ago
Deepak Yadav
I case u stuck see this  https://ide.geeksforgeeks.o...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.