-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.cpp
More file actions
145 lines (103 loc) · 2.32 KB
/
5.cpp
File metadata and controls
145 lines (103 loc) · 2.32 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <list>
using namespace std;
int main()
{
cout << "Bidirectional linked list: " << endl;
list<int> nums = { 3,1,5,9,2,6,4,7,8 };
cout << "list: ";
for (int el : nums)
{
cout << el << " ";
}
cout << "\n\nAfter sorting using iterators:" << endl;
bool exit = true;
while (exit) {
exit = false;
list<int>::iterator first = nums.begin();
list<int>::iterator second = ++nums.begin();
while (second != nums.end())
{
if (*first > *second) {
int temp = *first;
*first = *second;
*second = temp;
exit = true;
}
++first;
++second;
}
}
for (int el : nums)
{
cout << el << " ";
}
int k = 5;
cout << "\n\nTry to find " << k << " in list using by iterators: " << endl;
int index = 0;
bool check = true;
list<int>::iterator current = nums.begin();
while (current != --nums.end()) {
if (*current == k) {
cout << "k=" << 5 << " founded of index " << index << endl;
check = false;
}
index++;
++current;
}
if (check) {
cout << "Not founded" << endl;
}
cout << "\n\n\n" << endl;
cout << "Bidirectional linked list: " << endl;
nums = { 3,1,5,9,2,6,4,7,8 };
cout << "list: ";
for (int el : nums)
{
cout << el << " ";
}
cout << "\n\nAfter sorting:" << endl;
list<int> sorted_nums;
list<int> temp_nums;
int size = nums.size();
while (sorted_nums.size() != size) {
int max = nums.front();
while (nums.size() != 0) {
if (nums.front() > max) {
max = nums.front();
}
temp_nums.push_back(nums.front());
nums.pop_front();
}
while (temp_nums.size() != 0) {
if (temp_nums.front() == max) {
sorted_nums.push_back(temp_nums.front());
temp_nums.pop_front();
}
else {
nums.push_back(temp_nums.front());
temp_nums.pop_front();
}
}
}
for (int el : sorted_nums)
{
nums.push_front(el);
}
cout << "list: ";
for (int el : nums)
{
cout << el << " ";
}
cout << "\n\nTry to find "<< k <<" in list: " << endl;
index = 0;
for (int el : nums)
{
if (el == k) {
cout << "k="<< 5 <<" founded of index " << index << endl;
return 0;
}
index++;
}
cout << "Not founded" << endl;
}