#include <iostream>
#include <list>
int main() {
// Step 1: Declare a list of integers
std::list<int> mylist;
// Step 2: Add elements to the list using push_back
mylist.push_back(10);
mylist.push_back(20);
mylist.push_back(30);
// Step 3: Access and print elements using iterators
std::cout << "Elements in the list: ";
for (auto it = mylist.begin(); it != mylist.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
// Step 4: Insert an element at the beginning of the list
mylist.push_front(5);
// Step 5: Remove an element from the end of the list
mylist.pop_back();
// Step 6: Print the updated list
std::cout << "Updated elements in the list: ";
for (const auto& element : mylist) {
std::cout << element << " ";
}
std::cout << std::endl;
// Step 7: Check if the list is empty
if (mylist.empty()) {
std::cout << "The list is empty." << std::endl;
} else {
std::cout << "The list is not empty." << std::endl;
}
// Step 8: Get the size of the list
std::cout << "Size of the list: " << mylist.size() << std::endl;
// Step 9: Clear all elements from the list
mylist.clear();
// Step 10: Check if the list is empty after clearing
if (mylist.empty()) {
std::cout << "The list is empty after clearing." << std::endl;
} else {
std::cout << "The list is not empty after clearing." << std::endl;
}
return 0;
}