Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa166244eb
|
||
|
|
4009d8c9f4
|
@@ -1,35 +1,46 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
class Element
|
class Element {
|
||||||
{
|
|
||||||
protected:
|
protected:
|
||||||
Element* next;
|
Element* next;
|
||||||
Element* prev;
|
|
||||||
T info;
|
T info;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Element(T data) : next(nullptr), prev(nullptr), info(data) {}
|
Element() : next(nullptr), info(T()) {}
|
||||||
Element(Element* Next, Element* Prev, T data) : next(Next), prev(Prev), info(data) {}
|
Element(const T& data) : next(nullptr), info(data) {}
|
||||||
Element(const Element& el) : next(el.next), prev(el.prev), info(el.info) {}
|
Element(Element* Next, const T& data) : next(Next), info(data) {}
|
||||||
|
Element(const Element& el) : next(el.next), 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);
|
||||||
|
|
||||||
|
const T& getInfo() const {
|
||||||
|
return info;
|
||||||
|
}
|
||||||
|
|
||||||
|
Element* getNext() const {
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setNext(Element* nextElement) {
|
||||||
|
next = nextElement;
|
||||||
|
}
|
||||||
|
|
||||||
template<class T1> friend class LinkedList;
|
template<class T1> friend class LinkedList;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T1>
|
template<class T1>
|
||||||
ostream& operator<<(ostream& s, const Element<T1>& el)
|
ostream& operator<<(ostream& s, const Element<T1>& el) {
|
||||||
{
|
|
||||||
s << el.info;
|
s << el.info;
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
class LinkedList
|
class LinkedList {
|
||||||
{
|
|
||||||
protected:
|
protected:
|
||||||
Element<T>* head;
|
Element<T>* head;
|
||||||
Element<T>* tail;
|
Element<T>* tail;
|
||||||
@@ -39,20 +50,15 @@ public:
|
|||||||
LinkedList() : head(nullptr), tail(nullptr), count(0) {}
|
LinkedList() : head(nullptr), tail(nullptr), count(0) {}
|
||||||
|
|
||||||
virtual Element<T>* pop() = 0;
|
virtual Element<T>* pop() = 0;
|
||||||
virtual Element<T>* push(T value) = 0;
|
virtual Element<T>* push(const T& value) = 0;
|
||||||
virtual Element<T>& operator[](int index) = 0;
|
|
||||||
|
|
||||||
virtual bool isEmpty() { return count == 0; }
|
bool isEmpty() { return count == 0; }
|
||||||
|
|
||||||
template<class T1>
|
virtual ~LinkedList() {
|
||||||
friend ostream& operator<<(ostream& s, const LinkedList<T1>& list);
|
cout << "\nBase class destructor\n";
|
||||||
|
|
||||||
virtual ~LinkedList()
|
|
||||||
{
|
|
||||||
cout << "\nBase class destructor";
|
|
||||||
Element<T>* current = head;
|
Element<T>* current = head;
|
||||||
while (current != nullptr) {
|
while (current != nullptr) {
|
||||||
Element<T>* nextElement = current->next;
|
Element<T>* nextElement = current->getNext();
|
||||||
delete current;
|
delete current;
|
||||||
current = nextElement;
|
current = nextElement;
|
||||||
}
|
}
|
||||||
@@ -61,46 +67,25 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T1>
|
template<class T>
|
||||||
ostream& operator<<(ostream& s, const LinkedList<T1>& list)
|
class SinglyLinkedListStack : public LinkedList<T> {
|
||||||
{
|
|
||||||
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>
|
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
Stack() : LinkedList<T>()
|
SinglyLinkedListStack() : LinkedList<T>() {}
|
||||||
{
|
|
||||||
if (N > 0) {
|
|
||||||
for (int i = 0; i < N; i++)
|
|
||||||
this->push(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual Element<T>* push(T value)
|
virtual Element<T>* push(const T& value) override {
|
||||||
{
|
Element<T>* newElement = new Element<T>(value);
|
||||||
if (this->head == nullptr) { // if(count==0)
|
if (this->head == nullptr) {
|
||||||
this->tail = new Element<T>(value);
|
this->head = this->tail = newElement;
|
||||||
this->head = this->tail;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this->tail->next = new Element<T>(value);
|
this->tail->setNext(newElement);
|
||||||
this->tail->next->prev = this->tail;
|
this->tail = newElement;
|
||||||
this->tail = this->tail->next;
|
|
||||||
}
|
}
|
||||||
this->count++;
|
this->count++;
|
||||||
return this->tail;
|
return newElement;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual Element<T>* pop()
|
virtual Element<T>* pop() override {
|
||||||
{
|
|
||||||
if (this->tail == nullptr)
|
if (this->tail == nullptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
@@ -110,28 +95,90 @@ public:
|
|||||||
this->head = this->tail = nullptr;
|
this->head = this->tail = nullptr;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this->tail = this->tail->prev;
|
Element<T>* current = this->head;
|
||||||
this->tail->next = nullptr;
|
while (current->getNext() != this->tail) {
|
||||||
|
current = current->getNext();
|
||||||
|
}
|
||||||
|
this->tail = current;
|
||||||
|
current->setNext(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
this->count--;
|
this->count--;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~Stack() { cout << "\nStack class destructor"; }
|
virtual ~SinglyLinkedListStack() { cout << "\nSinglyLinkedListStack class destructor\n"; }
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
template<class T>
|
||||||
{
|
class SinglyLinkedListQueue : public LinkedList<T> {
|
||||||
if (true)
|
public:
|
||||||
{
|
SinglyLinkedListQueue() : LinkedList<T>() {}
|
||||||
Stack<double, 20> S;
|
|
||||||
for (int i = 0; i < 10; i++)
|
virtual Element<T>* push(const T& value) override {
|
||||||
S.push(i);
|
Element<T>* newElement = new Element<T>(value);
|
||||||
// S.insert(3.5, S.head->next->next->next); // insert method not defined in the provided code
|
if (this->head == nullptr) {
|
||||||
cout << S;
|
this->head = this->tail = newElement;
|
||||||
cout << "\n";
|
}
|
||||||
// cout<<S.Find_R(5.5, S.head); // Find_R method not defined in the provided code
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
StackQueue<int> stackQueue;
|
||||||
|
|
||||||
|
// Добавление элементов в конец и начало списка
|
||||||
|
stackQueue.push_back(1);
|
||||||
|
stackQueue.push_back(2);
|
||||||
|
stackQueue.push_front(3);
|
||||||
|
stackQueue.push_front(4);
|
||||||
|
|
||||||
|
// Удаление элементов с начала и конца списка
|
||||||
|
while (!stackQueue.isEmpty()) {
|
||||||
|
Element<int>* elem = stackQueue.pop_back();
|
||||||
|
if (elem) {
|
||||||
|
cout << elem->getInfo() << " ";
|
||||||
|
delete elem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user