赞
踩
带头双向循环链表
注意:这⾥的“带头”跟前⾯我们说的“头节点”是两个概念,实际前⾯的在单链表阶段称呼不严
谨,但是为了更好的理解就直接称为单链表的头节点。
带头链表⾥的头节点,实际为“哨兵位”,哨兵位节点不存储任何有效元素,只是站在这⾥“放哨的”
“哨兵位”存在的意义:
遍历循环链表避免死循环。
List.h 定义双向链表的结构,声明相关的函数
List.c 实现双向链表声明的函数
test.c 测试代码
#pragma once #include <stdio.h> #include <stdlib.h> #include <assert.h> typedef int LTDateType; //定义双向链表结点的结构 typedef struct ListNode { LTDateType date; struct ListNode* next; struct ListNode* prev; }LTNode; //声明双向链表中提供的方法 //初始化 //void LTInit(LTNode** pphead); LTNode* LTInit(); //打印 void LTPrint(LTNode* phead); //插入数据之前,链表必须初始化为只有一个结点的情况下 // 不改变哨兵位的位置,因此传一级指针即可 //尾插 void LTPushBack(LTNode* phead, LTDateType x); //头插 void LTPushFront(LTNode* phead, LTDateType x); //尾删 void LTPopBack(LTNode* phead); //头删 void LTPopFront(LTNode* phead); //查找 LTNode* LTFind(LTNode* phead, LTDateType x); //在pos位置之后插入数据 void LTInsert(LTNode* pos, LTDateType x); //删除pos结点 void LTErase(LTNode* pos); //销毁 void LTDesTroy(LTNode* phead);
//申请结点
LTNode* LTBuyNode(LTDateType x)
{
LTNode* node = (LTNode*)malloc(sizeof(LTNode));
if (node == NULL)
{
perror("malloc fail!");
exit(1);
}
node->date = x;
node->next = node->prev = node;
return node;
}
这里有两种实现方法
//初始化
//void LTInit(LTNode** pphead)
//{
// //给双向链表创建一个哨兵位
// *pphead = LTBuyNode(-1);
//}
LTNode* LTInit()
{
LTNode* phead = LTBuyNode(-1);
return phead;
}
//打印
void LTPrint(LTNode* phead)
{
assert(phead);
LTNode* pcur = phead->next;
while (pcur != phead)
{
printf("%d->", pcur->date);
pcur = pcur->next;
}
printf("\n");
}
注意:插入数据之前,链表必须初始化为只有一个结点的情况下
不改变哨兵位的位置,因此传一级指针即可
//尾插
void LTPushBack(LTNode* phead, LTDateType x)
{
assert(phead);
LTNode* newnode = LTBuyNode(x);
//phead phead->prev newnode
newnode->prev = phead->prev;
newnode->next = phead;
phead->prev->next = newnode;
phead->prev = newnode;
}
//头插
void LTPushFront(LTNode* phead, LTDateType x)
{
assert(phead);
LTNode* newnode = LTBuyNode(x);
//phead newnode phead->next
newnode->prev = phead;
newnode->next = phead->next;
phead->next->prev = newnode;
phead->next = newnode;
}
//尾删
void LTPopBack(LTNode* phead)
{
//链表必须有效并且链表不能为空(只有一个哨兵位)
assert(phead && phead->next != phead);
LTNode* del = phead->prev;
//phead del->prev del
del->prev->next = phead;
phead->prev = del->prev;
//删除del结点
free(del);
del = NULL;
}
//头删
void LTPopFront(LTNode* phead)
{
//链表必须有效并且链表不能为空(只有一个哨兵位)
assert(phead && phead->next != phead);
LTNode* del = phead->next;
//phead del del->next
del->next->prev = phead;
phead->next = del->next;
//删除del结点
free(del);
del = NULL;
}
//查找
LTNode* LTFind(LTNode* phead, LTDateType x)
{
assert(phead && phead->next != phead);
LTNode* pcur = phead->next;
while (pcur != phead)
{
if (pcur->date == x)
{
return pcur;
}
pcur = pcur->next;
}
return NULL;
}
//在pos位置之后插入数据
void LTInsert(LTNode* pos, LTDateType x)
{
assert(pos);
LTNode* newnode = LTBuyNode(x);
//pos newnode pos->next
newnode->prev = pos;
newnode->next = pos->next;
pos->next->prev = newnode;
pos->next = newnode;
}
//删除pos结点
void LTErase(LTNode* pos)
{
assert(pos);
//pos->prev pos pos->next
pos->next->prev = pos->prev;
pos->prev->next = pos->next;
free(pos);
pos = NULL;
}
//销毁
void LTDesTroy(LTNode* phead)
{
assert(phead);
LTNode* pcur = phead->next;
while (pcur != phead)
{
LTNode* next = pcur->next;
free(pcur);
pcur = next;
}
free(phead);
phead = NULL;
}
#pragma once #include <stdio.h> #include <stdlib.h> #include <assert.h> typedef int LTDateType; //定义双向链表结点的结构 typedef struct ListNode { LTDateType date; struct ListNode* next; struct ListNode* prev; }LTNode; //声明双向链表中提供的方法 //初始化 //void LTInit(LTNode** pphead); LTNode* LTInit(); //打印 void LTPrint(LTNode* phead); //插入数据之前,链表必须初始化为只有一个结点的情况下 // 不改变哨兵位的位置,因此传一级指针即可 //尾插 void LTPushBack(LTNode* phead, LTDateType x); //头插 void LTPushFront(LTNode* phead, LTDateType x); //尾删 void LTPopBack(LTNode* phead); //头删 void LTPopFront(LTNode* phead); //查找 LTNode* LTFind(LTNode* phead, LTDateType x); //在pos位置之后插入数据 void LTInsert(LTNode* pos, LTDateType x); //删除pos结点 void LTErase(LTNode* pos); //销毁 void LTDesTroy(LTNode* phead);
#define _CRT_SECURE_NO_WARNINGS 1 #include "List.h" //申请结点 LTNode* LTBuyNode(LTDateType x) { LTNode* node = (LTNode*)malloc(sizeof(LTNode)); if (node == NULL) { perror("malloc fail!"); exit(1); } node->date = x; node->next = node->prev = node; return node; } //初始化 //void LTInit(LTNode** pphead) //{ // //给双向链表创建一个哨兵位 // *pphead = LTBuyNode(-1); //} LTNode* LTInit() { LTNode* phead = LTBuyNode(-1); return phead; } //打印 void LTPrint(LTNode* phead) { assert(phead); LTNode* pcur = phead->next; while (pcur != phead) { printf("%d->", pcur->date); pcur = pcur->next; } printf("\n"); } //尾插 void LTPushBack(LTNode* phead, LTDateType x) { assert(phead); LTNode* newnode = LTBuyNode(x); //phead phead->prev newnode newnode->prev = phead->prev; newnode->next = phead; phead->prev->next = newnode; phead->prev = newnode; } //头插 void LTPushFront(LTNode* phead, LTDateType x) { assert(phead); LTNode* newnode = LTBuyNode(x); //phead newnode phead->next newnode->prev = phead; newnode->next = phead->next; phead->next->prev = newnode; phead->next = newnode; } //尾删 void LTPopBack(LTNode* phead) { //链表必须有效并且链表不能为空(只有一个哨兵位) assert(phead && phead->next != phead); LTNode* del = phead->prev; //phead del->prev del del->prev->next = phead; phead->prev = del->prev; //删除del结点 free(del); del = NULL; } //头删 void LTPopFront(LTNode* phead) { //链表必须有效并且链表不能为空(只有一个哨兵位) assert(phead && phead->next != phead); LTNode* del = phead->next; //phead del del->next del->next->prev = phead; phead->next = del->next; //删除del结点 free(del); del = NULL; } //查找 LTNode* LTFind(LTNode* phead, LTDateType x) { assert(phead && phead->next != phead); LTNode* pcur = phead->next; while (pcur != phead) { if (pcur->date == x) { return pcur; } pcur = pcur->next; } return NULL; } //在pos位置之后插入数据 void LTInsert(LTNode* pos, LTDateType x) { assert(pos); LTNode* newnode = LTBuyNode(x); //pos newnode pos->next newnode->prev = pos; newnode->next = pos->next; pos->next->prev = newnode; pos->next = newnode; } //删除pos结点 void LTErase(LTNode* pos) { assert(pos); //pos->prev pos pos->next pos->next->prev = pos->prev; pos->prev->next = pos->next; free(pos); pos = NULL; } //销毁 void LTDesTroy(LTNode* phead) { assert(phead); LTNode* pcur = phead->next; while (pcur != phead) { LTNode* next = pcur->next; free(pcur); pcur = next; } free(phead); phead = NULL; }
#define _CRT_SECURE_NO_WARNINGS 1 #include "List.h" //测试代码 void test() { //测试初始化 /*LTNode* plist = NULL; LTInit(&plist);*/ LTNode* plist = LTInit(); //测试尾插 LTPushBack(plist, 1); LTPrint(plist); LTPushBack(plist, 2); LTPrint(plist); LTPushBack(plist, 3); LTPrint(plist); LTPushBack(plist, 4); LTPrint(plist); //测试头插 /*LTPushFront(plist, 8); LTPushFront(plist, 7); LTPushFront(plist, 6); LTPushFront(plist, 5); LTPrint(plist);*/ //测试尾删 /*LTPopBack(plist); LTPrint(plist); LTPopBack(plist); LTPrint(plist); LTPopBack(plist); LTPrint(plist); LTPopBack(plist); LTPrint(plist); LTPopBack(plist); LTPrint(plist);*/ //测试头删 /*LTPopFront(plist); LTPrint(plist); LTPopFront(plist); LTPrint(plist); LTPopFront(plist); LTPrint(plist); LTPopFront(plist); LTPrint(plist); LTPopFront(plist); LTPrint(plist);*/ //测试查找 /*LTNode* find = LTFind(plist, 4); if (find == NULL) { printf("找不到!\n"); } else { printf("找到了!\n"); }*/ //测试在pos位置之后插入数据 /*LTNode* find = LTFind(plist, 4); LTInsert(find, 88); LTPrint(plist);*/ //测试删除pos结点 /*LTNode* find = LTFind(plist, 2); LTErase(find); LTPrint(plist); find = NULL;*/ //测试销毁 LTDesTroy(plist); plist = NULL; } int main() { test(); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。