Compare commits
2
Commits
aa166244eb
...
776f865526
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
776f865526
|
||
|
|
f824162a40
|
@@ -7,13 +7,14 @@ template<class T>
|
|||||||
class Element {
|
class Element {
|
||||||
protected:
|
protected:
|
||||||
Element* next;
|
Element* next;
|
||||||
|
Element* prev;
|
||||||
T info;
|
T info;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Element() : next(nullptr), info(T()) {}
|
Element() : next(nullptr), prev(nullptr), info(T()) {}
|
||||||
Element(const T& data) : next(nullptr), info(data) {}
|
Element(const T& data) : next(nullptr), prev(nullptr), info(data) {}
|
||||||
Element(Element* Next, const T& data) : next(Next), info(data) {}
|
Element(Element* Next, Element* Prev, const T& data) : next(Next), prev(Prev), info(data) {}
|
||||||
Element(const Element& el) : next(el.next), info(el.info) {}
|
Element(const Element& el) : next(el.next), prev(el.prev), info(el.info) {}
|
||||||
|
|
||||||
template<class T1>
|
template<class T1>
|
||||||
friend ostream& operator<<(ostream& s, const Element<T1>& el);
|
friend ostream& operator<<(ostream& s, const Element<T1>& el);
|
||||||
@@ -26,10 +27,18 @@ public:
|
|||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Element* getPrev() const {
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
|
||||||
void setNext(Element* nextElement) {
|
void setNext(Element* nextElement) {
|
||||||
next = nextElement;
|
next = nextElement;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setPrev(Element* prevElement) {
|
||||||
|
prev = prevElement;
|
||||||
|
}
|
||||||
|
|
||||||
template<class T1> friend class LinkedList;
|
template<class T1> friend class LinkedList;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -65,8 +74,32 @@ public:
|
|||||||
head = tail = nullptr;
|
head = tail = nullptr;
|
||||||
count = 0;
|
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>
|
template<class T>
|
||||||
class SinglyLinkedListStack : public LinkedList<T> {
|
class SinglyLinkedListStack : public LinkedList<T> {
|
||||||
public:
|
public:
|
||||||
@@ -111,9 +144,10 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
class SinglyLinkedListQueue : public LinkedList<T> {
|
class DoublyLinkedListStack : public SinglyLinkedListStack<T>
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
SinglyLinkedListQueue() : LinkedList<T>() {}
|
DoublyLinkedListStack() : SinglyLinkedListStack<T>() {}
|
||||||
|
|
||||||
virtual Element<T>* push(const T& value) override {
|
virtual Element<T>* push(const T& value) override {
|
||||||
Element<T>* newElement = new Element<T>(value);
|
Element<T>* newElement = new Element<T>(value);
|
||||||
@@ -122,6 +156,7 @@ public:
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this->tail->setNext(newElement);
|
this->tail->setNext(newElement);
|
||||||
|
newElement->setPrev(this->tail);
|
||||||
this->tail = newElement;
|
this->tail = newElement;
|
||||||
}
|
}
|
||||||
this->count++;
|
this->count++;
|
||||||
@@ -129,50 +164,176 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
virtual Element<T>* pop() override {
|
virtual Element<T>* pop() override {
|
||||||
if (this->head == nullptr)
|
if (this->tail == nullptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
Element<T>* res = this->head;
|
Element<T>* res = this->tail;
|
||||||
|
|
||||||
if (this->head == this->tail) {
|
if (this->head == this->tail) {
|
||||||
this->head = this->tail = nullptr;
|
this->head = this->tail = nullptr;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this->head = this->head->getNext();
|
this->tail = this->tail->getPrev();
|
||||||
|
if (this->tail) {
|
||||||
|
this->tail->setNext(nullptr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this->count--;
|
this->count--;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~SinglyLinkedListQueue() { cout << "\nSinglyLinkedListQueue class destructor\n"; }
|
Element<T>*insert(const T & value, int position) {
|
||||||
};
|
if (position < 0 || position > this->count) {
|
||||||
|
throw out_of_range("Index out of range");
|
||||||
|
}
|
||||||
|
|
||||||
template<class T>
|
Element<T>* newElement = new Element<T>(value);
|
||||||
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); }
|
if (position == 0) {
|
||||||
Element<T>* push_front(const T& value) { return SinglyLinkedListStack<T>::push(value); }
|
newElement->setNext(this->head);
|
||||||
Element<T>* pop_back() { return SinglyLinkedListStack<T>::pop(); }
|
if (this->head != nullptr) {
|
||||||
Element<T>* pop_front() { return SinglyLinkedListQueue<T>::pop(); }
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
using LinkedList<T>::isEmpty;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
Element<T>* find(const T & value) {
|
||||||
StackQueue<int> stackQueue;
|
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) {
|
||||||
stackQueue.push_back(1);
|
LinkedList<T> filteredList;
|
||||||
stackQueue.push_back(2);
|
Element<T>* current = this->head;
|
||||||
stackQueue.push_front(3);
|
while (current != nullptr) {
|
||||||
stackQueue.push_front(4);
|
if (condition == '>' && current->getInfo() > value) {
|
||||||
|
filteredList.push(current->getInfo());
|
||||||
|
}
|
||||||
|
else if (condition == '<' && current->getInfo() < value) {
|
||||||
|
filteredList.push(current->getInfo());
|
||||||
|
}
|
||||||
|
current = current->getNext();
|
||||||
|
}
|
||||||
|
return filteredList;
|
||||||
|
}
|
||||||
|
|
||||||
// Удаление элементов с начала и конца списка
|
|
||||||
while (!stackQueue.isEmpty()) {
|
Element<T>* search(const T & value) {
|
||||||
Element<int>* elem = stackQueue.pop_back();
|
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) {
|
if (elem) {
|
||||||
cout << elem->getInfo() << " ";
|
cout << elem->getInfo() << " ";
|
||||||
delete elem;
|
delete elem;
|
||||||
@@ -181,4 +342,4 @@ int main() {
|
|||||||
cout << endl;
|
cout << endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user