[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);
/************************************************************************/
/* 初始化第一個節點 */
/************************************************************************/
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;
}
/************************************************************************/
/* 將節點插入到隊列後端 */
/************************************************************************/
void insertLastNode(DNode* L,int data)
{
assert(L);
DNode* oNode=(DNode*)malloc(sizeof(DNode));
oNode->data = data;
L->next = oNode;
oNode->prev = L;
}
/**
插入到鏈表指定位置
**/
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);
/************************************************************************/
/* 初始化第一個節點 */
/************************************************************************/
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;
}
/************************************************************************/
/* 將節點插入到隊列後端 */
/************************************************************************/
void insertLastNode(DNode* L,int data)
{
assert(L);
DNode* oNode=(DNode*)malloc(sizeof(DNode));
oNode->data = data;
L->next = oNode;
oNode->prev = L;
}
/**
插入到鏈表指定位置
**/
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;
}