-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathSTL_Linkedlist.cpp
94 lines (92 loc) · 2.59 KB
/
STL_Linkedlist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream> //Header File
#include <bits/stdc++.h> // standard template library
using namespace std;
int main()
{
// Start of the main function
list<int> li;
// Insertion (at end)
li.push_back(10);
li.push_back(15);
li.push_back(7);
for (auto it = li.begin(); it != li.end(); it++)
{ // Print from left to right
cout << *it << " ";
}
cout << endl;
// Insertion (at middle)
auto it1 = li.begin();
advance(it1, 1);
li.insert(it1, 20);
for (auto it = li.begin(); it != li.end(); it++)
{ // Print from left to right
cout << *it << " ";
}
cout << endl;
// Deletion (in middle)
auto j = li.begin();
advance(j, 2);
li.erase(j);
for (auto it = li.begin(); it != li.end(); it++)
{ // Print from left to right
cout << *it << " ";
}
cout << endl;
li.push_back(20);
li.push_back(2);
li.push_back(35);
li.push_back(18);
li.push_back(2);
li.push_back(2);
for (auto it = li.begin(); it != li.end(); it++)
{ // Print from left to right
cout << *it << " ";
}
cout << endl;
// Sort (in ascending order)
li.sort();
for (auto it = li.begin(); it != li.end(); it++)
{ // Print from left to right
cout << *it << " ";
}
cout << endl;
// Reverse
li.reverse();
for (auto it = li.begin(); it != li.end(); it++)
{ // Print from left to right
cout << *it << " ";
}
cout << endl;
// Searching
int element = 100;
auto it2 = find(li.begin(), li.end(), element);
if (it2 != li.end())
{
cout << element << " is found" << endl;
}
else
{
cout << element << " is not found" << endl;
}
element = 10;
auto it3 = find(li.begin(), li.end(), element);
if (it3 != li.end())
{
cout << element << " is found" << endl;
}
else
{
cout << element << " is not found" << endl;
}
// Counting
element = 2;
int n = count(li.begin(), li.end(), element);
cout << element << " occurs " << n << " times." << endl;
// Minimum Element
int min_value = *min_element(li.begin(), li.end());
cout << "Minimum Value is " << min_value << endl;
// Maximum Element
int max_value = *max_element(li.begin(), li.end());
cout << "Maximum Value is " << max_value << endl;
}
// Linked List : STL (Standard Template Library) is a library of container classes, algorithms, and iterators. It is a generalized library and so, its components are parameterized. A working knowledge of template classes is a prerequisite for working with STL.