//结点类型
typedef int ElemType
{
ElemType data;
struct Node *prior;
struct Node *next;
}DList;
//初始化
void InitDList(DList *head)
{
if(head == NULL) exit(0);
head->next = head->prior = head;
}
//创建新结点
static DList *_ApplyNode(ElemType val , DList *prior , DList *next)
{
DList *s = (DList*)malloc(sizeof(DList));
if(s == NULL) return NULL;
s->data = val;
s->prior = prior;
s->next = next;
return s;
}
//按位置插
void InterDListPos(DList *head , ElemType val , int pos)
{
if(head == NULL) exit(0);
while(pos < 0) return;
DList *p = head;
while(pos && p != NULL)
{
p=p->next;
pos--;
}
if(p = NULL) return;
DList *s = _ApplyNode(val , p ,p->next);
if(s == NULL) return;
if(p->next != NULL)
p->next->prior = s;
p->next = s;
}
//Show
void ShowDList(DList *head)
{
if(head == NULL) exit(0);
while(p->next != NULL)
{
printf("%d-->",p->next->data);
p = p->next;
}
printf("NULL\n");
while(p != head)
{
printf("%d-->",p->data);
p = p->prior;
}
}
//头插
void InterDListHead(DList * head , ElemType val)
{
InterDListPos(head , val , 0);
}
//尾插
void InterDListTail(DList * head , ElemType val)
{
if(head == NULL) exit(0);
while(p->next != NULL)
{
p=p->next;
}
if(p->next == NULL)
DList *s = _ApplyNode(val , p ,NULL);
}
//按位置删除
void DeleteDListPos(DList *head , int pos)
{
if(head == NULL) exit(0);
while(pos <0) return NULL;
DList *p = head;
while(pos == 0)
{
}
while(pos && p->next != NULL)
{
p = p->next;
pos--;
}
if(p->next == NULL) return;
DList *q = p->next;
p->next = q->next;
if(p->next != NULL) q->next->prior = p;
free(q);
}
//头删
void DeleteDListHead(DList *head )
{
DeleteDListPos (head , 0);
}
//尾删
void DeleteDListTail(DList *head)
{
if(head == NULL) exit(0);
DList *p = head;
while(p->next != NULL)
{
p=p->next;
}
if(p->next == NULL)
p->next->prior =NULL;
free(p);
}
//销毁
void DestroyDList(DList *head)
{
if(head == NULL) exit(0);
while(head->next != NULL)
{
DeleteDListHead(head);
}
}