#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
int data;
struct node *next;
struct node *pre;
}Node;
Node *pHead=NULL;
void insert(Node *pNode)
{
if(NULL == pHead)
{
pHead = pNode;
pHead->next = pNode;
pHead->pre = pNode;
return;
}
/*插头法*/
pNode->next = pHead;
pNode->pre = pHead->pre;
pHead->pre->next = pNode;
pHead->pre = pNode;
pHead = pNode;
}
void del(int data)
{
Node *p;
if(NULL == pHead)
return;
p = pHead;
if(data == pHead->data)/*刚好为head*/
{
if(pHead == p->next)/*只有一个node*/
{
free(pHead);
pHead = NULL;
}
else
{
pHead = p->next;
p->next->pre = p->pre;
p->pre->next = p->next;
free(p);
}
return;
}
p = pHead->pre;
do
{
if(data == p->data)
{
break;
}
p = p->pre;
}
while(pHead->pre != p);
p->next->pre = p->pre;
p->pre->next = p->next;
free(p);
return ;
}
void display(void )
{
Node *p;
p = pHead->pre;
do
{
printf("%d ", p->data);
p = p->pre;
}
while(pHead->pre != p);
printf("\n\r");
}
int main(void )
{
int i;
Node *p;
for(i=0; i<100; i++)
{
p = (Node *)malloc(sizeof(Node));
p->data = i;
insert(p);
}
display();
for(i=30; i<90; i++)
{
del(i);
}
del(99);
display();
return 0;
}