双链表
整体结构
初始化
void init(DLlist* pList)
{
pList->head = pList->back = NULL;
pList->size = 0;
}
DLlist* _createList()
{
DLlist* pList = (DLlist*)malloc(sizeof(DLlist));
pList->head = pList->back = NULL;
pList->size = 0;
return pList;
}
添加元素
添加为头结点
图示:
int addToHead(DLlist* pList, _TYPE value)
{
if (checkNull(pList))
{
return 0;
}
DLNode* newNode = (DLNode*)malloc(sizeof(DLNode));
if (checkNull(newNode))
{
return 0; // 创建新节点失败,则返回
}
newNode->pNext = newNode->pPre = NULL;
newNode->value = value;
if (pList->head==NULL) // 若头节点为空,则新节点置为头节点
{
pList->head = newNode;
pList->back = newNode;
pList->size++;
}
else // 添加到头部
{
newNode->pNext = pList->head;
pList->head->pPre = newNode;
pList->head = newNode; // 将链表的头部设为新节点
pList->size++;
}
return 1;
}
添加为尾结点
int addToBack(DLlist* pList, _TYPE value)
{
if (checkNull(pList))
{
return 0;
}
DLNode* newNode = (DLNode*)malloc(sizeof(DLNode));
if (checkNull(newNode))
{
return 0; // 创建新节点失败,则返回
}
newNode->pNext = newNode->pPre = NULL;
newNode->value = value;
if (NULL == pList->head) // 若头节点为空,则新节点置为头节点
{
pList->head = newNode;
pList->back = newNode;
pList->head->pNext = pList->back->pNext = NULL;
pList->size++;
}
else if (pList->size == 1) // 只有一个元素,头尾都指向它,链接好头部与下一个节点
{
newNode->pPre = pList->head;
pList->head->pNext = newNode;
pList->back = newNode;
pList->size++;
}
else // 添加到尾部
{
newNode->pPre = pList->back;
pList->back->pNext = newNode;
pList->back = newNode;
pList->size++;
}
return 1;
}
数组元素添加进入链表
- 1 添加到尾部的形式
int addToBackWithArrayValues(DLlist* pList, _TYPE array[], int length)
{
if (checkNull(pList) || checkNull(array) || length == 0)
{
return 0;
}
for (int i = 0; i < length; i++)
{
if (!addToBack(pList, array[i]))
{
return 0;
}
}
return 1;
}
指定结点后面插入元素
int insertAfter(DLlist* pList, _TYPE curValue, _TYPE newValue)
{
if (checkNull(pList))
{
return 0;
}
if (isEmpty(pList))
{
printf("\nlog:插入指定节点后面失败,空链表\n");
return 0;
}
DLNode* newNode = (DLNode*)malloc(sizeof(DLNode));
if (checkNull(newNode))
{
return 0; // 创建新节点失败,则返回
}
newNode->pNext = newNode->pPre = NULL;
newNode->value = newValue;
if (pList->head->value==curValue) // 头节点满足
{
DLNode* next = pList->head->pNext;
next->pPre = newNode;
newNode->pNext = next;
newNode->pPre = pList->head;
pList->head->pNext = newNode;
pList->size++;
return 1;
}
else if (pList->back->value == curValue) // 尾节点满足,防止要遍历一遍,浪费时间
{
pList->back->pNext = newNode;
newNode->pPre = pList->back->pNext;
pList->back;
pList->size++;
return 1;
}
else // 其他
{
DLNode* p = pList->head->pNext;
while (p)
{
if (p->value==curValue)
{
DLNode* next = p->pNext;
next->pPre = newNode;
newNode->pNext = next;
p->pNext = newNode;
newNode->pPre = p;
pList->size++;
return 1;
}
p = p->pNext;
}
}
return 0;
}
删除元素
删除第一个指定元素的节点
图示:
//删除第一个指定元素的节点,返回1则删除成功
int deleteNode(DLlist* pList, _TYPE value)
{
if (checkNull(pList))
{
printf("\nlog:删除节点失败\n");
return 0;
}
if (isEmpty(pList))
{
printf("\nlog:删除节点失败,空链表\n");
return 0;
}
DLNode* findNode = findByValue(pList, value);
if (checkNull(findNode))
{
printf("\nlog:要删除的节点不存在,value=%d\n", value);
return 0;
}
if (value == pList->head->value) // 头节点满足
{
if (pList->head->pNext==NULL) // 只有head时
{
DLNode* curNode = pList->head;
pList->head = pList->back = NULL; // 删除所有节点后将head与back都赋值NULL
free(curNode);
}
else
{
DLNode* curNode = pList->head;
pList->head = curNode->pNext; // 头节点后移
free(curNode);
}
pList->size--;
return 1;
}
else if (value == pList->back->value) // 尾节点满足
{
DLNode* curNode = pList->back;
pList->back = curNode->pPre;
pList->back->pNext = NULL;
free(curNode);
pList->size--;
return 1;
}
else
{
DLNode* curNode = pList->head->pNext;
while (curNode!=NULL) // 下面可以调用findByValue来查找指定节点
{
if (curNode->value==value) // 删除满足条件的节点
{
DLNode* preNode = curNode->pPre;
DLNode* nextNode = curNode->pNext;
preNode->pNext = nextNode;
nextNode->pPre = preNode;
free(curNode); // 释放内存
pList->size--;
return 1;
}
curNode = curNode->pNext;
}
}
return 0;
}
修改指定结点值
int modify(DLlist* pList, _TYPE oldValue, _TYPE newValue)
{
if (checkNull(pList) )
{
return 0;
}
if (isEmpty(pList))
{
printf("\nlog:空链表\n");
return 0;
}
DLNode* curNode = pList->head;
while (curNode)
{
if (curNode->value==oldValue)
{
curNode->value = newValue;
return 1;
}
curNode = curNode->pNext;
}
return 0;
}