头文件
#pragma once
#ifndef _L_LIST_H
#define _L_LIST_H
typedef struct node
{
struct node* nextNode, * prevNode;
void* nodeData;
}node_t, * pNode_t;
typedef struct list
{
struct node* headNode;
int nodeCount;
}list_t, * pList_t;
pList_t CreateList(unsigned int nodeCount);
void DeleteList(pList_t list);
pList_t LinkList(pList_t list1, pList_t list2);
void PrintfList(pList_t list);
pList_t AddNode(pList_t list, pNode_t nodeToAdd);
pList_t DeleteNode(pList_t list, pNode_t nodeToDelete);
pNode_t FindNodeByIndex(pList_t list, unsigned int index);
pList_t InsertNodeByIndex(pList_t list, pNode_t nodeToInsert, unsigned int indexToInsert);
int GetNodeIndex(pList_t list, pNode_t node);
#endif
源代码
#include <stdlib.h>
#include <stdio.h>
#include "lList.h"
void DeleteList(pList_t list)
{
pNode_t currentNode = NULL;
pNode_t nextNode = NULL;
if (list == NULL)
{
return;
}
currentNode = list->headNode;
while (list->nodeCount--)
{
nextNode = currentNode->nextNode;
printf("节点:%p空间已释放\n", currentNode);
free(currentNode);
currentNode = nextNode;
}
printf("链表:%p空间已释放\n", list);
free(list);
}
pList_t CreateList(unsigned int nodeCount)
{
pNode_t currentNode = NULL;
pNode_t prevNode = NULL;
pList_t list = NULL;
if (!nodeCount)
{
return NULL;
}
list = calloc(1, sizeof(list_t));
if (list == NULL)
{
return NULL;
}
while (nodeCount--)
{
currentNode = calloc(1, sizeof(node_t));
if (currentNode == NULL)
{
DeleteList(list);
return NULL;
}
if (list->headNode == NULL)
{
list->headNode = currentNode;
prevNode = currentNode;
}
prevNode->nextNode = currentNode;
currentNode->prevNode = prevNode;
prevNode = currentNode;
list->nodeCount++;
}
currentNode->nextNode = list->headNode;
list->headNode->prevNode = currentNode;
return list;
}
pList_t LinkList(pList_t list1, pList_t list2)
{
list1->headNode->prevNode->nextNode = list2->headNode;
list2->headNode->prevNode->nextNode = list1->headNode;
list1->headNode->prevNode = list2->headNode->prevNode;
list2->headNode->prevNode = list1->headNode->prevNode;
list1->nodeCount += list2->nodeCount;
free(list2);
return list1;
}
void PrintfList(pList_t list)
{
pNode_t currentNode = NULL;
int nodeCount = list->nodeCount;
printf("双链表链表的地址:%p, 头节点的地址:%p,节点数目:%-4d\n", list, list->headNode, list->nodeCount);
currentNode = list->headNode;
while (nodeCount--)
{
printf("上一个节点的地址:%p,当前节点的地址:%p,下一个节点的地址:%p\n", currentNode->prevNode, currentNode, currentNode->nextNode);
currentNode = currentNode->nextNode;
}
}
pList_t AddNode(pList_t list, pNode_t nodeToAdd)
{
pNode_t node = NULL;
if (list == NULL || nodeToAdd == NULL)
{
return NULL;
}
node = calloc(1, sizeof(node_t));
if (node == NULL)
{
return NULL;
}
node->nodeData = nodeToAdd->nodeData;
node->nextNode = list->headNode;
node->prevNode = list->headNode->prevNode;
list->headNode->prevNode->nextNode = node;
list->headNode->prevNode = node;
list->nodeCount++;
return list;
}
pNode_t FindNodeByIndex(pList_t list, unsigned int index)
{
pNode_t node = NULL;
if (list == NULL)
{
return NULL;
}
node = list->headNode;
while (index--)
{
node = node->nextNode;
}
return node;
}
pList_t InsertNodeByIndex(pList_t list, pNode_t nodeToInsert, unsigned int indexToInsert)
{
pNode_t node = NULL;
if (list == NULL || nodeToInsert == NULL)
{
return NULL;
}
node = calloc(1, sizeof(node_t));
if (node == NULL)
{
return NULL;
}
node->nodeData = nodeToInsert->nodeData;
nodeToInsert = node;
node = FindNodeByIndex(list, indexToInsert);
nodeToInsert->nextNode = node;
nodeToInsert->prevNode = node->prevNode;
node->prevNode->nextNode = nodeToInsert;
node->prevNode = nodeToInsert;
if (indexToInsert % list->nodeCount == 0)
{
list->headNode = nodeToInsert;
}
list->nodeCount++;
return list;
}
pList_t DeleteNode(pList_t list, pNode_t nodeToDelete)
{
if (list == NULL || nodeToDelete == NULL)
{
return NULL;
}
if (nodeToDelete == list->headNode)
{
list->headNode = list->headNode->nextNode;
}
nodeToDelete->prevNode->nextNode = nodeToDelete->nextNode;
nodeToDelete->nextNode->prevNode = nodeToDelete->prevNode;
free(nodeToDelete);
list->nodeCount--;
return list;
}
int GetNodeIndex(pList_t list, pNode_t node)
{
int nodeCount = 0;
int index = 0;
pNode_t currentNode = NULL;
if (list == NULL || node == NULL)
{
return -1;
}
currentNode = list->headNode;
nodeCount = list->nodeCount;
while (nodeCount--)
{
if (currentNode == node || currentNode->nodeData == node->nodeData)
{
return index;
}
currentNode = currentNode->nextNode;
index++;
}
return -1;
}