[size=large]双向循环链表[/size]
struct node
{
int id;
int data;
int status;
struct node *up;
struct node *next;
};
class DoubleLink
{
public:
DoubleLink();
~DoubleLink();
node* up();
node* next();
node* getCurrent();
int getSize();
node* addNode(int size);
void deleteNode(int size); // 删除size个节点
void deleteLink(); // 删除整条
void show();
void showDesc();
private:
node* current;
void creatLink();
};
DoubleLink::DoubleLink()
{
creatLink();
}
DoubleLink::~DoubleLink()
{
if (current != NULL)
deleteLink();
}
void DoubleLink::creatLink()
{
current = (node *)malloc(sizeof(node));
current->up = current;
current->next = current;
current->id = 0;
current->data = 0;
current->status = 0;
}
void DoubleLink::deleteLink()
{
current->up->next = NULL;
while (current != NULL)
{
node *p = current->next;
free(current);
current = p;
}
current = NULL;
}
void DoubleLink::deleteNode(int size)
{
if (getSize() <= size) deleteLink();
node *beg = current->up;
for (int i = 0; i < size; i++)
{
node *p = current->next;
free(current);
current = p;
}
current->up = beg;
beg->next = current;
}
node* DoubleLink::addNode(int size)
{
node *beg = current;
node *end = current->next;
for (int i = 0; i < size; i++)
{
node *mid = (node *)malloc(sizeof(node));
beg->next = mid;
mid->up = beg;
beg = mid;
}
beg->next = end;
end->up = beg;
return current;
}
int DoubleLink::getSize()
{
int size = 1;
node *p = current->next;
while (p != current)
{
p = p->next;
size++;
}
return size;
}
void DoubleLink::show()
{
cout << "DoubleLink Show :" << endl;
cout << "第0个: " << current->data << endl;
node *p = current;
p = p->next;
for (int i = 1; p != current; i++)
{
cout << "第"<< i << "个: " << p->data << endl;
p = p->next;
}
}
void DoubleLink::showDesc()
{
cout << "DoubleLink Show Desc :" << endl;
cout << "第0个: " << current->data << endl;
node *p = current;
p = p->up;
for (int i = 1; p != current; i++)
{
cout << "第"<< i << "个: " << p->data << endl;
p = p->up;
}
}
node* DoubleLink::up()
{
return current->up;
}
node* DoubleLink::getCurrent()
{
return current;
}
node* DoubleLink::next()
{
return current->next;
}