#include <iostream>
struct Node {
int data;
Node* prev;
Node* next;
Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};
class DoublyLinkedList {
private:
Node* head;
Node* tail;
public:
DoublyLinkedList() : head(nullptr), tail(nullptr) {}
void push_back(int val) {
Node* newNode = new Node(val);
if (!head) {
head = tail = newNode;
return;
}
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
void pop_back() {
if (!tail) return;
if (head == tail) {
delete head;
head = tail = nullptr;
return;
}
Node* temp = tail;
tail = tail->prev;
tail->next = nullptr;
delete temp;
}
void print() {
Node* current = head;
while (current) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << "\n";
}
~DoublyLinkedList() {
while (head) {
Node* temp = head;
head = head->next;
delete temp;
}
}
};
int main() {
DoublyLinkedList list;
list.push_back(1);
list.push_back(2);
list.push_back(3);
list.print(); // 1 2 3
list.pop_back();
list.print(); // 1 2
return 0;
}