insert();remove();find();filter()
This commit is contained in:
148
pr6.cpp
148
pr6.cpp
@@ -1,5 +1,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -222,7 +223,99 @@ public:
|
|||||||
return res;
|
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) {
|
friend ostream& operator<<(ostream& os, const DoublyLinkedStack& stack) {
|
||||||
DoublyLinkedElement<T>* current = static_cast<DoublyLinkedElement<T>*>(stack.head);
|
DoublyLinkedElement<T>* current = static_cast<DoublyLinkedElement<T>*>(stack.head);
|
||||||
while (current != nullptr) {
|
while (current != nullptr) {
|
||||||
@@ -232,7 +325,7 @@ public:
|
|||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overload the [] operator for indexing
|
// Overloading the subscript operator
|
||||||
T& operator[](int index) {
|
T& operator[](int index) {
|
||||||
if (index < 0 || index >= this->count) {
|
if (index < 0 || index >= this->count) {
|
||||||
throw out_of_range("Index out of range");
|
throw out_of_range("Index out of range");
|
||||||
@@ -242,43 +335,42 @@ public:
|
|||||||
for (int i = 0; i < index; ++i) {
|
for (int i = 0; i < index; ++i) {
|
||||||
current = static_cast<DoublyLinkedElement<T>*>(current->getNext());
|
current = static_cast<DoublyLinkedElement<T>*>(current->getNext());
|
||||||
}
|
}
|
||||||
|
|
||||||
return current->info;
|
return current->info;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~DoublyLinkedStack() { cout << "\nDoublyLinkedStack class destructor\n"; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
StackQueue<int> stackQueue;
|
DoublyLinkedStack<int> stack;
|
||||||
|
|
||||||
// Добавление элементов в конец и начало списка
|
stack.push(1);
|
||||||
stackQueue.push_back(1);
|
stack.push(2);
|
||||||
stackQueue.push_back(2);
|
stack.push(3);
|
||||||
stackQueue.push_front(3);
|
stack.push(4);
|
||||||
stackQueue.push_front(4);
|
|
||||||
|
|
||||||
// Удаление элементов с начала и конца списка
|
cout << "Stack after pushing 1, 2, 3, 4: " << stack << endl;
|
||||||
while (!stackQueue.isEmpty()) {
|
|
||||||
Element<int>* elem = stackQueue.pop_back();
|
stack.insert(2, 99);
|
||||||
if (elem) {
|
cout << "Stack after inserting 99 at index 2: " << stack << endl;
|
||||||
cout << elem->getInfo() << " ";
|
|
||||||
delete elem;
|
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
|
cout << "Filtering elements greater than 2: ";
|
||||||
DoublyLinkedStack<int> doublyLinkedStack;
|
stack.filter([](const int& value) { return value > 2; });
|
||||||
doublyLinkedStack.push(10);
|
|
||||||
doublyLinkedStack.push(20);
|
|
||||||
doublyLinkedStack.push(30);
|
|
||||||
|
|
||||||
cout << "Stack contents: " << doublyLinkedStack << endl;
|
cout << "Element at index 1: " << stack[1] << endl;
|
||||||
|
|
||||||
cout << "Element at index 1: " << doublyLinkedStack[1] << endl;
|
while (!stack.isEmpty()) {
|
||||||
|
Element<int>* elem = stack.pop();
|
||||||
while (!doublyLinkedStack.isEmpty()) {
|
|
||||||
Element<int>* elem = doublyLinkedStack.pop();
|
|
||||||
if (elem) {
|
if (elem) {
|
||||||
cout << elem->getInfo() << " ";
|
cout << elem->getInfo() << " ";
|
||||||
delete elem;
|
delete elem;
|
||||||
|
|||||||
Reference in New Issue
Block a user