# 双向链表的初始化结构

## 代码实现

``````//双向链表的结点，包含一个数据域，两个指针域
typedef struct DoublyNode {
ElementType date;  //数据域
struct DoublyNode* prev;   //指向前缀结点
struct DoublyNode* next;   //指向后缀结点
}DoublyNode;
``````

## 2.双向链表的头结点

``````//双向链表
int length;
DoublyNode* next;
};``````

## 3.总代码

``````//双向链表的结点，包含一个数据域，两个指针域
typedef struct DoublyNode {
ElementType date;  //数据域
struct DoublyNode* prev;   //指向前缀结点
struct DoublyNode* next;   //指向后缀结点
}DoublyNode;

//双向链表
int length;
DoublyNode* next;
};
``````

# 双向链表中的指定文件插入元素

## 总代码

``````void InsertDoublyLinkList(DoublyLinkList* dlist, int pos, ElementType element) {
//创建空节点
node->date = element;
node->prev = NULL;
node->next = NULL;
//在第一个位置插入结点
if (pos == 1) {
node->next = dlist->next;
dlist->next = node;
node->next->prev = node;
dlist->length++;
return;
}
for (int i = 1; currNode && i < pos - 1; i++) {
currNode = currNode->next;
}
if (currNode) {
node->prev = currNode;
if (currNode->next) {
//如果前置结点非空->因为空就表示没有后继结点了
//将插入位置的前置结点改为指向新结点
currNode->next->prev = node;
}
node->next = currNode->next;
currNode->next = node;
dlist->length++;
}
}``````

# 双向链表的删除

## 总代码

``````void DeleteDoublyLinkList(DoublyLinkList* dlist, int pos) {
if (pos == 1) {
if (node) {
dlist->next;
if (node->next) {
//如果哟有第二个结点，那么设置第二个结点的前置结点为NULL
node->next->prev = NULL;
}
free(node);
dlist->length--;
}
return;
}

for (int i = 1; i < pos; i++) {
node = node->next;
}
if (node) {
if (node->next) {
node->next->prev = node->prve;
}
node->prev->next = node->next;
free(node);
dlist->length--;
}
return;
}

``````

THE END