Compare commits

...

4 Commits

242
pr6.cpp
View File

@ -1,8 +1,33 @@
#include <iostream>
#include <stdexcept>
#include <functional>
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:
@ -31,6 +56,7 @@ public:
}
template<class T1> friend class LinkedList;
template<class T1> friend class DoublyLinkedStack;
};
template<class T1>
@ -161,18 +187,214 @@ 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:
DoublyLinkedElement* prev;
public:
DoublyLinkedElement() : Element<T>(), prev(nullptr) {}
DoublyLinkedElement(const T& data) : Element<T>(data), prev(nullptr) {}
DoublyLinkedElement(DoublyLinkedElement* Next, DoublyLinkedElement* Prev, const T& data)
: Element<T>(Next, data), prev(Prev) {}
DoublyLinkedElement* getPrev() const {
return prev;
}
void setPrev(DoublyLinkedElement* prevElement) {
prev = prevElement;
}
};
template<class T>
class DoublyLinkedStack : public SinglyLinkedListStack<T> {
public:
DoublyLinkedStack() : SinglyLinkedListStack<T>() {}
virtual Element<T>* push(const T& value) override {
DoublyLinkedElement<T>* newElement = new DoublyLinkedElement<T>(value);
if (this->head == nullptr) {
this->head = this->tail = newElement;
}
else {
static_cast<DoublyLinkedElement<T>*>(this->tail)->setNext(newElement);
newElement->setPrev(static_cast<DoublyLinkedElement<T>*>(this->tail));
this->tail = newElement;
}
this->count++;
return newElement;
}
virtual Element<T>* pop() override {
if (this->tail == nullptr)
return nullptr;
DoublyLinkedElement<T>* res = static_cast<DoublyLinkedElement<T>*>(this->tail);
if (this->head == this->tail) {
this->head = this->tail = nullptr;
}
else {
this->tail = res->getPrev();
if (this->tail) {
this->tail->setNext(nullptr);
}
}
this->count--;
return res;
}
// Insert a new element at the specified index
void insert(int index, const T& value) {
if (index < 0 || index > this->count) {
throw out_of_range("Index out of range");
}
if (index == this->count) {
push(value);
return;
}
DoublyLinkedElement<T>* newElement = new DoublyLinkedElement<T>(value);
if (index == 0) {
newElement->setNext(static_cast<DoublyLinkedElement<T>*>(this->head));
static_cast<DoublyLinkedElement<T>*>(this->head)->setPrev(newElement);
this->head = newElement;
}
else {
DoublyLinkedElement<T>* current = static_cast<DoublyLinkedElement<T>*>(this->head);
for (int i = 0; i < index - 1; ++i) {
current = static_cast<DoublyLinkedElement<T>*>(current->getNext());
}
newElement->setNext(current->getNext());
newElement->setPrev(current);
if (current->getNext() != nullptr) {
static_cast<DoublyLinkedElement<T>*>(current->getNext())->setPrev(newElement);
}
current->setNext(newElement);
}
this->count++;
}
// Remove the element at the specified index
void remove(int index) {
if (index < 0 || index >= this->count) {
throw out_of_range("Index out of range");
}
DoublyLinkedElement<T>* toDelete = static_cast<DoublyLinkedElement<T>*>(this->head);
if (index == 0) {
this->head = toDelete->getNext();
if (this->head != nullptr) {
static_cast<DoublyLinkedElement<T>*>(this->head)->setPrev(nullptr);
}
}
else {
for (int i = 0; i < index; ++i) {
toDelete = static_cast<DoublyLinkedElement<T>*>(toDelete->getNext());
}
DoublyLinkedElement<T>* prevElement = toDelete->getPrev();
DoublyLinkedElement<T>* nextElement = static_cast<DoublyLinkedElement<T>*>(toDelete->getNext());
if (prevElement != nullptr) {
prevElement->setNext(nextElement);
}
if (nextElement != nullptr) {
nextElement->setPrev(prevElement);
}
}
if (toDelete == this->tail) {
this->tail = toDelete->getPrev();
}
delete toDelete;
this->count--;
}
// Find the first element with the specified value
Element<T>* find(const T& value) const {
DoublyLinkedElement<T>* current = static_cast<DoublyLinkedElement<T>*>(this->head);
while (current != nullptr) {
if (current->getInfo() == value) {
return current;
}
current = static_cast<DoublyLinkedElement<T>*>(current->getNext());
}
return nullptr;
}
// Find all elements that satisfy the given condition
void filter(function<bool(const T&)> condition) const {
DoublyLinkedElement<T>* current = static_cast<DoublyLinkedElement<T>*>(this->head);
while (current != nullptr) {
if (condition(current->getInfo())) {
cout << current->getInfo() << " ";
}
current = static_cast<DoublyLinkedElement<T>*>(current->getNext());
}
cout << endl;
}
// Overloading the output stream operator
friend ostream& operator<<(ostream& os, const DoublyLinkedStack& stack) {
DoublyLinkedElement<T>* current = static_cast<DoublyLinkedElement<T>*>(stack.head);
while (current != nullptr) {
os << current->getInfo() << " ";
current = static_cast<DoublyLinkedElement<T>*>(current->getNext());
}
return os;
}
// Overloading the subscript operator
T& operator[](int index) {
if (index < 0 || index >= this->count) {
throw out_of_range("Index out of range");
}
DoublyLinkedElement<T>* current = static_cast<DoublyLinkedElement<T>*>(this->head);
for (int i = 0; i < index; ++i) {
current = static_cast<DoublyLinkedElement<T>*>(current->getNext());
}
return current->info;
}
};
int main() {
StackQueue<int> stackQueue;
DoublyLinkedStack<int> stack;
// Добавление элементов в конец и начало списка
stackQueue.push_back(1);
stackQueue.push_back(2);
stackQueue.push_front(3);
stackQueue.push_front(4);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
// Удаление элементов с начала и конца списка
while (!stackQueue.isEmpty()) {
Element<int>* elem = stackQueue.pop_back();
cout << "Stack after pushing 1, 2, 3, 4: " << stack << endl;
stack.insert(2, 99);
cout << "Stack after inserting 99 at index 2: " << stack << endl;
stack.remove(1);
cout << "Stack after removing element at index 1: " << stack << endl;
Element<int>* found = stack.find(99);
if (found) {
cout << "Found element with value 99: " << found->getInfo() << endl;
}
else {
cout << "Element with value 99 not found" << endl;
}
cout << "Filtering elements greater than 2: ";
stack.filter([](const int& value) { return value > 2; });
cout << "Element at index 1: " << stack[1] << endl;
while (!stack.isEmpty()) {
Element<int>* elem = stack.pop();
if (elem) {
cout << elem->getInfo() << " ";
delete elem;
@ -181,4 +403,4 @@ int main() {
cout << endl;
return 0;
}
}