-
Notifications
You must be signed in to change notification settings - Fork 491
/
Lists-Operations.cpp
98 lines (58 loc) Β· 1.65 KB
/
Lists-Operations.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
95
96
97
98
#include<list>
#include<iostream>
using namespace std;
void print(list<string> l2){
//Iterate over the list and print
for(string s:l2)
cout<<s<<",";
cout<<endl;
}
int main(){
//List is a Doubly Linked List it is advantageous in comparison to vector when it comes to insertion and deletion
//O(1) insert and delete last/first and insert middle is also slower than vector
list<int> l;
list<int> l1{1,2,6,9,12};
list<string> l2{"Apple","Banana","Mango"};
cout<<"Pineapple Pushed "<<endl;
l2.push_back("Pineapple");
print(l2);
/*//WE can sort the list */
l2.sort();
cout<<"Sorted : ";print(l2);
//We can reverse
l2.reverse();
cout<<"REverse : ";print(l2);
cout<<"Front Element "<<l2.front()<<endl;
l2.pop_front();
cout<<"Removed Pineapple "<<endl;
/*//Add Front */
cout<<"Added Straw "<<endl;
l2.push_front("Strawberry");
print(l2);
/* // Pop back */
cout<<"Pop Last : "<<l2.back()<<endl;
l2.pop_back();
l2.push_back("Apple");
string f;
cout<<"Enter element to remove : ";
cin>>f;
//Remove the element name list all occurence
l2.remove(f);
print(l2);
/* ----Erase middle elements one or more -----*/
//We cant just simply add numbers to l2.begin here because in vector it was linear array
//but here its list not linear memory allocation
cout<<"Removing element at index 2"<<endl;
auto it =l2.begin();
it++;
it++;
l2.erase(it);
print(l2);
/* ----Insert middle elements one or more -----*/
cout<<"Adding element at index 1"<<endl;
auto itr =l2.begin();
itr++;
l2.insert(itr,"Watermelon");
print(l2);
return 0;
}