fix build errors

This commit is contained in:
gandc committed 2024-05-20 04:02:53 +03:00
1 parent 4009d8c9f4
commit aa166244eb
1 file changed
+31 -13
+31 -13
View File
@@ -18,6 +18,18 @@ public:
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;
}; };
@@ -40,13 +52,13 @@ public:
virtual Element<T>* pop() = 0; virtual Element<T>* pop() = 0;
virtual Element<T>* push(const T& value) = 0; virtual Element<T>* push(const T& value) = 0;
virtual bool isEmpty() { return count == 0; } bool isEmpty() { return count == 0; }
virtual ~LinkedList() { virtual ~LinkedList() {
cout << "\nBase class destructor\n"; cout << "\nBase class destructor\n";
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;
} }
@@ -66,7 +78,7 @@ public:
this->head = this->tail = newElement; this->head = this->tail = newElement;
} }
else { else {
this->tail->next = newElement; this->tail->setNext(newElement);
this->tail = newElement; this->tail = newElement;
} }
this->count++; this->count++;
@@ -84,11 +96,11 @@ public:
} }
else { else {
Element<T>* current = this->head; Element<T>* current = this->head;
while (current->next != this->tail) { while (current->getNext() != this->tail) {
current = current->next; current = current->getNext();
} }
this->tail = current; this->tail = current;
current->next = nullptr; current->setNext(nullptr);
} }
this->count--; this->count--;
@@ -109,7 +121,7 @@ public:
this->head = this->tail = newElement; this->head = this->tail = newElement;
} }
else { else {
this->tail->next = newElement; this->tail->setNext(newElement);
this->tail = newElement; this->tail = newElement;
} }
this->count++; this->count++;
@@ -126,7 +138,7 @@ public:
this->head = this->tail = nullptr; this->head = this->tail = nullptr;
} }
else { else {
this->head = this->head->next; this->head = this->head->getNext();
} }
this->count--; this->count--;
@@ -141,10 +153,12 @@ class StackQueue : protected SinglyLinkedListStack<T>, protected SinglyLinkedLis
public: public:
StackQueue() : SinglyLinkedListStack<T>(), SinglyLinkedListQueue<T>() {} StackQueue() : SinglyLinkedListStack<T>(), SinglyLinkedListQueue<T>() {}
Element<T>* push_back(const T& value) { return this->push(value); } Element<T>* push_back(const T& value) { return SinglyLinkedListQueue<T>::push(value); }
Element<T>* push_front(const T& value) { return this->push(value); } Element<T>* push_front(const T& value) { return SinglyLinkedListStack<T>::push(value); }
Element<T>* pop_back() { return this->pop(); } Element<T>* pop_back() { return SinglyLinkedListStack<T>::pop(); }
Element<T>* pop_front() { return this->pop(); } Element<T>* pop_front() { return SinglyLinkedListQueue<T>::pop(); }
using LinkedList<T>::isEmpty;
}; };
int main() { int main() {
@@ -158,7 +172,11 @@ int main() {
// Удаление элементов с начала и конца списка // Удаление элементов с начала и конца списка
while (!stackQueue.isEmpty()) { while (!stackQueue.isEmpty()) {
cout << *(stackQueue.pop_back()) << " "; Element<int>* elem = stackQueue.pop_back();
if (elem) {
cout << elem->getInfo() << " ";
delete elem;
}
} }
cout << endl; cout << endl;