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…


This content originally appeared on DEV Community and was authored by Mujahida Joynab

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;
}

}`

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;
    }
}`

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;
    }
}

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;
}

}

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;
    }
}

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;
    }
}

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;
    }
}


This content originally appeared on DEV Community and was authored by Mujahida Joynab


Print Share Comment Cite Upload Translate Updates
APA

Mujahida Joynab | Sciencx (2025-01-31T09:20:56+00:00) List Modifier Functions. Retrieved from https://www.scien.cx/2025/01/31/list-modifier-functions/

MLA
" » List Modifier Functions." Mujahida Joynab | Sciencx - Friday January 31, 2025, https://www.scien.cx/2025/01/31/list-modifier-functions/
HARVARD
Mujahida Joynab | Sciencx Friday January 31, 2025 » List Modifier Functions., viewed ,<https://www.scien.cx/2025/01/31/list-modifier-functions/>
VANCOUVER
Mujahida Joynab | Sciencx - » List Modifier Functions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/31/list-modifier-functions/
CHICAGO
" » List Modifier Functions." Mujahida Joynab | Sciencx - Accessed . https://www.scien.cx/2025/01/31/list-modifier-functions/
IEEE
" » List Modifier Functions." Mujahida Joynab | Sciencx [Online]. Available: https://www.scien.cx/2025/01/31/list-modifier-functions/. [Accessed: ]
rf:citation
» List Modifier Functions | Mujahida Joynab | Sciencx | https://www.scien.cx/2025/01/31/list-modifier-functions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.