资源简介
《系统程序员成长计划》源码
代码片段和文件信息
/*
* File: dlist.c
* Author: Li XianJing
* Brief: double list implementation.
*
* Copyright (c) Li XianJing
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not write to the Free Software
* Foundation Inc. 59 Temple Place Suite 330 Boston MA 02111-1307 USA
*/
/*
* History:
* ================================================================
* 2008-11-09 Li XianJing created
*
*/
#include
#include “dlist.h“
typedef struct _DListNode
{
struct _DListNode* prev;
struct _DListNode* next;
void* data;
}DListNode;
struct _DList
{
DListNode* first;
};
static DListNode* dlist_node_create(void* data)
{
DListNode* node = malloc(sizeof(DListNode));
if(node != NULL)
{
node->prev = NULL;
node->next = NULL;
node->data = data;
}
return node;
}
static void dlist_node_destroy(DListNode* node)
{
if(node != NULL)
{
node->next = NULL;
node->prev = NULL;
free(node);
}
return;
}
DList* dlist_create(void)
{
DList* thiz = malloc(sizeof(DList));
if(thiz != NULL)
{
thiz->first = NULL;
}
return thiz;
}
static DListNode* dlist_get_node(DList* thiz size_t index int fail_return_last)
{
DListNode* iter = thiz->first;
while(iter != NULL && iter->next != NULL && index > 0)
{
iter = iter->next;
index--;
}
if(!fail_return_last)
{
iter = index > 0 ? NULL : iter;
}
return iter;
}
DListRet dlist_insert(DList* thiz size_t index void* data)
{
DListNode* node = NULL;
DListNode* cursor = NULL;
if((node = dlist_node_create(data)) == NULL)
{
return DLIST_RET_OOM;
}
if(thiz->first == NULL)
{
thiz->first = node;
return DLIST_RET_OK;
}
cursor = dlist_get_node(thiz index 1);
if(index < dlist_length(thiz))
{
if(thiz->first == cursor)
{
thiz->first = node;
}
else
{
cursor->prev->next = node;
node->prev = cursor->prev;
}
node->next = cursor;
cursor->prev = node;
}
else
{
cursor->next = node;
node->prev = cursor;
}
return DLIST_RET_OK;
}
DListRet dlist_prepend(DList* thiz void* data)
{
return dlist_insert(thiz 0 data);
}
DListRet dlist_append(DList* thiz void* data)
{
return dlist_insert(thiz -1 data);
}
DListRet dlist_delete(DList* thiz size_t index)
{
DListNode* cursor = dlist_get_node(thiz index 0);
if(cursor != NULL)
{
if(cursor == thiz->first)
{
thiz->first =
- 上一篇:中国城市统计年鉴2016
- 下一篇:PCap01-开发板
相关资源
- Qt实现动态曲线图
- 植物大战僵尸修改器源码
- 非常精美的AngularJS框架后台管理系统
- msc c51单片机各实用源码及proteus仿真集
- 编译原理课程设计
- STM32F103+Modbus通信源码
- 蜀门SF登录器源码
- 2017最全的vivado license 源码下载43419
- 智能拼图游戏.NET 开发源码
- OPENSSL算法库之rc4算法使用
- StarRat源码 功能强大的远控
- SPI网络数据包拦截程序源码
- 人脸识别三套源码含小程序源码亲测
- 传智播客IOS视频源码
- 单周期源码.zip
- 完整版APDS9960手势识别源码
- 趣味桌球源码-CocosCreator
- 微金所网站案例源码
- GEC210 Linux驱动源码
- ZXing二维码扫描简洁版中文注释
- Dlion-开源固件源码V03版本及说明.rar
- 易语言多开源码
- 实验室设备管理系统(课程设计)源
- 基于winpcap的网络入侵检测系统(源码
- darknet v1
- 音频信号分析仪的FPGA源码
- QQ堂源码
- 南方数据1.0源码
- 六机器人STM32F103控制程序源码
- 蜘蛛机器人STM32F103控制程序源码
评论
共有 条评论