What is Vector?
- Library of a dynamic array What is List?
- Library of a doubly Linked List
How to declare list -
listmylist;
`#include
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
list l;
return 0;
}
#include
Initializing Time Complexity - O(1) .
Now it is NULL .
**2. list<type>myList(N)**
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
list l;
cout << l.size() << endl;
return 0;
}`
Output - 0
How to initialize a array of list ?
`#include
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
list l(10);
cout << l.size() << endl;
return 0;
}
#include
How to set value?
**list<typ>myList(N,P)**
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
list l(10, 3);
cout << *l.begin() << endl; // Iterator as it is doubly linked list . Can't access it like vector l[0]
return 0;
}
#include
To print all values -
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
list l(10, 3);
for (auto it = l.begin(); it != l.end(); it++) // auto keyword because it is doubly linked list . There is no index in doubly linked list .
{
cout << *it << endl;
}
return 0;
}`
Can you visualize differences between list and vector? If not learn doubly linked list to visualize list.
Output -
3
3
3
3
3
3
3
3
3
3
Printing with range based for loop -
`#include
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
list l(10, 5);
for(int val : l){
cout << val << endl;
}
}`
It is easy to write .
Copying one string to another
listmyList(list2)
`#include
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
list l = {1, 2, 3, 4, 5};
list l2(l);
for (int val : l2)
{
cout << val << endl;
}
return 0;
}`
Output -
1
2
3
4
5
We can use here array beside list for copying .
How to use?
listmyList(A,A+N)
`#include
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
list l = {1, 2, 3, 4, 5};
int a[] = {1, 2, 3};
list<int> l2(a, a + 3);
for (int val : l2)
{
cout << val << endl;
}
return 0;
}`
We can use vector as well -
How to use ?
listmyList(v.begin(),v.end())
`#include
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vector v = {10, 20, 30};
list l2(v.begin(), v.end());
for (int val : l2)
{
cout << val << endl;
}
return 0;`
}
All functions of constructors -
Constructor
Summary of Constructor
listmylist - Construct a list with 0 elements . TC - O(1)
listmyList(N) - Construct a list with N elements and the value will be garbage - TC - O(N)
listmyList(N,P) - Construct a list with N elements and the value will be P . TC - O(N)
listmyList(list2) - Construct a list by copying another list list2 . TC - O(N)
listmyList(A,A+N) - Construct a list by copying all elements from an array A of size N . TC - O(N)
listmyList(v.begin(),v.end()) - Construct a list by copying all elements from a vector v . TC - O(N)
Top comments (0)