-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist_program.cpp
More file actions
54 lines (43 loc) · 1.4 KB
/
list_program.cpp
File metadata and controls
54 lines (43 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <list>
int main() {
// Declare a list of integers
std::list<int> numbers;
// Add elements to the list
numbers.push_back(10); // Add at the end
numbers.push_front(5); // Add at the beginning
numbers.push_back(20); // Add at the end
// Access elements
std::cout << "Elements in the list:" << std::endl;
for (const auto& num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
// Insert an element after the first element
auto it = numbers.begin();
++it; // Advance iterator to the second element
numbers.insert(it, 15);
// Access elements using iterators
std::cout << "Elements in the list (using iterators):" << std::endl;
for (const auto& num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
// Remove the last element
numbers.pop_back();
// Check if the list is empty
if (numbers.empty()) {
std::cout << "The list is empty." << std::endl;
} else {
std::cout << "The list is not empty." << std::endl;
}
// Get the size of the list
std::cout << "Size of the list: " << numbers.size() << std::endl;
// Clear all elements from the list
numbers.clear();
// Check if the list is empty again
if (numbers.empty()) {
std::cout << "The list is empty after clearing." << std::endl;
}
return 0;
}