class Node{
public:
int data;
Node(int data){
this->data = data;
}
public:
shared_ptr<Node> prev;
shared_ptr<Node> next;
};
class LinkedList{
public:
shared_ptr<Node> head;
shared_ptr<Node> tail;
public:
LinkedList(){}
void fun(){
printf("fun()\n");
}
void append(shared_ptr<Node> node){
if(!head){
head = node;
tail = head;
}else{
tail->next = node;
node->prev = tail;
tail = node;
}
}
// overload ostream
friend std::ostream& operator<<(std::ostream& out, shared_ptr<LinkedList> list){
shared_ptr<Node> curr = list->head;
while(curr){
out<<'['<<curr->data<<']'<<'\n';
curr = curr->next;
}
return out;
}
void print(){
shared_ptr<Node> curr = head;
while(curr){
printf("[%d]\n", curr->data);
curr = curr->next;
}
}
bool remove(shared_ptr<Node> node){
bool ret = false;
shared_ptr<Node> curr = head;
if(node && curr){
while(curr){
if(curr == node){
if(curr->prev == nullptr){
if(curr->next == nullptr){
// [curr]
node.reset();
head.reset();
tail.reset();
}else{
// [curr]->[]
shared_ptr<Node> next = curr->next;
curr->next.reset();
next->prev.reset();
head = next;
}
}else{
if(curr->next == nullptr){
// []->[curr]
shared_ptr<Node> prev = curr->prev;
curr->prev.reset();
prev->next.reset();
tail = prev;
}else{
// []->[curr]->[]
shared_ptr<Node> next = curr->next;
shared_ptr<Node> prev = curr->prev;
next->prev = prev;
prev->next = next;
}
}
ret = true;
}
curr = curr->next;
}
}
return ret;
}
};