1//code by Soumyadeep Ghosh
2//ig: @soumyadepp
3
4#include <bits/stdc++.h>
5
6using namespace std;
7
8void display_list(list<int>li)
9{
10 //auto variable to iterate through the list
11 for(auto i:li)
12 {
13 cout<<i<<" ";
14 }
15}
16int main()
17{
18 //definition
19 list<int>list_1;
20 int n,x;
21 cin>>n;
22 //taking input and inserting using insert function
23 for(int i=0;i<n;i++)
24 {
25 cin>>x;
26 list_1.insert(x);
27 }
28 //if list is not empty display it
29 if(list_1.empty()==false)
30 {
31 display_list(list_1);
32 }
33 list_1.sort(); //sorts the list
34 list_1.reverse(); //reverses the list
35 list_1.pop_back(); //deletes last element of the list
36 list_1.pop_front(); //deletes the first element of the list
37
38 display_list(list_1); //function to display the list
39
40
41 return 0;
42}
43//in addition , you can use nested lists such as list<list<int>> or list<vector<list>> etc
1
2#include <bits/stdc++.h>
3#include <iostream>
4#include <list>
5#include <iterator>
6
7#define ll long long
8
9using namespace std;
10
11//function to print all the elements of the linked list
12void showList(list <int> l){
13 list <int> :: iterator it; //create an iterator according to the data structure
14 for(it = l.begin(); it != l.end(); it++){
15 cout<<*it<<" ";
16 }
17
18}
19
20
21int main(){
22
23 list <int> l1;
24 list <int> l2;
25
26 for(int i=0; i<10; i++){
27 l1.push_back(i*2); //fill list 1 with multiples of 2
28 l2.push_back(i*3); //fill list 2 with multiples of 3
29 }
30
31 cout<<"content of list 1 is "<<endl;
32 showList(l1);
33 cout<<endl;
34
35 cout<<"content of list 2 is "<<endl;
36 showList(l2);
37 cout<<endl;
38
39 //reverse the first list
40 l1.reverse();
41 showList(l1);
42 cout<<endl;
43
44 //sort the first list
45 l1.sort();
46 showList(l1);
47 cout<<endl;
48
49 //removing an element from both sides
50 l2.pop_front();
51 l2.pop_back();
52
53 //adding an element from both sides
54 l2.push_back(10);
55 l2.push_front(20);
56
57
58 return 0;
59}
1#include <bits/stdc++.h>
2using namespace std;
3void display(list<int> &lst){
4 list<int> :: iterator it;
5 for(it = lst.begin(); it != lst.end(); it++){
6 cout<<*it<<" ";
7 }
8}
9int main(){
10 list<int> list1;
11 int data, size;
12 cout<<"Enter the list Size ";
13 cin>>size;
14 for(int i = 0; i<size; i++){
15 cout<<"Enter the element of the list ";
16 cin>>data;
17 list1.push_back(data);
18 }
19 cout<<endl;
20 display(list1);
21 return 0;
22}
23
24