insert();remove();find();filter()

This commit is contained in:
gandc 2024-05-20 12:30:10 +03:00
parent ae743ee8ec
commit fd630bd508
Signed by: gandc
GPG Key ID: 9F77B03D43C42CB4

148
pr6.cpp
View File

@ -1,5 +1,6 @@
#include <iostream>
#include <stdexcept>
#include <functional>
using namespace std;
@ -222,7 +223,99 @@ public:
return res;
}
// Overload the << operator for output
// 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) {
@ -232,7 +325,7 @@ public:
return os;
}
// Overload the [] operator for indexing
// Overloading the subscript operator
T& operator[](int index) {
if (index < 0 || index >= this->count) {
throw out_of_range("Index out of range");
@ -242,43 +335,42 @@ public:
for (int i = 0; i < index; ++i) {
current = static_cast<DoublyLinkedElement<T>*>(current->getNext());
}
return current->info;
}
virtual ~DoublyLinkedStack() { cout << "\nDoublyLinkedStack class destructor\n"; }
};
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();
if (elem) {
cout << elem->getInfo() << " ";
delete elem;
}
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 << endl;
// Testing DoublyLinkedStack
DoublyLinkedStack<int> doublyLinkedStack;
doublyLinkedStack.push(10);
doublyLinkedStack.push(20);
doublyLinkedStack.push(30);
cout << "Filtering elements greater than 2: ";
stack.filter([](const int& value) { return value > 2; });
cout << "Stack contents: " << doublyLinkedStack << endl;
cout << "Element at index 1: " << stack[1] << endl;
cout << "Element at index 1: " << doublyLinkedStack[1] << endl;
while (!doublyLinkedStack.isEmpty()) {
Element<int>* elem = doublyLinkedStack.pop();
while (!stack.isEmpty()) {
Element<int>* elem = stack.pop();
if (elem) {
cout << elem->getInfo() << " ";
delete elem;