#include <iostream>
#include <stdio.h>
//using namespace std;
typedef struct Node
{
int data;
struct Node* prev;
struct Node* next;
}node;
class d_link
{
private:
Node* head;
Node* tail;
int cnt;
public:
d_link()
{
head = nullptr;
tail = nullptr;
int cnt = 0;
std::cout<< "creat"<<std::endl;
std::cout<< cnt<<std::endl;
}
~d_link()
{
while(head != nullptr){
Node* temp = head->next;
delete head;
std::cout<< "destroy";
head = temp;
cnt--;
}
}
d_link* PushBack(int _data)
{
node* newNode = new node;
newNode->data = _data;
if(this->head == nullptr && this->tail == nullptr){
std::cout<< "10"<<std::endl;
head = newNode;
tail = newNode;
cnt = 0;
cnt++;
std::cout<< cnt<<std::endl;
return this;
}
else
{
std::cout<< "20"<<std::endl;
tail->next = newNode;
newNode->prev = tail;
this->tail = newNode;
std::cout<< cnt<<std::endl;
cnt++;
// tail = newNode;
std::cout<< cnt<<std::endl;
}
return this;
}
d_link* PushFront(int _data)
{
node* newNode = new node;
newNode->data = _data;
if(this->head == nullptr && this->tail == nullptr){
std::cout<< "30"<<std::endl;
head = newNode;
tail = newNode;
cnt = 0;
cnt++;
std::cout<< cnt<<std::endl;
return this;
}
else
{
std::cout<< "40"<<std::endl;
head->prev = newNode;
newNode->next = head;
this->head = newNode;
std::cout<< cnt<<std::endl;
cnt++;
//tail = newNode;
std::cout<< cnt<<std::endl;
}
return this;
}
int printnode()
{
while(head && head->next)
{
std::cout<< "ouptput ";
std::cout<< head->data<<std::endl;
head = head->next;
}
/*std::cout<< head->data<<std::endl;
std::cout<< head->next->data<<std::endl;
std::cout<< head->next->next->data<<std::endl;
std::cout<< head->next->next->next->data<<std::endl;
// head = head->next; */
return 0;
}
};
int main(){
d_link* a;
a = new d_link;
a->PushBack(6);
a->PushBack(2);
a->PushBack(3);
a->PushBack(4);
a->PushBack(5);
a->PushFront(1);
a->PushFront(2);
a->printnode();
a->~d_link();
return 0;
}