1#include <algorithm>
2#include <iostream>
3#include <list>
4
5int main()
6{
7 // Create a list containing integers
8 std::list<int> l = { 7, 5, 16, 8 };
9
10 // Add an integer to the front of the list
11 l.push_front(25);
12 // Add an integer to the back of the list
13 l.push_back(13);
14
15 // Insert an integer before 16 by searching
16 auto it = std::find(l.begin(), l.end(), 16);
17 if (it != l.end()) {
18 l.insert(it, 42);
19 }
20
21 // Print out the list
22 std::cout << "l = { ";
23 for (int n : l) {
24 std::cout << n << ", ";
25 }
26 std::cout << "};\n";
27}
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#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