双向带头循环链表
先写好头文件
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int LTDataType;
typedef struct ListNode
{
struct ListNode* next;
struct ListNode* prev;
LTDataType data;
}LTNode;
LTNode* LTInit();
void LTPrint(LTNode* phead);
bool LTEmpty(LTNode* phead);
void SLTPusBack(LTNode* phead, LTDataType x);
void SLTPusFront(LTNode* phead, LTDataType x);
void SLTPopBack(LTNode* phead);
void SLTPopFront(LTNode* phead);
LTNode* LTFind(LTNode* phead, LTDataType x);
//在pos之前插入
void LTInsert(LTNode* pos, LTDataType x);
void LTErase(LTNode* pos);
void LTDestroy(LTNode* phead);
再实现具体的函数代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "List.h"
LTNode* BuyLTNode(LTDataType x)
{
LTNode* newnode =(LTNode*) malloc(sizeof(LTNode));
if (newnode == NULL)
{
perror("malloc fail");
return NULL;
}
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
//初始化,并默认给头节点
LTNode* LTInit()
{
LTNode* phead = BuyLTNode(-1);
phead->next = phead;
phead->prev = phead;
return phead;
}
bool LTEmpty(LTNode* phead)
{
assert(phead);
return phead->next == phead;
}
void SLTPusBack(LTNode* phead, LTDataType x)
{
/*assert(phead);
LTNode* tail = phead->prev;
LTNode* newnode = BuyLTNode(x);
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;*/
LTInsert(phead, x);
}
void SLTPusFront(LTNode* phead, LTDataType x)
{
/*assert(phead);
LTNode* newnode = BuyLTNode(x);
newnode->next = phead->next;
phead->next->prev = newnode;
phead->next = newnode;
newnode->prev = phead;*/
LTInsert(phead->next, x);
}
void SLTPopBack(LTNode* phead)
{
assert(phead);
assert(!LTEmpty(phead));
LTErase(phead->prev);
/*LTNode* tail = phead->prev;
LTNode* tailPrev = tail->prev;
free(tail);
tailPrev->next = phead;
phead->prev = tailPrev;*/
}
void SLTPopFront(LTNode* phead)
{
assert(phead);
assert(!LTEmpty(phead));
LTErase(phead->next);
/*LTNode* first = phead->next;
LTNode* second = first->next;
phead->next = second;
second->prev = phead;
free(first);*/
}
void LTPrint(LTNode* phead)
{
assert(phead);
printf("guard<==>");
LTNode* cur = phead->next;
while (cur != phead)
{
printf("%d<==>",cur->data);
cur = cur->next;
}
printf("\n");
}
LTNode* LTFind(LTNode* phead, LTDataType x)
{
assert(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* prev = pos->prev;
LTNode* newnode = BuyLTNode(x);
//prev newnode pos
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}
void LTErase(LTNode* pos)
{
assert(pos);
LTNode* posPrev = pos->prev;
LTNode* posNext = pos->next;
posPrev->next = posNext;
posNext->prev = posPrev;
free(pos);
}
void LTDestroy(LTNode* phead)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
LTNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
}
最终的测试代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "List.h"
void TestList1()
{
LTNode* plist = LTInit();
SLTPusBack(plist, 1);
SLTPusBack(plist, 2);
SLTPusBack(plist, 3);
SLTPusBack(plist, 4);
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
void TestList2()
{
LTNode* plist = LTInit();
SLTPusFront(plist, 1);
SLTPusFront(plist, 2);
SLTPusFront(plist, 3);
SLTPusFront(plist, 4);
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
void TestList3()
{
LTNode* plist = LTInit();
SLTPusFront(plist, 1);
SLTPusFront(plist, 2);
SLTPusFront(plist, 3);
SLTPusFront(plist, 4);
LTPrint(plist);
SLTPopFront(plist);
LTPrint(plist);
SLTPopFront(plist);
LTPrint(plist);
SLTPopFront(plist);
LTPrint(plist);
SLTPopFront(plist);
LTPrint(plist);
//SLTPopFront(plist);
//LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
void TestList4()
{
LTNode* plist = LTInit();
SLTPusFront(plist, 1);
SLTPusFront(plist, 2);
SLTPusFront(plist, 3);
SLTPusFront(plist, 4);
LTPrint(plist);
LTNode* pos = LTFind(plist, 3);
if (pos)
{
LTInsert(pos, 30);
}
LTPrint(plist);
LTDestroy(plist);
plist = NULL;
}
int main()
{
TestList4();
return 0;
}