400字范文,内容丰富有趣,生活中的好帮手!
400字范文 > 游戏筑基开发之双链表及其基本功能(C语言)

游戏筑基开发之双链表及其基本功能(C语言)

时间:2021-06-07 23:28:32

相关推荐

游戏筑基开发之双链表及其基本功能(C语言)

C语言双链表

主要使用结构体、一级指针、二级指针进行实现。功能包括:初始化、尾插法、任意插入法、任意删除法、打印数据等。

数据结构如下:

//双链表struct tagDoubleListNode{int v;struct tagDoubleListNode *prior;//前驱指针struct tagDoubleListNode *next;//后继指针};typedef struct tagDoubleList{struct tagDoubleListNode *nStart;//头节点标记位struct tagDoubleListNode *nEnd;//尾节点标记位int Size;//链表大小}TDoubleList, *PTDoubleList;

具体实现如下:

待优化,仅提供参考。

#include<stdio.h>//双链表struct tagDoubleListNode{int v;struct tagDoubleListNode *prior;struct tagDoubleListNode *next;};typedef struct tagDoubleList{struct tagDoubleListNode *nStart;struct tagDoubleListNode *nEnd;int Size;}TDoubleList, *PTDoubleList;//初始化static int DoubleListInit(PTDoubleList *ppv){PTDoubleList pv = malloc(sizeof(TDoubleList));pv->nEnd = NULL;pv->nStart = pv->nEnd;pv->Size = 0;*ppv = pv;pv = NULL;return 1;}//尾插入static int DoubleListAdd(PTDoubleList pv, int v){if (pv == NULL)return 0;struct tagDoubleListNode* pvNode = malloc(sizeof(struct tagDoubleListNode));pvNode->v = v;pvNode->next = NULL;if (pv->Size == 0){pvNode->prior = NULL;pv->nStart = pvNode;pv->nEnd = pv->nStart;}else{pvNode->prior = pv->nEnd;pv->nEnd->next = pvNode;pv->nEnd = pvNode;}pv->Size++;pvNode = NULL;return 1;}//任意插入static int DoubleListPush(PTDoubleList pv, int index, int v){if (pv == NULL || index > pv->Size)return 0;if (index == pv->Size){DoubleListAdd(pv, v);return 1;}struct tagDoubleListNode* pvNode = malloc(sizeof(struct tagDoubleListNode));pvNode->v = v;if (index == 0){pvNode->prior = NULL;pvNode->next = pv->nStart;pv->nStart = pvNode;pv->Size++;}else {struct tagDoubleListNode* headNode = pv->nStart;int count = -1;while (headNode != NULL && (++count) != index - 1)headNode = headNode->next;pvNode->next = headNode->next;headNode->next->prior = pvNode;headNode->next = pvNode;pvNode->prior = headNode;headNode = NULL;pv->Size++;}return 1;}//任意删除static int DoubleListDelete(PTDoubleList pv, int index){if (pv == NULL || index > pv->Size || pv->Size == 0)return 0;int count = -1;struct tagDoubleListNode* headNode = pv->nStart;struct tagDoubleListNode* temp = NULL;while (headNode != NULL && (++count) != index - 1 && index != 0)headNode = headNode->next;if (index + 1 == pv->Size){free(headNode->next);headNode->next = NULL;}else if (index == 0){temp = pv->nStart;pv->nStart = temp->next;free(temp);temp = NULL;}else{temp = headNode->next;headNode->next->next->prior = headNode;headNode->next = headNode->next->next;free(temp);temp = NULL;}pv->Size--;return 1;}//释放内存static int DoubleListFree(PTDoubleList *ppv){if (*ppv == NULL)return 0;struct tagDoubleListNode* headNode = (*ppv)->nStart;while (headNode != NULL){headNode = headNode->next;free((*ppv)->nStart);(*ppv)->nStart = headNode;}headNode = NULL;(*ppv)->nEnd = NULL;(*ppv)->nStart = NULL;(*ppv)->Size = 0;return 1;}//打印数据,从左至右static int LeftPrintData(PTDoubleList pv){if (pv == NULL || pv->Size == 0)return 0;struct tagDoubleListNode *Node = pv->nStart;while (Node != NULL){printf("%4d", Node->v);Node = Node->next;}printf("%\n");return 1;}//打印数据,从右至左static int RightPrintData(PTDoubleList pv){if (pv == NULL || pv->Size == 0)return 0;struct tagDoubleListNode *Node = pv->nEnd;while (Node != NULL){printf("%4d", Node->v);Node = Node->prior;}printf("%\n");return 1;}//测试int main(){PTDoubleList pv = NULL;DoubleListInit(&pv);//增添数据DoubleListAdd(pv, 3);DoubleListAdd(pv, 10);DoubleListAdd(pv, 1);DoubleListAdd(pv, 5);DoubleListAdd(pv, 7);DoubleListAdd(pv, 20);LeftPrintData(pv);//RightPrintData(pv);DoubleListPush(pv, 2, 25);LeftPrintData(pv);//RightPrintData(pv);DoubleListDelete(pv, 0);LeftPrintData(pv);DoubleListDelete(pv, 2);LeftPrintData(pv);DoubleListDelete(pv, 3);LeftPrintData(pv);return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。