#include
#include
typedef int DLL_DataType;
typedef struct Node
{
DLL_DataType data;
struct Node *pre,*next;
}DLL_Node;
typedef struct
{
DLL_Node *head,*tail;
}DList;
DLL_Node* DLL_makeNode(DLL_DataType value)
{
DLL_Node
*new_ptr=malloc(sizeof(DLL_Node));
new_ptr->data=value;
new_ptr->pre=new_ptr->next=NULL;
return new_ptr;
}
void DLL_init(DList *p_dl)
{
p_dl->head=p_dl->tail=NULL;
}
void DLL_clear(DList *p_dl)
{
while(p_dl->head)
{
DLL_Node *del_ptr=p_dl->head;
p_dl->head=p_dl->head->next;
printf("%d",del_ptr->data);
free(del_ptr);
}
p_dl->tail=NULL;
}
int DLL_isEmpty(DList dl)
{
return dl.head==NULL;
}
void DLL_insertAtHead(DList
*p_dl,DLL_DataType value)
{
DLL_Node *new_ptr=DLL_makeNode(value);
new_ptr->next=p_dl->head;
if(p_dl->head)
{
p_dl->head->pre=new_ptr;
}
p_dl->head=new_ptr;
if(p_dl->tail==NULL)
{
p_dl->tail=new_ptr;
}
}
void DLL_insertAtTail(DList
*p_dl,DLL_DataType value)
{
DLL_Node *new_ptr=DLL_makeNode(value);
new_ptr->pre=p_dl->tail;
if(p_dl->tail)
{
p_dl->tail->next=new_ptr;
}
p_dl->tail=new_ptr;
if(p_dl->head==NULL)
{
p_dl->head=new_ptr;
}
}
int DLL_deleteFromHead(DList
*p_dl,DLL_DataType *p_value)
{
DLL_Node *del_ptr=p_dl->head;
if(del_ptr==NULL)
{
return 0;
}
*p_value=del_ptr->data;
p_dl->head=del_ptr->next;
if(p_dl->head)
{
p_dl->head->pre=NULL;
}
else
{
p_dl->tail=NULL;
}
free(del_ptr);
return 1;
}
int DLL_deleteFromTail(DList
*p_dl,DLL_DataType *p_value)
{
DLL_Node *del_ptr=p_dl->tail;
if(del_ptr==NULL)
{
return 0;
}
*p_value=del_ptr->data;
p_dl->tail=del_ptr->pre;
if(p_dl->tail)
{
p_dl->tail->next=NULL;
}
else
{
p_dl->head=NULL;
}
free(del_ptr);
return 1;
}
void print(DList dl)
{
DLL_Node *cur_ptr=dl.head;
while(cur_ptr)
{
printf("%d\n",cur_ptr->data);
cur_ptr=cur_ptr->next;
}
}
int main()
{
DLL_DataType i;
DLL_DataType x;
DList dl;
DLL_init(&dl);
for(i=0;i<3;i++)
{
DLL_insertAtHead(&dl,i);
}
print(dl);
printf("-----------------\n");
DLL_deleteFromTail(&dl,&x);
print(dl);
printf("被删除的数据为%d\n",x);
return 0;
}
阅读(1705) | 评论(0) | 转发(0) |