fix findStateByName;

This commit is contained in:
gandc 2024-05-23 01:01:51 +03:00
parent 5ed52b5e02
commit 6584ce5f92
Signed by: gandc
GPG Key ID: 9F77B03D43C42CB4

57
pr6.cpp
View File

@ -23,6 +23,10 @@ public:
return info;
}
T& getInfo() {
return info;
}
Element* getNext() const {
return next;
}
@ -100,12 +104,13 @@ public:
this->head = this->tail = nullptr;
}
else {
Element<T>* current = this->head;
/*Element<T>* current = this->head;
while (current->getNext() != this->tail) {
current = current->getNext();
}
this->tail = current;
current->setNext(nullptr);
current->setNext(nullptr);*/
this->head = this->head->getNext();
}
this->count--;
@ -421,15 +426,16 @@ public:
}
// Поиск государства по названию
State findStateByName(const string& name) {
State* findStateByName(const string& name) {
Element<State>* current = stateList.getHead();
while (current) {
while (current != nullptr) {
if (current->getInfo().getName() == name) {
return current->getInfo();
return &(current->getInfo());
}
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;
//StateList
StateList stateList;
StateList states;
// Пример использования
State usa("USA", "Washington D.C.", "English", 328000000, 9833510);
State india("India", "New Delhi", "Hindi", 1380000000, 3287263);
stateList.addState(usa);
stateList.addState(india);
states.addState(State("USA", "Washington D.C.", "English", 328000000, 9833510));
states.addState(State("India", "New Delhi", "Hindi", 1380000000, 3287263));
//stateList.addState(usa);
//stateList.addState(india);
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;