fix removeState, rm addState, add pop override to fit inheritence

This commit is contained in:
gandc 2024-05-23 04:01:45 +03:00
parent 5cdcca7290
commit c55592d210
Signed by: gandc
GPG Key ID: 9F77B03D43C42CB4

20
pr6.cpp
View File

@ -421,8 +421,12 @@ protected:
public:
// Добавление нового государства в начало списка
void addState(const State& state) {
//void addState(const State& state) {
// stateList.push(state);
//}
virtual Element<State>* push(const State& state) override {
stateList.push(state);
return stateList.getHead();
}
friend ostream& operator<<(ostream& os, const StateList& stateList) {
Element<State>* current = stateList.stateList.getHead();
@ -434,6 +438,15 @@ public:
}
// Удаление государства из начала списка
virtual Element<State>* pop() override {
try {
return new Element<State>(removeState());
}
catch (const logic_error& e) {
cout << e.what() << endl;
return nullptr;
}
}
State removeState() {
Element<State>* element = stateList.pop();
if (element) {
@ -443,7 +456,6 @@ public:
}
throw logic_error("State list is empty");
}
// Поиск государства по названию
State* findStateByName(const string& name) {
Element<State>* current = stateList.getHead();
@ -588,8 +600,8 @@ int main() {
StateList* states = new StateList();
//for 6.2
states->addState(State("USA", "Washington D.C.", "English", 328000000, 9833510));
states->addState(State("India", "New Delhi", "Hindi", 1380000000, 3287263));
states->push(State("USA", "Washington D.C.", "English", 328000000, 9833510));
states->push(State("India", "New Delhi", "Hindi", 1380000000, 3287263));
// Сохраняем список состояний в файл
states->save("states.txt");