[Cpp]
# Include <assert. h>
# Include <stdlib. h>
# Include <stdio. h>
Typedef struct myList DNode;
Struct myList
{
DNode * next;
DNode * prev;
Int data;
};
DNode * init (int data );
Void insertLastNode (DNode * L, int data );
Void insertNode (DNode * L, int data );
DNode * findPrev (DNode * L, int data );
Void deleNode (DNode * L, int data );
DNode * findCur (DNode * L, int data );
/*************************************** *********************************/
/* Initialize the first node */
/*************************************** *********************************/
DNode * init (int data)
{
DNode * headNode = (DNode *) malloc (sizeof (DNode ));
Assert (headNode );
HeadNode-> next = NULL;
HeadNode-> prev = NULL;
HeadNode-> data = data;
Return headNode;
}
Int isEmpty (DNode * L)
{
Return L-> next = NULL;
}
Int isLast (DNode * L, DNode * p)
{
Return p-> next = NULL;
}
DNode * findCur (DNode * L, int data)
{
DNode * curNode;
CurNode = L-> next;
While (curNode! = NULL & curNode-> data! = Data)
CurNode = curNode-> next;
Return curNode;
}
/*************************************** *********************************/
/* Insert a node to the backend of the queue */
/*************************************** *********************************/
Void insertLastNode (DNode * L, int data)
{
Assert (L );
DNode * oNode = (DNode *) malloc (sizeof (DNode ));
ONode-> data = data;
L-> next = oNode;
ONode-> prev = L;
}
/**
Insert to the specified position of the linked list
**/
Void insertNode (DNode * L, int data)
{
Assert (L );
DNode * qNode = (DNode *) malloc (sizeof (DNode ));
DNode * prevNode = findPrev (L, data );
QNode-> next = prevNode-> next;
PrevNode-> next-> prev = qNode;
PrevNode-> next = qNode;
QNode-> prev = prevNode;
Return;
}
DNode * findPrev (DNode * L, int data)
{
Assert (L );
DNode * tmpNode;
TmpNode = L;
While (tmpNode-> next! = NULL & tmpNode-> next-> data! = Data)
{
TmpNode = tmpNode-> next;
}
Return tmpNode;
}
Void deleNode (DNode * L, int data)
{
DNode * tmpNode = findCur (L, data );
If (tmpNode = NULL)
{
Return;
}
If (isLast (L, tmpNode ))
{
TmpNode-> prev = NULL;
TmpNode-> prev-> next = NULL;
Free (tmpNode );
}
TmpNode-> prev-> next = tmpNode-> next;
TmpNode-> next-> prev = tmpNode-> prev;
Free (tmpNode );
Return;
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
DNode * myDNode;
MyDNode = init (1 );
Return 0;
}
# Include <assert. h>
# Include <stdlib. h>
# Include <stdio. h>
Typedef struct myList DNode;
Struct myList
{
DNode * next;
DNode * prev;
Int data;
};
DNode * init (int data );
Void insertLastNode (DNode * L, int data );
Void insertNode (DNode * L, int data );
DNode * findPrev (DNode * L, int data );
Void deleNode (DNode * L, int data );
DNode * findCur (DNode * L, int data );
/*************************************** *********************************/
/* Initialize the first node */
/*************************************** *********************************/
DNode * init (int data)
{
DNode * headNode = (DNode *) malloc (sizeof (DNode ));
Assert (headNode );
HeadNode-> next = NULL;
HeadNode-> prev = NULL;
HeadNode-> data = data;
Return headNode;
}
Int isEmpty (DNode * L)
{
Return L-> next = NULL;
}
Int isLast (DNode * L, DNode * p)
{
Return p-> next = NULL;
}
DNode * findCur (DNode * L, int data)
{
DNode * curNode;
CurNode = L-> next;
While (curNode! = NULL & curNode-> data! = Data)
CurNode = curNode-> next;
Return curNode;
}
/*************************************** *********************************/
/* Insert a node to the backend of the queue */
/*************************************** *********************************/
Void insertLastNode (DNode * L, int data)
{
Assert (L );
DNode * oNode = (DNode *) malloc (sizeof (DNode ));
ONode-> data = data;
L-> next = oNode;
ONode-> prev = L;
}
/**
Insert to the specified position of the linked list
**/
Void insertNode (DNode * L, int data)
{
Assert (L );
DNode * qNode = (DNode *) malloc (sizeof (DNode ));
DNode * prevNode = findPrev (L, data );
QNode-> next = prevNode-> next;
PrevNode-> next-> prev = qNode;
PrevNode-> next = qNode;
QNode-> prev = prevNode;
Return;
}
DNode * findPrev (DNode * L, int data)
{
Assert (L );
DNode * tmpNode;
TmpNode = L;
While (tmpNode-> next! = NULL & tmpNode-> next-> data! = Data)
{
TmpNode = tmpNode-> next;
}
Return tmpNode;
}
Void deleNode (DNode * L, int data)
{
DNode * tmpNode = findCur (L, data );
If (tmpNode = NULL)
{
Return;
}
If (isLast (L, tmpNode ))
{
TmpNode-> prev = NULL;
TmpNode-> prev-> next = NULL;
Free (tmpNode );
}
TmpNode-> prev-> next = tmpNode-> next;
TmpNode-> next-> prev = tmpNode-> prev;
Free (tmpNode );
Return;
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
DNode * myDNode;
MyDNode = init (1 );
Return 0;
}