#define _CRT_SECURE_NO_WARNINGS 1
#include"LinkList.h"
//申请空间
LTNode* BuyLTNode(LTtypeData x)
{
LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
if (newnode == NULL)
{
perror("malloc ");
exit(-1);
}
newnode->next = NULL;
newnode->prev = NULL;
newnode->data = x;
return newnode;
}
//打印测试链表
void Print(LTNode* phead)
{
/*assert(phead);
LTNode* cur = phead->next;
printf("print ");
while (cur != phead);
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");*/
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
//初始化链表
LTNode* ListInit()
{
LTNode* phead = BuyLTNode(-1);
phead->next = phead;
phead->prev = phead;
return phead;
}
//链表的尾插
void LTPushBack(LTNode* phead, LTtypeData x)
{
/*assert(phead);
LTNode* newnode = BuyLTNode(x);
LTNode* tail = phead->prev;
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;*/
/*assert(phead);
LTNode* newnode = BuyLTNode(x);
LTNode* tail = phead->prev;
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;*/
LTInsert(phead, x);
}
//链表的尾删除
void LTPopBack(LTNode* phead)
{
/*assert(phead);
assert(phead->next != phead);
LTNode* tail = phead->prev;
LTNode* tailprev = tail->prev;
phead->prev = tailprev;
tailprev->next = phead;
free(tail);*/
LTErase(phead->prev);
}
//链表的头插
void LTPushFront(LTNode* phead, LTtypeData x)
{
/*assert(phead);
LTNode* tail = phead->next;
LTNode* newnode = BuyLTNode(x);
tail->prev = newnode;
newnode->next = tail;
newnode->prev = phead;
phead->next = newnode;*/
LTInsert( phead->next, x);
}
//链表的头删除
void LTPopFront(LTNode* phead)
{
/*assert(phead);
LTNode* first = phead->next;
phead->next = first->next;
first->next->prev = phead;*/
LTErase(phead->next);
}
//链表数据查找
LTNode* LTFind(LTNode* phead, LTtypeData x)
{
assert(phead);
LTNode* cur = phead;
while (cur->next != cur )
{
if (x == cur->data)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
//链表的指定位置插入
void LTInsert(LTNode* pos, LTtypeData x)
{
assert(pos);
LTNode* first = pos->prev;
LTNode* newnode = BuyLTNode(x);
first->next = newnode;
newnode->prev = first;
pos->prev = newnode;
newnode->next = pos;
}
//链表的删除
void LTErase(LTNode* pos)
{
assert(pos);
LTNode* prev = pos->prev;
LTNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
}
//判断链表是否为空
bool LTEmpty(LTNode* phead)
{
/*if (phead->next == phead)
return true;
else
return false;*/
return phead->next == phead;
}
//计算链表的长度
size_t LTSize(LTNode* phead)
{
LTNode* cur = phead;
size_t count = 0;
while (phead != cur->next)
{
count++;
cur = cur->next;
}
return count;
}
//释放链表
void LTDestroy(LTNode* phead)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
LTNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
}
今天我学习了数据结构中的双链表,他非常的简单,对数据的操作也是非常的简便。双链表虽然看起来挺复杂,但是实现起来估计比单链表还简单。