扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include#include#include
#includetypedef int LTDataType;
typedef struct DListNode
{
struct DListNode* next;
struct DListNode* prev;
LTDataType data;
}LTNode;
LTNode* BuyListNode(LTDataType x);
LTNode* ListInit();
void ListPushBack(LTNode* phead,LTDataType x);
void ListPopBack(LTNode* phead);
void ListPushFront(LTNode* phead, LTDataType x);
void ListPopFront(LTNode* phead);
void LTPrint(LTNode* phead);
LTNode* LTFind(LTNode* phead,LTDataType x);
//在pos之前位置插入x
void ListInsert(LTNode* pos,LTDataType x);
//删除pos位置
void ListErase(LTNode* pos);
bool LTEmpty(LTNode* phead);
size_t LTSize(LTNode* phead);
void LTDestroy(LTNode* phead);
函数的实现:对于insert函数来说,完全可以替代头插尾插,设置好参数就可以了
创新互联主营公安网站建设的网络公司,主营网站建设方案,app软件开发公司,公安h5小程序制作搭建,公安网站营销推广欢迎公安等地区企业咨询
#define _CRT_SECURE_NO_WARNINGS 1
#include "DList.h"
LTNode* BuyListNode(LTDataType x)
{
LTNode* node = (LTNode*)malloc(sizeof(LTNode));
if (node == NULL)
{
perror("malloc fail");
exit(-1);
}
node->data = x;
node->next = NULL;
node->prev = NULL;
return node;
}
LTNode* ListInit()
{
LTNode* phead = BuyListNode(-1);
phead->next = phead;
phead->prev = phead;
return phead;
}
void ListPushBack(LTNode* phead, LTDataType x)
{
assert(phead);
LTNode* newnode = BuyListNode(x);
LTNode* tail = phead->prev;
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;
//ListInsert(phead, x);
}
void ListPopBack(LTNode* phead)
{
assert(phead);
assert(phead->next != phead);
LTNode* tail = phead->prev;
LTNode* tailPrev = tail->prev;
tailPrev->next = phead;
phead->prev = tailPrev;
free(tail);
//ListErase(phead->prev);
}
void ListPushFront(LTNode* phead,LTDataType x)
{
assert(phead);
LTNode* newnode = BuyListNode(x);
LTNode* next = phead->next;
phead->next->prev = newnode;
phead->next = newnode;
newnode->prev = phead;
//ListInsert(phead->next, x);
}
void ListPopFront(LTNode* phead)
{
assert(phead);
assert(phead->next != phead);
LTNode* first = phead->next;
LTNode* second = first->next;
free(first);
phead->next = second;
second->prev = phead;
//ListErase(phead->next);
}
void LTPrint(LTNode* phead)
{
assert(phead);
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)
{
printf("找到了");
return cur;
}
cur = cur->next;
}
return NULL;
}
//在pos之前位置插入x
void ListInsert(LTNode* pos, LTDataType x)
{
assert(pos);
LTNode* prev = pos->prev;
LTNode* newnode = BuyListNode(x);
prev->next = newnode;
newnode->prev = prev;
pos->prev = newnode;
}
//删除pos位置
void ListErase(LTNode* pos)
{
LTNode* prev = pos->prev;
LTNode* next = pos->next;
free(pos);
prev->next = next;
next->prev = prev;
}
bool LTEmpty(LTNode* phead)
{
assert(phead);
return phead->next == phead;
}
size_t LTSize(LTNode* phead)
{
assert(phead);
size_t size = 0;
LTNode* cur = phead->next;
while (cur != phead)
{
++size;
cur = cur->next;
}
return size;
}
void LTDestroy(LTNode* phead)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
LTNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
}
测试用例:#include "DList.h"
void test1()
{
LTNode* phead = ListInit();
ListPushBack(phead, 1);
LTPrint(phead);
ListPushBack(phead, 2);
LTPrint(phead);
ListPushBack(phead, 3);
LTPrint(phead);
ListPushBack(phead, 4);
LTPrint(phead);
ListPopBack(phead);
ListPopBack(phead);
ListPopBack(phead);
LTPrint(phead);
ListPopFront(phead);
//ListPopFront(phead);
LTPrint(phead);
LTNode* pos = LTFind(phead, 4);
if (pos != NULL)
{
printf("%d ", pos->data);
}
}
int main()
{
test1();
return 0;
}
这里只简单的测试了一点,需要的可以自行编辑运行
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流