Я реализую свою собственную версию очереди для практики, но мне интересно, правильно ли я удаляю элементы, в функции "pop ()".
Я новичок в C ++, и я я не уверен, что я просто удаляю указатель на узел, который пытаюсь удалить, вместо самого фактического узла.
#include <iostream>
template <typename T>
class Queue {
struct Node {
Node* next;
Node* previous;
T data;
Node(T value){
next = nullptr;
previous = nullptr;
data = value;
}
};
Node* front;
Node* back;
public:
Queue(){
front = nullptr;
back = nullptr;
}
void push_back(T data){
Node* n = new Node(data);
if(front == nullptr){
front = n;
back = n;
} else {
back->next = n;
back = n;
}
}
void print(){
Node* cursor = front;
while(cursor != nullptr){
std::cout << cursor->data << '\n';
cursor = cursor->next;
}
}
T pop(){
Node* temp = front;
T element = temp->data;
front = front->next;
delete temp; // Is this deleting the pointer or the Node it points to?
return element;
}
};
int main(){
Queue<int> q;
q.push_back(1);
q.push_back(2);
q.push_back(3);
q.print();
int element = q.pop();
q.print();
}