List Modifier Functions

Mujahida Joynab - Jan 31 - - Dev Community

Inserting a link list into another linked list -
`#include
using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
list l = {10, 20, 30};
list l2;
l2 = l;

for (int i : l2)
{
    cout << i << endl;
}
Enter fullscreen mode Exit fullscreen mode

}`

Also

`#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    list<int> l = {10, 20, 30};
    list<int> l2;
    l2.assign(l.begin(), l.end());

    for (int i : l2)
    {
        cout << i << endl;
    }
}`
Enter fullscreen mode Exit fullscreen mode

Insert at tail and Insert at head

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    list<int> l = {10, 20, 30};
    l.push_back(40); // TC - O(1)
    l.push_front(100); // TC - O(1)
    for (int i : l)
    {
        cout << i << endl;
    }
}
Enter fullscreen mode Exit fullscreen mode

Delete at tail and Delete at head

include

using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
list l = {10, 20, 30};
l.push_back(40); // TC - O(1)
l.push_front(100); // TC - O(1)

l.pop_back(); 
l.pop_back(); 
l.pop_front() ;

for (int i : l)
{
    cout << i << endl;
}
Enter fullscreen mode Exit fullscreen mode

}

Access any position -

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    list<int> l = {10, 20, 30};
    l.push_back(40);   // TC - O(1)
    l.push_front(100); // TC - O(1)

    cout << *next(l.begin(), 3) << endl;
    for (int i : l)
    {
        cout << i << endl;
    }
}
Enter fullscreen mode Exit fullscreen mode

Insert at any position

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    list<int> l = {10, 20, 30};
    l.push_back(40);   // TC - O(1)
    l.push_front(100); // TC - O(1)

    l.insert(next(l.begin(), 3), 1000); // TC - O(N) 
    for (int i : l)
    {
        cout << i << endl;
    }
}
Enter fullscreen mode Exit fullscreen mode

Insert a list into another list

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    list<int> l = {10, 20, 30};
    list<int> l2 = {11, 12, 13};
    l.push_back(40);   // TC - O(1)
    l.push_front(100); // TC - O(1)

    l.insert(next(l.begin(), 2), l2.begin(), l2.end());
    for (int i : l)
    {
        cout << i << endl;
    }
}
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .