#include <iostream>
#include <format>
namespace Phyll {
template <typename T> class LinkedList {
private:
struct Node {
T data;
Node *prev, *next;
explicit Node(const T& data, Node* prev = nullptr, Node* Next = nullptr)
: data{data}, prev{prev}, next{next} {}
};
Node* head;
public:
LinkedList() : head{nullptr} {}
Node* addToEmpty(const T& data) {
if (head == nullptr) {
head = new (std::nothrow) Node{data};
if (head == nullptr)
return nullptr;
return head;
}
return nullptr;
}
Node* addToEnd(const T& data) {
Node* temp{new (std::nothrow) Node{data}};
if (temp == nullptr)
return nullptr;
if (head == nullptr) {
head = temp;
return head;
}
Node* tp{head};
while (tp != nullptr && tp->next != nullptr)
tp = tp->next;
if (tp == nullptr)
head = temp;
else {
tp->next = temp;
temp->prev = tp;
}
return head;
}
Node* createList() {
int n;
std::cout << "Enter the number of nodes: ";
std::cin >> n;
if (n == 0)
return head;
std::cout << "Enter the element for the node 1: ";
int data;
std::cin >> data;
head = addToEmpty(data);
Node* lastNode{head};
for(int i = 1; i < n; i++) {
std::cout << std::format("Enter the element for the Node {}: ", i+1) << std::flush;
std::cin >> data;
lastNode = addToEnd(data);
}
return head;
}
void print() const {
Node* temp{head};
std::cout << "nullptr";
while (temp != nullptr) {
std::cout << std::format(" <- [prev][{}][next] -> ", temp->data);
temp = temp->next;
}
std::cout << "nullptr\n";
}
~LinkedList() {
while (head != nullptr) {
const Node* current{head};
head = head->next;
delete current;
}
}
};
}
int main() {
Phyll::LinkedList<int> list;
list.createList();
list.print();
}
a little help pls