fix findStateByName;

This commit is contained in:
2024-05-23 01:01:51 +03:00
parent 5ed52b5e02
commit 6584ce5f92

57
pr6.cpp
View File

@@ -23,6 +23,10 @@ public:
return info; return info;
} }
T& getInfo() {
return info;
}
Element* getNext() const { Element* getNext() const {
return next; return next;
} }
@@ -100,12 +104,13 @@ public:
this->head = this->tail = nullptr; this->head = this->tail = nullptr;
} }
else { else {
Element<T>* current = this->head; /*Element<T>* current = this->head;
while (current->getNext() != this->tail) { while (current->getNext() != this->tail) {
current = current->getNext(); current = current->getNext();
} }
this->tail = current; this->tail = current;
current->setNext(nullptr); current->setNext(nullptr);*/
this->head = this->head->getNext();
} }
this->count--; this->count--;
@@ -421,15 +426,16 @@ public:
} }
// Поиск государства по названию // Поиск государства по названию
State findStateByName(const string& name) { State* findStateByName(const string& name) {
Element<State>* current = stateList.getHead(); Element<State>* current = stateList.getHead();
while (current) { while (current != nullptr) {
if (current->getInfo().getName() == name) { if (current->getInfo().getName() == name) {
return current->getInfo(); return &(current->getInfo());
} }
current = current->getNext(); current = current->getNext();
} }
throw logic_error("State not found"); /*throw logic_error("State not found");*/
return nullptr;
} }
// Фильтрация государств по площади // Фильтрация государств по площади
@@ -496,19 +502,42 @@ int main() {
cout << endl; cout << endl;
//StateList //StateList
StateList stateList; StateList states;
// Пример использования // Пример использования
State usa("USA", "Washington D.C.", "English", 328000000, 9833510); states.addState(State("USA", "Washington D.C.", "English", 328000000, 9833510));
State india("India", "New Delhi", "Hindi", 1380000000, 3287263); states.addState(State("India", "New Delhi", "Hindi", 1380000000, 3287263));
stateList.addState(usa); //stateList.addState(usa);
stateList.addState(india); //stateList.addState(india);
cout << "List of states:" << endl; cout << "List of states:" << endl;
cout << stateList.findStateByName("USA") << endl; cout << states.findStateByName("USA") << endl;
//cout << "Removing state:" << endl;
//cout << stateList.removeState() << endl;
cout << "States with area between 2000 and 3000:" << endl;
states.printStatesByArea(2000, 3000);
State* foundState = states.findStateByName("USA");
if (foundState) {
cout << "Found state: " << *foundState << endl;
}
else {
cout << "State not found" << endl;
}
try {
State removedState = states.removeState();
cout << "Removed state: " << removedState << endl;
}
catch (const out_of_range& e) {
cout << e.what() << endl;
}
cout << "States after removal:" << endl;
states.printStatesByArea(0, 10000);
cout << "Removing state:" << endl;
cout << stateList.removeState() << endl;
return 0; return 0;