include iostream include stack using namespace std struct books char n

 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
#include <iostream>
#include <stack>
using namespace std;
struct books {
char name[40];
int year;
};
using namespace std;
int main() {
size_t dimension = 3;
books array[dimension];
stack<books> temp1;
stack<books> temp2;
stack<books> _books;
for (int i = 0; i < dimension; ++i) {
cout << "Enter name" << endl;
cin >> array[i].name;
cout << "Enter year" << endl;
cin >> array[i].year;
}
cout << "entered: \n";
for (int i = 0; i < dimension; ++i) {
temp1.push(array[i]);
cout << temp1.top().name << " - " << temp1.top().year << endl;
}
int max, temp;
while (!temp1.empty()) {
max = temp1.top().year;
temp2.push(temp1.top());
temp1.pop();
while (!temp1.empty()) {
temp = temp1.top().year;
if (temp > max) {
max = temp;
}
temp2.push(temp1.top());
temp1.pop();
}
while (!temp2.empty()) {
temp = temp2.top().year;
if (temp == max) {
_books.push(temp2.top());
} else {
temp1.push(temp2.top());
}
temp2.pop();
}
}
cout << "sorted: " << endl;
while (!_books.empty()) {
cout << _books.top().name << " - " << _books.top().year << endl;
_books.pop();
}
return 0;
}