今日一言:“如果不想做点事情,就不要想到达这个世界上的任何地方。”
前言
前面了解了单链表,但是单链表用起来总感觉有些麻烦,尤其是尾插、尾删操作的时候,要遍历一整个链表才能实现。今天我们介绍一种结构,完美避免了单链表的缺陷,它就是双向链表。
线性表分为顺序存储结构、链式存储结构,顺序存储结构对应的是[[顺序表]],链式存储结构则为链表。根据单向或双向、循环或非循环、带头或非带头这三个方面不同,链表又可分为八种:不带头单向不循环、不带头单向循环、不带头双向不循环、不带头双向循环、带头单向不循环、带头单向循环、带头双向不循环、带头双向循环。
之前我们介绍了结构最最简单的单链表(不带头单向不循环链表),今天我们介绍结构最最复杂的双向链表(带头双向循环链表)。
下面我们来看看双向链表的实现代码:
#include
#include
#include
#include
#define LTDataType int
typedef struct List
{
LTDataType data;
struct List* next;
struct List* prev;
}LTNode;
LTNode* LTInit();
void LTDestory(LTNode* phead);
void LTPrint(LTNode* phead);
void LTPushBack(LTNode* phead, LTDataType x);
void LTPopBack(LTNode* phead);
void LTPushFront(LTNode* phead, LTDataType x);
void LTPopFront(LTNode* phead);
LTNode* LTFind(LTNode* phead, LTDataType x);
void LTInsert(LTNode* pos, LTDataType x);//在pos前插入
void LTErase(LTNode* pos);//删除pos位置
#include"List.h"
LTNode* BuyNode(LTDataType x)
{
LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
if (NULL == newnode)
{
perror("malloc fail\n");
return NULL;
}
newnode->data = x;
newnode->prev = NULL;
newnode->next = NULL;
return newnode;
}
LTNode* LTInit()
{
LTNode* phead = BuyNode(-1);
phead->next = phead;
phead->prev = phead;
return phead;
}
void LTPrint(LTNode* phead)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
void LTPushBack(LTNode* phead, LTDataType x)
{
assert(phead);
LTNode* newnode = BuyNode(x);
LTNode* tail = phead->prev;
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;
}
bool LTEmpty(LTNode* phead)
{
assert(phead);
return phead->prev == phead;
}
void LTPopBack(LTNode* phead)
{
assert(phead);
assert(!LTEmpty(phead));
LTNode* tail = phead->prev;
LTNode* tailprev = tail->prev;
free(tail);
tailprev->next = phead;
phead->prev = tailprev;
}
void LTPushFront(LTNode* phead, LTDataType x)
{
assert(phead);
LTNode* newnode = BuyNode(x);
LTNode* first = phead->next;
phead->next = newnode;
newnode->prev = phead;
newnode->next = first;
first->prev = newnode;
}
void LTPopFront(LTNode* phead)
{
assert(phead);
assert(!LTEmpty(phead));
LTNode* first = phead->next;
LTNode* firstnext = first->next;
free(first);
phead->next = firstnext;
firstnext->prev = phead;
}
LTNode* LTFind(LTNode* phead, LTDataType x)
{
assert(phead);
assert(!LTEmpty(phead));
LTNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void LTInsert(LTNode* pos, LTDataType x)
{
assert(pos);
LTNode* newnode = BuyNode(x);
LTNode* prev = pos->prev;
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}
void LTErase(LTNode* pos)
{
assert(pos);
assert(!LTEmpty(pos));
LTNode* prev = pos->prev;
LTNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
}
#include"List.h"
void Test1()
{
LTNode* plist = LTInit();
LTPushBack(plist, 1);
LTPushBack(plist, 2);
LTPrint(plist);
LTNode* pos = LTFind(plist,2);
LTInsert(pos, 0);
LTPrint(plist);
LTErase(pos);
LTPrint(plist);
}
int main()
{
Test1();
return 0;
}
注意:如果删除哨兵位的头节点,会出错,但是C语言不太好解决这个问题,我们没必要加入一个参数去防止这种情况,以后在C++就没有这种问题了。