This commit is contained in:
gandc 2024-05-20 03:56:15 +03:00
parent 8c084709c8
commit 4009d8c9f4
Signed by: gandc
GPG Key ID: 9F77B03D43C42CB4

157
pr6.cpp
View File

@ -1,18 +1,19 @@
#include <iostream>
#include <stdexcept>
using namespace std;
template<class T>
class Element
{
class Element {
protected:
Element* next;
Element* prev;
T info;
public:
Element(T data) : next(nullptr), prev(nullptr), info(data) {}
Element(Element* Next, Element* Prev, T data) : next(Next), prev(Prev), info(data) {}
Element(const Element& el) : next(el.next), prev(el.prev), info(el.info) {}
Element() : next(nullptr), info(T()) {}
Element(const T& data) : next(nullptr), info(data) {}
Element(Element* Next, const T& data) : next(Next), info(data) {}
Element(const Element& el) : next(el.next), info(el.info) {}
template<class T1>
friend ostream& operator<<(ostream& s, const Element<T1>& el);
@ -21,15 +22,13 @@ public:
};
template<class T1>
ostream& operator<<(ostream& s, const Element<T1>& el)
{
ostream& operator<<(ostream& s, const Element<T1>& el) {
s << el.info;
return s;
}
template<class T>
class LinkedList
{
class LinkedList {
protected:
Element<T>* head;
Element<T>* tail;
@ -39,17 +38,12 @@ public:
LinkedList() : head(nullptr), tail(nullptr), count(0) {}
virtual Element<T>* pop() = 0;
virtual Element<T>* push(T value) = 0;
virtual Element<T>& operator[](int index) = 0;
virtual Element<T>* push(const T& value) = 0;
virtual bool isEmpty() { return count == 0; }
template<class T1>
friend ostream& operator<<(ostream& s, const LinkedList<T1>& list);
virtual ~LinkedList()
{
cout << "\nBase class destructor";
virtual ~LinkedList() {
cout << "\nBase class destructor\n";
Element<T>* current = head;
while (current != nullptr) {
Element<T>* nextElement = current->next;
@ -61,46 +55,25 @@ public:
}
};
template<class T1>
ostream& operator<<(ostream& s, const LinkedList<T1>& list)
{
Element<T1>* current = list.head;
while (current != nullptr) {
s << *current << " ";
current = current->next;
}
return s;
}
template<class T, int N = 20>
class Stack : public LinkedList<T>
{
template<class T>
class SinglyLinkedListStack : public LinkedList<T> {
public:
Stack() : LinkedList<T>()
{
if (N > 0) {
for (int i = 0; i < N; i++)
this->push(0);
}
}
SinglyLinkedListStack() : LinkedList<T>() {}
virtual Element<T>* push(T value)
{
if (this->head == nullptr) { // if(count==0)
this->tail = new Element<T>(value);
this->head = this->tail;
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->next = new Element<T>(value);
this->tail->next->prev = this->tail;
this->tail = this->tail->next;
this->tail->next = newElement;
this->tail = newElement;
}
this->count++;
return this->tail;
return newElement;
}
virtual Element<T>* pop()
{
virtual Element<T>* pop() override {
if (this->tail == nullptr)
return nullptr;
@ -110,28 +83,84 @@ public:
this->head = this->tail = nullptr;
}
else {
this->tail = this->tail->prev;
this->tail->next = nullptr;
Element<T>* current = this->head;
while (current->next != this->tail) {
current = current->next;
}
this->tail = current;
current->next = nullptr;
}
this->count--;
return res;
}
virtual ~Stack() { cout << "\nStack class destructor"; }
virtual ~SinglyLinkedListStack() { cout << "\nSinglyLinkedListStack class destructor\n"; }
};
int main()
{
if (true)
{
Stack<double, 20> S;
for (int i = 0; i < 10; i++)
S.push(i);
// S.insert(3.5, S.head->next->next->next); // insert method not defined in the provided code
cout << S;
cout << "\n";
// cout<<S.Find_R(5.5, S.head); // Find_R method not defined in the provided code
template<class 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->next = 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->next;
}
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 this->push(value); }
Element<T>* push_front(const T& value) { return this->push(value); }
Element<T>* pop_back() { return this->pop(); }
Element<T>* pop_front() { return this->pop(); }
};
int main() {
StackQueue<int> stackQueue;
// Добавление элементов в конец и начало списка
stackQueue.push_back(1);
stackQueue.push_back(2);
stackQueue.push_front(3);
stackQueue.push_front(4);
// Удаление элементов с начала и конца списка
while (!stackQueue.isEmpty()) {
cout << *(stackQueue.pop_back()) << " ";
}
cout << endl;
return 0;
}