Compare commits
2 Commits
8c084709c8
...
aa166244eb
| Author | SHA1 | Date | |
|---|---|---|---|
| aa166244eb | |||
| 4009d8c9f4 |
179
pr6.cpp
179
pr6.cpp
@ -1,35 +1,46 @@
|
||||
#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);
|
||||
|
||||
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>
|
||||
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,20 +50,15 @@ 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; }
|
||||
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;
|
||||
Element<T>* nextElement = current->getNext();
|
||||
delete current;
|
||||
current = nextElement;
|
||||
}
|
||||
@ -61,46 +67,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->setNext(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 +95,90 @@ public:
|
||||
this->head = this->tail = nullptr;
|
||||
}
|
||||
else {
|
||||
this->tail = this->tail->prev;
|
||||
this->tail->next = nullptr;
|
||||
Element<T>* current = this->head;
|
||||
while (current->getNext() != this->tail) {
|
||||
current = current->getNext();
|
||||
}
|
||||
this->tail = current;
|
||||
current->setNext(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->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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user