Compare commits
3 Commits
a9ba899b81
...
6584ce5f92
| Author | SHA1 | Date | |
|---|---|---|---|
| 6584ce5f92 | |||
| 5ed52b5e02 | |||
| 9284b28471 |
204
pr6.cpp
204
pr6.cpp
@ -4,30 +4,6 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class State {
|
||||
public:
|
||||
string name;
|
||||
string capital;
|
||||
string language;
|
||||
long population;
|
||||
double area;
|
||||
|
||||
State() : name(""), capital(""), language(""), population(0), area(0.0) {}
|
||||
State(string n, string c, string l, long p, double a)
|
||||
: name(n), capital(c), language(l), population(p), area(a) {}
|
||||
|
||||
friend ostream& operator<<(ostream& os, const State& state) {
|
||||
os << "State: " << state.name << ", Capital: " << state.capital
|
||||
<< ", Language: " << state.language << ", Population: " << state.population
|
||||
<< ", Area: " << state.area;
|
||||
return os;
|
||||
}
|
||||
|
||||
bool operator==(const State& other) const {
|
||||
return name == other.name;
|
||||
}
|
||||
};
|
||||
|
||||
template<class T>
|
||||
class Element {
|
||||
protected:
|
||||
@ -47,6 +23,10 @@ public:
|
||||
return info;
|
||||
}
|
||||
|
||||
T& getInfo() {
|
||||
return info;
|
||||
}
|
||||
|
||||
Element* getNext() const {
|
||||
return next;
|
||||
}
|
||||
@ -57,6 +37,7 @@ public:
|
||||
|
||||
template<class T1> friend class LinkedList;
|
||||
template<class T1> friend class DoublyLinkedStack;
|
||||
template<class T1> friend ostream& operator<<(ostream& s, const Element<T1>& el);
|
||||
};
|
||||
|
||||
template<class T1>
|
||||
@ -79,6 +60,8 @@ public:
|
||||
virtual Element<T>* push(const T& value) = 0;
|
||||
|
||||
bool isEmpty() { return count == 0; }
|
||||
//getter
|
||||
Element<T>* getHead() const { return head; }
|
||||
|
||||
virtual ~LinkedList() {
|
||||
cout << "\nBase class destructor\n";
|
||||
@ -121,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--;
|
||||
@ -187,7 +171,6 @@ public:
|
||||
using LinkedList<T>::isEmpty;
|
||||
};
|
||||
|
||||
// New class definitions for Doubly Linked List Elements and Doubly Linked Stack
|
||||
template<class T>
|
||||
class DoublyLinkedElement : public Element<T> {
|
||||
protected:
|
||||
@ -213,6 +196,8 @@ class DoublyLinkedStack : public SinglyLinkedListStack<T> {
|
||||
public:
|
||||
DoublyLinkedStack() : SinglyLinkedListStack<T>() {}
|
||||
|
||||
using LinkedList::head;
|
||||
|
||||
virtual Element<T>* push(const T& value) override {
|
||||
DoublyLinkedElement<T>* newElement = new DoublyLinkedElement<T>(value);
|
||||
if (this->head == nullptr) {
|
||||
@ -315,30 +300,54 @@ public:
|
||||
this->count--;
|
||||
}
|
||||
|
||||
// Find the first element with the specified value
|
||||
// Iterative version of find
|
||||
Element<T>* find(const T& value) const {
|
||||
DoublyLinkedElement<T>* current = static_cast<DoublyLinkedElement<T>*>(this->head);
|
||||
//DoublyLinkedElement<T>* current = static_cast<DoublyLinkedElement<T>*>(this->head);
|
||||
Element<T>* current = this->head;
|
||||
while (current != nullptr) {
|
||||
if (current->getInfo() == value) {
|
||||
return current;
|
||||
}
|
||||
current = static_cast<DoublyLinkedElement<T>*>(current->getNext());
|
||||
current = current->getNext(); //static_cast<DoublyLinkedElement<T>*>(current->getNext());
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Find all elements that satisfy the given condition
|
||||
// Recursive version of find
|
||||
Element<T>* find_recursive(const T& value, DoublyLinkedElement<T>* current) const {
|
||||
if (current == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
if (current->getInfo() == value) {
|
||||
return current;
|
||||
}
|
||||
return find_recursive(value, static_cast<DoublyLinkedElement<T>*>(current->getNext()));
|
||||
}
|
||||
|
||||
// Iterative version of filter
|
||||
void filter(function<bool(const T&)> condition) const {
|
||||
DoublyLinkedElement<T>* current = static_cast<DoublyLinkedElement<T>*>(this->head);
|
||||
//DoublyLinkedElement<T>* current = static_cast<DoublyLinkedElement<T>*>(this->head);
|
||||
Element<T>* current = this->head;
|
||||
while (current != nullptr) {
|
||||
if (condition(current->getInfo())) {
|
||||
cout << current->getInfo() << " ";
|
||||
}
|
||||
current = static_cast<DoublyLinkedElement<T>*>(current->getNext());
|
||||
current = current->getNext(); //static_cast<DoublyLinkedElement<T>*>(current->getNext());
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
// Recursive version of filter
|
||||
void filter_recursive(function<bool(const T&)> condition, DoublyLinkedElement<T>* current) const {
|
||||
if (current == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (condition(current->getInfo())) {
|
||||
cout << current->getInfo() << " ";
|
||||
}
|
||||
filter_recursive(condition, static_cast<DoublyLinkedElement<T>*>(current->getNext()));
|
||||
}
|
||||
|
||||
// Overloading the output stream operator
|
||||
friend ostream& operator<<(ostream& os, const DoublyLinkedStack& stack) {
|
||||
DoublyLinkedElement<T>* current = static_cast<DoublyLinkedElement<T>*>(stack.head);
|
||||
@ -364,6 +373,84 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class State {
|
||||
protected:
|
||||
string name;
|
||||
string capital;
|
||||
string language;
|
||||
long population;
|
||||
double area;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
State() : name(""), capital(""), language(""), population(0), area(0.0) {}
|
||||
State(string n, string c, string l, long p, double a)
|
||||
: name(n), capital(c), language(l), population(p), area(a) {}
|
||||
|
||||
friend ostream& operator<<(ostream& os, const State& state) {
|
||||
os << "State: " << state.name << ", Capital: " << state.capital
|
||||
<< ", Language: " << state.language << ", Population: " << state.population
|
||||
<< ", Area: " << state.area;
|
||||
return os;
|
||||
}
|
||||
|
||||
bool operator==(const State& other) const {
|
||||
return name == other.name;
|
||||
}
|
||||
// geters
|
||||
string getName() const { return name; }
|
||||
double getArea() const { return area; }
|
||||
};
|
||||
|
||||
|
||||
class StateList {
|
||||
protected:
|
||||
SinglyLinkedListQueue<State> stateList;
|
||||
|
||||
public:
|
||||
// Добавление нового государства в начало списка
|
||||
void addState(const State& state) {
|
||||
stateList.push(state);
|
||||
}
|
||||
|
||||
// Удаление государства из начала списка
|
||||
State removeState() {
|
||||
Element<State>* element = stateList.pop();
|
||||
if (element) {
|
||||
State state = element->getInfo();
|
||||
delete element;
|
||||
return state;
|
||||
}
|
||||
throw logic_error("State list is empty");
|
||||
}
|
||||
|
||||
// Поиск государства по названию
|
||||
State* findStateByName(const string& name) {
|
||||
Element<State>* current = stateList.getHead();
|
||||
while (current != nullptr) {
|
||||
if (current->getInfo().getName() == name) {
|
||||
return &(current->getInfo());
|
||||
}
|
||||
current = current->getNext();
|
||||
}
|
||||
/*throw logic_error("State not found");*/
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Фильтрация государств по площади
|
||||
void printStatesByArea(double minArea, double maxArea) {
|
||||
Element<State>* current = stateList.getHead();
|
||||
while (current != nullptr) {
|
||||
const State& state = current->getInfo();
|
||||
if (state.getArea() >= minArea && state.getArea() <= maxArea) {
|
||||
cout << state << endl;
|
||||
}
|
||||
current = current->getNext();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
DoublyLinkedStack<int> stack;
|
||||
|
||||
@ -388,9 +475,21 @@ int main() {
|
||||
cout << "Element with value 99 not found" << endl;
|
||||
}
|
||||
|
||||
found = stack.find_recursive(3, static_cast<DoublyLinkedElement<int>*>(stack.head));
|
||||
if (found) {
|
||||
cout << "Found element with value 3 using recursive find: " << found->getInfo() << endl;
|
||||
}
|
||||
else {
|
||||
cout << "Element with value 3 not found using recursive find" << endl;
|
||||
}
|
||||
|
||||
cout << "Filtering elements greater than 2: ";
|
||||
stack.filter([](const int& value) { return value > 2; });
|
||||
|
||||
cout << "Filtering elements greater than 2 using recursive filter: ";
|
||||
stack.filter_recursive([](const int& value) { return value > 2; }, static_cast<DoublyLinkedElement<int>*>(stack.head));
|
||||
cout << endl;
|
||||
|
||||
cout << "Element at index 1: " << stack[1] << endl;
|
||||
|
||||
while (!stack.isEmpty()) {
|
||||
@ -402,5 +501,44 @@ int main() {
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
//StateList
|
||||
StateList states;
|
||||
|
||||
// Пример использования
|
||||
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 << 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);
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user