西部数码网站管理助手搭建织梦,无锡网站排名优化公司,如何快速的做网站,手机网站 搜索优化 百度目录
前言#xff1a;
一#xff1a;双链表的定义
编辑 二#xff1a;双向链表的实现
2.1#xff1a;链表的构造
2.2#xff1a;创建头节点
2.3#xff1a;创建节点
2.4#xff1a;链表的尾插
2.5#xff1a;链表的打印
2.6#xff1a;链表的尾删
2.7
一双链表的定义
编辑 二双向链表的实现
2.1链表的构造
2.2创建头节点
2.3创建节点
2.4链表的尾插
2.5链表的打印
2.6链表的尾删
2.7链表的头插
2.8链表的头删
2.9链表的查找
2.10在目标位置前面插入
2.11删除目标位置结点
2.12链表的销毁 总代码
test.c
List.c List.h 前言 双链表的引入是因为单链表要访问某个结点的前驱结点时只能从头开始遍历访问后驱结点的复杂度为O(1)访问前驱结点的复杂度为O(n)。为了克服上述缺点引入了双链表。 双链表的引进对于链表的操作有了极大的遍历 一双链表的定义 链表由单向的链变成了双向链。 双向链表(double linked list)是在单链表的每个结点中再设置一个指向其前驱结点的指针域。 二双向链表的实现
2.1链表的构造 包含了一个数据域两个指针域指向前后驱节点 // 带头双向循环链表增删查改实现
typedef int LTDataType;
typedef struct ListNode
{LTDataType data; //数据struct ListNode* next; //下一个指针域struct ListNode* prev; //上一个指针域
}ListNode; 2.2创建头节点 双向链表一般都是带头节点的在链表中带了头节点对于链表分割这一问题有了简单化 动态开辟出一块空间前后指针都指向自己 //初始化链表头头节点
ListNode* ListInit()
{ListNode* phead (ListNode*)malloc(sizeof(ListNode));assert(phead);phead-data -1;phead-next phead;phead-prev phead;return phead;
}
2.3创建节点 这里置NULL和单链表的置NULL是一样的意思对后续的操作提供便利 // 创建返回链表的结点.
ListNode* ListCreate(LTDataType x)
{ListNode* newnode (ListNode*)malloc(sizeof(ListNode));assert(newnode);newnode-data x;newnode-next NULL;newnode-prev NULL;return newnode;
}
2.4链表的尾插 单链表的尾插还需要考虑是否存在第一个节点这里直接插入即可 注意操作顺序 // 双向链表尾插
void ListPushBack(ListNode* phead, LTDataType x)
{assert(phead);ListNode* newnode ListCreate(x);struct ListNode* tail phead-prev;// phead tail newnode//newnode-prev phead-prev;//newnode-next phead;//phead-prev-next newnode;//phead-prev newnode;//注意前后顺序newnode-prev tail;tail-next newnode;newnode-next phead;phead-prev newnode;} 2.5链表的打印 这里的关键就是从哪里开始如何判断结束因为是循环 我们可以从头结点的下一个开始打印当遇到头结点即是结束 // 双向链表打印
void ListPrint(ListNode* phead)
{assert(phead);struct ListNode* cur phead-next;printf(哨兵位);while (cur ! phead){printf(%d, cur-data);cur cur-next;}printf(\n);
} 2.6链表的尾删 要多一个断言判断如果只有一个头指针就不用操作了 保存尾结点的前驱释放尾结点即可 // 双向链表尾删
void ListPopBack(ListNode* phead)
{assert(phead);assert(phead-next ! phead); //只有头指针不删struct ListNode* cur phead-prev;struct ListNode* curPrev cur-prev; //尾节点的上一个phead-prev curPrev;curPrev-next phead;free(cur);cur NULL;
} 2.7链表的头插 和尾插操作相似定义头节点的下一个结点进行链接即可 注意顺序 // 双向链表头插
void ListPushFront(ListNode* phead, LTDataType x)
{assert(phead);ListNode* newnode ListCreate(x);struct ListNode* cur phead-next;newnode-next cur; cur-prev newnode;newnode-prev phead;phead-next newnode;
} 2.8链表的头删
删除操作一般都需要判断一下是否只有头节点。判断双向链表的条件是phead-next ! phead; // 双向链表头删
void ListPopFront(ListNode* phead)
{assert(phead);assert(phead-next ! phead); //只有头指针不删ListNode* cur phead-next;ListNode* next cur-next;phead-next next;next-prev phead;free(cur);cur NULL;
} 2.9链表的查找 找到该结点后返回的是指针而不是数据返回该指针位置方便后续操作 // 双向链表查找
ListNode* ListFind(ListNode* phead, LTDataType x)
{assert(phead);ListNode* cur phead-next;while (cur ! phead){if (cur-data x)return cur;cur cur-next;}return NULL;
}
2.10在目标位置前面插入
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x)
{assert(pos); //检查pos位置是否有效ListNode* newnode ListCreate(x);newnode-next pos; //将newnode节点next prev 链接前后节点newnode-prev pos-prev;pos-prev-next newnode;pos-prev newnode;
} 2.11删除目标位置结点
// 双向链表删除pos位置的节点
void ListErase(ListNode* pos)
{assert(pos);ListNode* posPrev pos-prev;ListNode* next pos-next;posPrev-next next;next-prev posPrev;free(pos);pos NULL;
} 2.12链表的销毁
// 双向链表销毁
void ListDestory(ListNode* phead)
{assert(phead);ListNode* cur phead-prev;while (cur ! phead) //将除了头结点的都销毁{ListNode* curPrev cur-prev;free(cur);cur curPrev;}free(phead); //再释放头结点//phead NULL;
} 总代码
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#includeList.hvoid test1()
{ListNode* plist NULL;plist ListInit();ListPushBack(plist, 1);ListPushBack(plist, 2);ListPushBack(plist, 3);ListPushBack(plist, 4);ListPrint(plist);ListPopBack(plist);ListPrint(plist);ListPopBack(plist);ListPrint(plist);ListPopBack(plist);ListPrint(plist);ListPopBack(plist);ListPrint(plist);}void test2()
{ListNode* plist NULL;plist ListInit();ListPrint(plist);//ͷListPushFront(plist, 6);ListPrint(plist);//ͷɾListPopFront(plist);ListPrint(plist);}void test3()
{ListNode* plist NULL;plist ListInit();ListPushBack(plist, 1);ListPushBack(plist, 2);ListPushBack(plist, 3);ListPushBack(plist, 4);ListPrint(plist);//βɾListPopBack(plist);ListPopBack(plist);ListPopBack(plist);ListNode* pos ListFind(plist,1);ListInsert(pos, 666);ListPrint(plist);ListErase(pos);ListPrint(plist);ListDestory(plist);
}
int main()
{//test1();//test2();test3();return 0;
}
List.c
#define _CRT_SECURE_NO_WARNINGS 1
#includeList.h//初始化链表头头节点
ListNode* ListInit()
{ListNode* phead (ListNode*)malloc(sizeof(ListNode));assert(phead);phead-data -1;phead-next phead;phead-prev phead;return phead;
}// 创建返回链表的头结点.
ListNode* ListCreate(LTDataType x)
{ListNode* newnode (ListNode*)malloc(sizeof(ListNode));assert(newnode);newnode-data x;newnode-next NULL;newnode-prev NULL;return newnode;
}// 双向链表尾插
void ListPushBack(ListNode* phead, LTDataType x)
{assert(phead);ListNode* newnode ListCreate(x);struct ListNode* tail phead-prev;// phead tail newnode//newnode-prev phead-prev;//newnode-next phead;//phead-prev-next newnode;//phead-prev newnode;//注意前后顺序newnode-prev tail;tail-next newnode;newnode-next phead;phead-prev newnode;}// 双向链表打印
void ListPrint(ListNode* phead)
{assert(phead);struct ListNode* cur phead-next;printf(哨兵位);while (cur ! phead){printf(%d, cur-data);cur cur-next;}printf(\n);
}// 双向链表尾删
void ListPopBack(ListNode* phead)
{assert(phead);assert(phead-next ! phead); //只有头指针不删struct ListNode* cur phead-prev;struct ListNode* curPrev cur-prev; //尾节点的上一个phead-prev curPrev;curPrev-next phead;free(cur);cur NULL;
}// 双向链表头插
void ListPushFront(ListNode* phead, LTDataType x)
{assert(phead);ListNode* newnode ListCreate(x);struct ListNode* cur phead-next;newnode-next cur; cur-prev newnode;newnode-prev phead;phead-next newnode;
}// 双向链表头删
void ListPopFront(ListNode* phead)
{assert(phead);assert(phead-next ! phead); //只有头指针不删ListNode* cur phead-next;ListNode* next cur-next;phead-next next;next-prev phead;free(cur);cur NULL;
}// 双向链表查找
ListNode* ListFind(ListNode* phead, LTDataType x)
{assert(phead);ListNode* cur phead-next;while (cur ! phead){if (cur-data x)return cur;cur cur-next;}return NULL;
}// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x)
{assert(pos); //检查pos位置是否有效ListNode* newnode ListCreate(x);newnode-next pos; //将newnode节点next prev 链接前后节点newnode-prev pos-prev;pos-prev-next newnode;pos-prev newnode;
}// 双向链表删除pos位置的节点
void ListErase(ListNode* pos)
{assert(pos);ListNode* posPrev pos-prev;ListNode* next pos-next;posPrev-next next;next-prev posPrev;free(pos);pos NULL;
}// 双向链表销毁
void ListDestory(ListNode* phead)
{assert(phead);ListNode* cur phead-prev;while (cur ! phead) //将除了头节点的都销毁{ListNode* curPrev cur-prev;free(cur);cur curPrev;}free(phead);//phead NULL;
} List.h
#pragma once
#includestdio.h
#includeassert.h
#includestdlib.h// 带头双向循环链表增删查改实现
typedef int LTDataType;
typedef struct ListNode
{LTDataType data; //数据struct ListNode* next; //下一个指针域struct ListNode* prev; //上一个指针域
}ListNode;// 创建返回链表的头结点.
ListNode* ListCreate(LTDataType x);//初始化链表头头节点
ListNode* ListInit();// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x);// 双向链表打印
void ListPrint(ListNode* pHead);// 双向链表尾删
void ListPopBack(ListNode* pHead);// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x);// 双向链表头删
void ListPopFront(ListNode* pHead);// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x);// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x);// 双向链表删除pos位置的节点
void ListErase(ListNode* pos);// 双向链表销毁
void ListDestory(ListNode* pHead); 以上就是我对【数据结构|双向链表|增删改查】的介绍不足之处还望指点。