fix build errors

This commit is contained in:
gandc 2024-05-20 04:02:53 +03:00
parent 4009d8c9f4
commit aa166244eb
Signed by: gandc
GPG Key ID: 9F77B03D43C42CB4

44
pr6.cpp
View File

@ -18,6 +18,18 @@ public:
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;
};
@ -40,13 +52,13 @@ public:
virtual Element<T>* pop() = 0;
virtual Element<T>* push(const T& value) = 0;
virtual bool isEmpty() { return count == 0; }
bool isEmpty() { return count == 0; }
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;
}
@ -66,7 +78,7 @@ public:
this->head = this->tail = newElement;
}
else {
this->tail->next = newElement;
this->tail->setNext(newElement);
this->tail = newElement;
}
this->count++;
@ -84,11 +96,11 @@ public:
}
else {
Element<T>* current = this->head;
while (current->next != this->tail) {
current = current->next;
while (current->getNext() != this->tail) {
current = current->getNext();
}
this->tail = current;
current->next = nullptr;
current->setNext(nullptr);
}
this->count--;
@ -109,7 +121,7 @@ public:
this->head = this->tail = newElement;
}
else {
this->tail->next = newElement;
this->tail->setNext(newElement);
this->tail = newElement;
}
this->count++;
@ -126,7 +138,7 @@ public:
this->head = this->tail = nullptr;
}
else {
this->head = this->head->next;
this->head = this->head->getNext();
}
this->count--;
@ -141,10 +153,12 @@ class StackQueue : protected SinglyLinkedListStack<T>, protected SinglyLinkedLis
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(); }
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() {
@ -158,7 +172,11 @@ int main() {
// Удаление элементов с начала и конца списка
while (!stackQueue.isEmpty()) {
cout << *(stackQueue.pop_back()) << " ";
Element<int>* elem = stackQueue.pop_back();
if (elem) {
cout << elem->getInfo() << " ";
delete elem;
}
}
cout << endl;