Revert "filter return new element list"
This reverts commit 776f8655269d687fac801ae99ffe0506b6be4b97.
This commit is contained in:
parent
776f865526
commit
ce929f13f2
264
pr6.cpp
264
pr6.cpp
@ -7,7 +7,7 @@ template<class T>
|
||||
class Element {
|
||||
protected:
|
||||
Element* next;
|
||||
Element* prev;
|
||||
Element* prev; // Новый указатель на предыдущий элемент
|
||||
T info;
|
||||
|
||||
public:
|
||||
@ -27,7 +27,7 @@ public:
|
||||
return next;
|
||||
}
|
||||
|
||||
Element* getPrev() const {
|
||||
Element* getPrev() const { // Новый метод для доступа к prev
|
||||
return prev;
|
||||
}
|
||||
|
||||
@ -35,7 +35,7 @@ public:
|
||||
next = nextElement;
|
||||
}
|
||||
|
||||
void setPrev(Element* prevElement) {
|
||||
void setPrev(Element* prevElement) { // Новый метод для установки prev
|
||||
prev = prevElement;
|
||||
}
|
||||
|
||||
@ -74,32 +74,8 @@ public:
|
||||
head = tail = nullptr;
|
||||
count = 0;
|
||||
}
|
||||
|
||||
Element<T>& operator[](int index) {
|
||||
if (index < 0 || index >= count) {
|
||||
throw out_of_range("Index out of range");
|
||||
}
|
||||
Element<T>* current = head;
|
||||
for (int i = 0; i < index; ++i) {
|
||||
current = current->getNext();
|
||||
}
|
||||
return *current;
|
||||
}
|
||||
|
||||
template<class T1>
|
||||
friend ostream& operator<<(ostream& s, const LinkedList<T1>& list);
|
||||
};
|
||||
|
||||
template<class T1>
|
||||
ostream& operator<<(ostream& s, const LinkedList<T1>& list) {
|
||||
Element<T1>* current = list.head;
|
||||
while (current != nullptr) {
|
||||
s << current->getInfo() << " ";
|
||||
current = current->getNext();
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
class SinglyLinkedListStack : public LinkedList<T> {
|
||||
public:
|
||||
@ -144,8 +120,58 @@ public:
|
||||
};
|
||||
|
||||
template<class T>
|
||||
class DoublyLinkedListStack : public SinglyLinkedListStack<T>
|
||||
{
|
||||
class SinglyLinkedListQueue : public LinkedList<T> {
|
||||
public:
|
||||
SinglyLinkedListQueue() : LinkedList<T>() {}
|
||||
|
||||
virtual Element<T>* push(const T& value) override {
|
||||
Element<T>* newElement = new Element<T>(value);
|
||||
if (this->head == nullptr) {
|
||||
this->head = this->tail = newElement;
|
||||
}
|
||||
else {
|
||||
this->tail->setNext(newElement);
|
||||
this->tail = newElement;
|
||||
}
|
||||
this->count++;
|
||||
return newElement;
|
||||
}
|
||||
|
||||
virtual Element<T>* pop() override {
|
||||
if (this->head == nullptr)
|
||||
return nullptr;
|
||||
|
||||
Element<T>* res = this->head;
|
||||
|
||||
if (this->head == this->tail) {
|
||||
this->head = this->tail = nullptr;
|
||||
}
|
||||
else {
|
||||
this->head = this->head->getNext();
|
||||
}
|
||||
|
||||
this->count--;
|
||||
return res;
|
||||
}
|
||||
|
||||
virtual ~SinglyLinkedListQueue() { cout << "\nSinglyLinkedListQueue class destructor\n"; }
|
||||
};
|
||||
|
||||
template<class T>
|
||||
class StackQueue : protected SinglyLinkedListStack<T>, protected SinglyLinkedListQueue<T> {
|
||||
public:
|
||||
StackQueue() : SinglyLinkedListStack<T>(), SinglyLinkedListQueue<T>() {}
|
||||
|
||||
Element<T>* push_back(const T& value) { return SinglyLinkedListQueue<T>::push(value); }
|
||||
Element<T>* push_front(const T& value) { return SinglyLinkedListStack<T>::push(value); }
|
||||
Element<T>* pop_back() { return SinglyLinkedListStack<T>::pop(); }
|
||||
Element<T>* pop_front() { return SinglyLinkedListQueue<T>::pop(); }
|
||||
|
||||
using LinkedList<T>::isEmpty;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
class DoublyLinkedListStack : public SinglyLinkedListStack<T> {
|
||||
public:
|
||||
DoublyLinkedListStack() : SinglyLinkedListStack<T>() {}
|
||||
|
||||
@ -156,7 +182,7 @@ public:
|
||||
}
|
||||
else {
|
||||
this->tail->setNext(newElement);
|
||||
newElement->setPrev(this->tail);
|
||||
newElement->setPrev(this->tail); // Установка prev для нового элемента
|
||||
this->tail = newElement;
|
||||
}
|
||||
this->count++;
|
||||
@ -175,7 +201,7 @@ public:
|
||||
else {
|
||||
this->tail = this->tail->getPrev();
|
||||
if (this->tail) {
|
||||
this->tail->setNext(nullptr);
|
||||
this->tail->setNext(nullptr); // Удаление ссылки на старый tail
|
||||
}
|
||||
}
|
||||
|
||||
@ -183,163 +209,27 @@ public:
|
||||
return res;
|
||||
}
|
||||
|
||||
Element<T>*insert(const T & value, int position) {
|
||||
if (position < 0 || position > this->count) {
|
||||
throw out_of_range("Index out of range");
|
||||
}
|
||||
virtual ~DoublyLinkedListStack() { cout << "\nDoublyLinkedListStack class destructor\n"; }
|
||||
};
|
||||
|
||||
Element<T>* newElement = new Element<T>(value);
|
||||
int main() {
|
||||
DoublyLinkedListStack<int> doublyStack;
|
||||
|
||||
if (position == 0) {
|
||||
newElement->setNext(this->head);
|
||||
if (this->head != nullptr) {
|
||||
this->head->setPrev(newElement);
|
||||
}
|
||||
this->head = newElement;
|
||||
if (this->count == 0) {
|
||||
this->tail = newElement;
|
||||
}
|
||||
}
|
||||
else {
|
||||
Element<T>* current = this->head;
|
||||
for (int i = 0; i < position - 1; ++i) {
|
||||
current = current->getNext();
|
||||
}
|
||||
newElement->setNext(current->getNext());
|
||||
newElement->setPrev(current);
|
||||
if (current->getNext() != nullptr) {
|
||||
current->getNext()->setPrev(newElement);
|
||||
}
|
||||
current->setNext(newElement);
|
||||
if (newElement->getNext() == nullptr) {
|
||||
this->tail = newElement;
|
||||
}
|
||||
}
|
||||
this->count++;
|
||||
return newElement;
|
||||
// Добавление элементов в стек
|
||||
doublyStack.push(1);
|
||||
doublyStack.push(2);
|
||||
doublyStack.push(3);
|
||||
doublyStack.push(4);
|
||||
|
||||
// Удаление элементов из стека
|
||||
while (!doublyStack.isEmpty()) {
|
||||
Element<int>* elem = doublyStack.pop();
|
||||
if (elem) {
|
||||
cout << elem->getInfo() << " ";
|
||||
delete elem;
|
||||
}
|
||||
|
||||
bool remove(const T & value) {
|
||||
Element<T>* current = this->head;
|
||||
while (current != nullptr) {
|
||||
if (current->getInfo() == value) {
|
||||
if (current == this->head) {
|
||||
this->head = current->getNext();
|
||||
if (this->head != nullptr) {
|
||||
this->head->setPrev(nullptr);
|
||||
}
|
||||
}
|
||||
else if (current == this->tail) {
|
||||
this->tail = current->getPrev();
|
||||
if (this->tail != nullptr) {
|
||||
this->tail->setNext(nullptr);
|
||||
}
|
||||
}
|
||||
else {
|
||||
current->getPrev()->setNext(current->getNext());
|
||||
current->getNext()->setPrev(current->getPrev());
|
||||
}
|
||||
delete current;
|
||||
this->count--;
|
||||
return true;
|
||||
}
|
||||
current = current->getNext();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Element<T>* find(const T & value) {
|
||||
Element<T>* current = this->head;
|
||||
while (current != nullptr) {
|
||||
if (current->getInfo() == value) {
|
||||
return current;
|
||||
}
|
||||
current = current->getNext();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
LinkedList<T> filter(const T & value, char condition) {
|
||||
LinkedList<T> filteredList;
|
||||
Element<T>* current = this->head;
|
||||
while (current != nullptr) {
|
||||
if (condition == '>' && current->getInfo() > value) {
|
||||
filteredList.push(current->getInfo());
|
||||
}
|
||||
else if (condition == '<' && current->getInfo() < value) {
|
||||
filteredList.push(current->getInfo());
|
||||
}
|
||||
current = current->getNext();
|
||||
}
|
||||
return filteredList;
|
||||
}
|
||||
|
||||
|
||||
Element<T>* search(const T & value) {
|
||||
Element<T>* current = this->head;
|
||||
while (current != nullptr) {
|
||||
if (current->getInfo() == value) {
|
||||
return current;
|
||||
}
|
||||
current = current->getNext();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
virtual ~DoublyLinkedListStack() { cout << "\nDoublyLinkedListStack class destructor\n"; }
|
||||
};
|
||||
|
||||
int main() {
|
||||
DoublyLinkedListStack<int> doublyStack;
|
||||
|
||||
// Добавление элементов в стек
|
||||
doublyStack.push(1);
|
||||
doublyStack.push(2);
|
||||
doublyStack.push(3);
|
||||
doublyStack.push(4);
|
||||
|
||||
// Вывод элементов с помощью перегруженного оператора <<
|
||||
cout << doublyStack << endl;
|
||||
|
||||
// Доступ к элементам через перегруженный оператор []
|
||||
try {
|
||||
cout << "Element at index 2: " << doublyStack[2].getInfo() << endl;
|
||||
}
|
||||
catch (const out_of_range& e) {
|
||||
cout << e.what() << endl;
|
||||
}
|
||||
|
||||
// Вставка элемента
|
||||
doublyStack.insert(5, 2);
|
||||
cout << doublyStack << endl;
|
||||
|
||||
// Поиск элемента
|
||||
Element<int>* foundElement = doublyStack.find(3);
|
||||
if (foundElement) {
|
||||
cout << "Found element: " << foundElement->getInfo() << endl;
|
||||
}
|
||||
else {
|
||||
cout << "Element not found" << endl;
|
||||
}
|
||||
|
||||
// Удаление элемента
|
||||
if (doublyStack.remove(2)) {
|
||||
cout << "Element removed successfully" << endl;
|
||||
}
|
||||
else {
|
||||
cout << "Element not found" << endl;
|
||||
}
|
||||
cout << doublyStack << endl;
|
||||
|
||||
// Удаление элементов из стека
|
||||
while (!doublyStack.isEmpty()) {
|
||||
Element<int>* elem = doublyStack.pop();
|
||||
if (elem) {
|
||||
cout << elem->getInfo() << " ";
|
||||
delete elem;
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user