/* I am too lazy,even I didn't use Chinese to describe. */
/************************************************
* Double-Direction Link-Table Model 1.0
* KangQiao 2010.7.6 14:24
* Free code,no copyright.
* E-Mail:[email protected]
* Website:http://hi.baidu.com/zunhuakq
* If this version has any bug,please contact me.
***********************************************/
/* Edit the include if you need. */
#include <stdlib.h>
typedef struct item
{
/* your data structure here*/
struct item *b;
struct item *f;
}ITEM;
ITEM *head=NULL,*tail=NULL;
void append(ITEM *i)
{
if(tail) /* non-empty table */
tail->f=i;
else /* empty table */
head=i;
i->b=tail;
i->f=NULL;
tail=i;
}
void ins(ITEM *a,ITEM *b,ITEM *i)
/*
Instruction:
This function will insert between a and b,so the item b should be next to a,must be that order!
*/
{
if(a==NULL && b==NULL) /* empty table */
{
head=tail=i;
}
else if(a==NULL) /* insert before head */
{
head->b=i;
head=i;
}
else if(b==NULL) /* insert after tail */
{
tail->f=i;
tail=i;
}
else /* normal */
{
a->f=i;
b->b=i;
}
i->b=a;
i->f=b;
}
void del(ITEM *i)
{
if(i->b==NULL && i->f==NULL) /* only one item */
{
head=tail=NULL;
}
else if(i->b==NULL) /* remove head */
{
head=head->f;
head->b=NULL;
}
else if(i->f==NULL) /* remove tail */
{
tail=tail->b;
tail->f=NULL;
}
else /* normal */
{
i->b->f=i->f;
i->f->b=i->b;
}
free(i);
}
void rpl(ITEM *a,ITEM *b) /* use b instead a */
{
if(a->b==NULL && a->f==NULL)
{
head=tail=b;
}
else if(a->b==NULL) /* a is the head */
{
head=b;
a->f->b=b;
}
else if(a->f==NULL) /* a is the tail */
{
tail=b;
a->b->f=b;
}
else /* normal */
{
a->b->f=b;
a->f->b=b;
}
b->b=a->b;
b->f=a->f;
free(a);
}