0% found this document useful (0 votes)
4 views

list[1].c++

The document provides a comprehensive guide on using C++ lists, covering initialization, insertion, deletion, accessing elements, size and capacity, sorting, merging, splicing, and handling multidimensional lists. It includes code examples demonstrating each operation and outputs the results of various list manipulations. The program concludes with the successful removal of even numbers from a list.

Uploaded by

Asif Hafeez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

list[1].c++

The document provides a comprehensive guide on using C++ lists, covering initialization, insertion, deletion, accessing elements, size and capacity, sorting, merging, splicing, and handling multidimensional lists. It includes code examples demonstrating each operation and outputs the results of various list manipulations. The program concludes with the successful removal of even numbers from a list.

Uploaded by

Asif Hafeez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

CODE

#include <iostream>
#include <list>
#include <algorithm> // For sort

using namespace std;

// Function to display the contents of a list using an iterator


void displayList(const list<int> &lst)
{
for (auto it = lst.begin(); it != lst.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
}

// Function to display the contents of a multidimensional list using an iterator


void displayMultiList(const list<list<int>> &multiList)
{
for (auto rowIt = multiList.begin(); rowIt != multiList.end(); ++rowIt)
{
for (auto colIt = rowIt->begin(); colIt != rowIt->end(); ++colIt)
{
cout << *colIt << " ";
}
cout << endl;
}
}

int main()
{
// 1. Declaration and Initialization
cout << "1. Declaration and Initialization" << endl;

// Type 1: Initialization using an initializer list


list<int> numbers1 = {1, 2, 3, 4, 5};
cout << "List initialized using an initializer list: ";
displayList(numbers1);

// Type 2: Initialization with a specific size and value


list<int> numbers2(5, 10); // 5 elements, each initialized to 10
cout << "List initialized with specific size and value: ";
displayList(numbers2);
cout << endl;

// 2. Insertion
cout << "2. Insertion" << endl;

// Inserting at the beginning


numbers1.push_front(0);
cout << "Inserted 0 at the beginning: ";
displayList(numbers1);

// Inserting at the end


numbers1.push_back(6);
cout << "Inserted 6 at the end: ";
displayList(numbers1);
// Inserting at a specific position using iterator
auto insertPosition = numbers1.begin();
advance(insertPosition, 3); // Move iterator to the 3rd position
numbers1.insert(insertPosition, 42);
cout << "Inserted 42 at the 3rd position: ";
displayList(numbers1);
cout << endl;

// 3. Deletion
cout << "3. Deletion" << endl;

// Deleting the first element


numbers1.pop_front();
cout << "Deleted the first element: ";
displayList(numbers1);

// Deleting the last element


numbers1.pop_back();
cout << "Deleted the last element: ";
displayList(numbers1);

// Deleting an element at a specific position


auto deletePosition = numbers1.begin();
advance(deletePosition, 2); // Move iterator to the 3rd position
numbers1.erase(deletePosition);
cout << "Deleted the 3rd element: ";
displayList(numbers1);
cout << endl;

// 4. Accessing Elements
cout << "4. Accessing Elements" << endl;
cout << "Accessing elements using a range-based loop: ";
for (int value : numbers1)
{
cout << value << " ";
}
cout << endl
<< endl;

// 5. Size and Capacity


cout << "5. Size and Capacity" << endl;
cout << "Size of the list: " << numbers1.size() << endl;
cout << "Is the list empty? " << (numbers1.empty() ? "Yes" : "No") << endl;
cout << endl;

// 6. Sorting and Reversing


cout << "6. Sorting and Reversing" << endl;

// Sorting the list in ascending order


numbers1.sort();
cout << "List after sorting: ";
displayList(numbers1);

// Reversing the list


numbers1.reverse();
cout << "List after reversing: ";
displayList(numbers1);
cout << endl;
// 7. Merging and Splicing
cout << "7. Merging and Splicing" << endl;

// Merging another sorted list


list<int> numbersToMerge = {7, 8, 9};
numbers1.merge(numbersToMerge);
cout << "List after merging with {7, 8, 9}: ";
displayList(numbers1);

// Splicing elements from another list


list<int> numbersToSplice = {10, 11, 12};
auto splicePosition = numbers1.begin();
advance(splicePosition, 3);
numbers1.splice(splicePosition, numbersToSplice);
cout << "List after splicing {10, 11, 12} at the 4th position: ";
displayList(numbers1);
cout << endl;

// 8. Multidimensional List (Multilist)


cout << "8. Multidimensional List (Multilist)" << endl;

list<list<int>> multiList = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};


cout << "Displaying a multidimensional list:" << endl;
displayMultiList(multiList);
cout << endl;

// 9. Assign and Removing


cout << "9. Assign and Removing" << endl;

// Assign new values


numbers1.assign(5, 100); // Assign 5 elements, each with value 100
cout << "List after assigning 5 elements with value 100: ";
displayList(numbers1);

// Remove a specific value


numbers1.remove(100);
cout << "List after removing all occurrences of 100: ";
displayList(numbers1);

// Remove consecutive duplicates


numbers1 = {10, 20, 20, 30, 40, 40, 40};
numbers1.unique();
cout << "List after removing consecutive duplicates: ";
displayList(numbers1);
cout << endl;

// 10. Remove Elements Based on Condition


cout << "10. Remove Elements Based on Condition" << endl;

// Remove even numbers


numbers1.remove_if([](int value)
{ return value % 2 == 0; });
cout << "List after removing even numbers: ";
displayList(numbers1);

cout << "Program finished successfully!" << endl;


return 0;
}
OUTPUT:

1. Declaration and Initialization


List initialized using an initializer list: 1 2 3 4 5
List initialized with specific size and value: 10 10 10 10 10

2. Insertion
Inserted 0 at the beginning: 0 1 2 3 4 5
Inserted 6 at the end: 0 1 2 3 4 5 6
Inserted 42 at the 3rd position: 0 1 2 42 3 4 5 6

3. Deletion
Deleted the first element: 1 2 42 3 4 5 6
Deleted the last element: 1 2 42 3 4 5
Deleted the 3rd element: 1 2 3 4 5

4. Accessing Elements
Accessing elements using a range-based loop: 1 2 3 4 5

5. Size and Capacity


Size of the list: 5
Is the list empty? No

6. Sorting and Reversing


List after sorting: 1 2 3 4 5
List after reversing: 5 4 3 2 1

7. Merging and Splicing


List after merging with {7, 8, 9}: 5 4 3 2 1 7 8 9
List after splicing {10, 11, 12} at the 4th position: 5 4 3 10 11 12 2 1 7 8 9

8. Multidimensional List (Multilist)


Displaying a multidimensional list:
1 2 3
4 5 6
7 8 9

9. Assign and Removing


List after assigning 5 elements with value 100: 100 100 100 100 100
List after removing all occurrences of 100:
List after removing consecutive duplicates: 10 20 30 40

10. Remove Elements Based on Condition


List after removing even numbers:
Program finished successfully!

You might also like