typedef int DItemType;
typedef struct SDoubleList
{
DItemType iValue;
SDoubleList *prev;
SDoubleList *next;
} DLinkedList,*pDLinkedList;
/* */
DLinkedList *InitDoubleList()
{
DLinkedList *doublelist = (DLinkedList*)malloc(sizeof(DLinkedList));
if (NULL == doublelist)
exit(1);
doublelist->prev = NULL;
doublelist->next = NULL;
return doublelist;
}
/* */
bool DLinkedList_Push(DLinkedList* DL, DItemType value)
{
if (NULL==DL)
{
puts(" , !
");
return false;
}
DLinkedList *temp = (DLinkedList *)malloc(sizeof(DLinkedList));
if (NULL == temp) {
puts(" !");
return false;
}
temp->iValue = value;
temp->next = NULL;
temp->prev = NULL;
DLinkedList *ttemp = DL;
while (NULL != ttemp->next)
{
ttemp = ttemp->next;
}
ttemp->next = temp;
temp->prev = ttemp;
return true;
}
/* */
bool DLinkedList_Pop(DLinkedList *DL)
{
if (NULL==DL || NULL==DL->next) {
puts(" , !");
return false;
}
DLinkedList *temp = DL->next;
DL->next = temp->next;
if (NULL==temp->next) {
free(temp);
return true;
}
temp->next->prev = DL;
free(temp);
return true;
}
/* */
void VisitDLinkedList(DLinkedList *DL)
{
if (NULL==DL || NULL == DL->next) {
puts(" !");
return;
}
DLinkedList *temp = DL->next;
while (NULL !=temp)
{
printf("%d ", temp->iValue);
temp = temp->next;
}
printf("
");
}
/* */
bool findItem(DLinkedList *DL, DItemType value)
{
if (NULL==DL || NULL==DL->next) {
puts(" , !");
return false;
}
DLinkedList *temp = DL->next;
while (temp)
{
if (value==temp->iValue)
return true;
temp = temp->next;
}
return false;
}
void main()
{
DLinkedList *DL;
DL = InitDoubleList();
DLinkedList_Push(DL, 10);
DLinkedList_Push(DL, 20);
DLinkedList_Push(DL, 30);
VisitDLinkedList(DL);
DLinkedList_Pop(DL);
DLinkedList_Pop(DL);
VisitDLinkedList(DL);
if (findItem(DL,30))
puts(" !");
else puts(" !");
}