DEV Community

Mujahida Joynab
Mujahida Joynab

Posted on

List Modifier Functions

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

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay