#pragma once
using namespace std;
template<class ty>
struct node
{
node(node*pfront,ty t,node*ptail)
{
this->front = pfront;
this->t = t;
this->tail = ptail;
}
node()
{
this->front = this;
this->tail = this;
}
node(ty t)
{
this->front = this;
this->t = t;
this->tail = this;
}
node* front;
ty t;
node* tail;
};
template<class ty>
class list
{
public:
list()
{
this->headnode = nullptr;
}
void printfbytail()
{
node<ty>* pmove = this->headnode;
if (this->headnode == nullptr) return;
while (1)
{
cout << pmove->t << " ";
pmove = pmove->tail;
if (pmove == this->headnode->front)
{
cout << pmove->t << " ";
cout << endl;
return;
}
}
}
void printbyfront()
{
node<ty>* pmove = this->headnode->front;
if (this->headnode == nullptr) return;
while (1)
{
cout << pmove->t << " ";
pmove = pmove->front;
if (pmove == this->headnode)
{
cout << pmove->t << " ";
cout << endl;
return;
}
}
}
void push_front(ty t)
{
if (this->headnode == nullptr)
{
this->headnode = new node<ty>(t);
}
else
{
node<ty>* pmove = new node<ty>(t);
pmove->front = headnode->front;
pmove->tail = headnode;
headnode->front = pmove;
headnode = pmove;
}
}
void push_back(ty t)
{
if (this->headnode == nullptr)push_front(t);
else
{
node<ty>* pmove = new node<ty>(t);
pmove->front = this->headnode->front;
pmove->tail = headnode;
this->headnode->front->tail = pmove;
this->headnode->front = pmove;
}
}
void insert(int n,ty t)
{
if (headnode == nullptr) push_back(t);
node<ty>* pmove = this->headnode->tail;
node<ty>* pre = this->headnode;
node<ty>* pnew = new node<ty>(t);
for (int i = 0; i < n; i++)
{
pmove = pmove->tail;
pre = pre->tail;
}
pnew->tail = pmove;
pnew->front = pre;
pre->tail = pnew;
pmove->front = pnew;
}
void pop_front()
{
if (this->headnode == nullptr) return;
this->headnode->tail->front = this->headnode->front;
this->headnode->front->tail = this->headnode->tail;
headnode = this->headnode->tail;
}
void pop_back()
{
if (this->headnode == nullptr) return;
node<ty>* backnode = this->headnode->front;
this->headnode->front = backnode->front;
backnode->front->tail = this->headnode;
delete backnode;
}
void deletes(int n)
{
if (headnode == nullptr) return;
node<ty>* pmove = this->headnode->tail;
node<ty>* pre = this->headnode;
for (int i = 0; i < n; i++)
{
pmove = pmove->tail;
pre = pre->tail;
}
node<ty>* backnode = pmove->tail;
pre->tail = backnode;
backnode->front = pre;
delete pmove;
}
~list()
{
if (this->headnode != nullptr)
{
this->headnode = nullptr;
}
}
node<ty>* headnode;
};
双向循环链表
最新推荐文章于 2023-01-24 16:48:30 发布