// Example program
#include<iostream>
#include<memory>
#include<initializer_list>
class circlular_double_linked_list {
struct node{
int data;
node* prev;
std::unique_ptr<node> next;
node(int data, node* prev): data{data}, prev{prev} {}
~node(){
std::cout<<"Deleting node with data: "<<data<<"\n";
}
};
node* head;
public:
circlular_double_linked_list(std::initializer_list<int> list){
if (list.size()==0){
head=nullptr;
return;
}
auto it = list.begin();
head = new node(*it, nullptr);
auto curr_node = head;
it++;
while(it != list.end()){
curr_node->next = std::make_unique<node>(*it, curr_node);
curr_node = curr_node->next.get();
it++;
}
curr_node->next = std::unique_ptr<node>(head);
head->prev = curr_node;
}
~circlular_double_linked_list(){
head->next.reset(nullptr);
}
void add_to_tail(int x){
auto& tail = head->prev;
tail->next.release();//give up ownership of head
tail->next = std::make_unique<node>(x, tail);
tail->next->next = std::unique_ptr<node>(head);//gain ownership of head
tail=tail->next.get();
}
void delete_tail(){
auto tail = head->prev;
auto newtail = tail->prev;
tail->next.release(); //give up ownership of head
newtail->next.reset(head); //gain ownership of head and delete tail
head -> prev = newtail;
}
};
int main(){
circlular_double_linked_list cdll{1,2,3,4,5,6,7,8,9};
cdll.add_to_tail(10);
cdll.add_to_tail(11);
cdll.delete_tail();
}