代码清单

 // linkedlist.h
#ifndef __LINKEDLIST_H__
#define __LINKEDLIST_H__ #include <assert.h>
#include <malloc.h>
#include <string.h> typedef int LinkedListData; typedef struct LinkedListNode {
LinkedListData data; struct LinkedListNode * prior;
struct LinkedListNode * next;
} LinkedListNode, *LinkedList; typedef enum {
TRAVELDIR_FORWARD, TRAVELDIR_BACKWARD
} LinkedListTravelDir; LinkedList linkedlist_new();
void linkedlist_destory(LinkedList *list); void linkedlist_insert(LinkedList list, LinkedListTravelDir dir, int location,
const LinkedListData data);
void linkedlist_delete(LinkedList list, LinkedListTravelDir dir, int location);
int linkedlist_locate(const LinkedList list, LinkedListTravelDir dir,
const LinkedListData data, int (*fpCompare)(const void *, const void *));
LinkedListData * linkedlist_get(LinkedList list, LinkedListTravelDir dir,
int location);
int linkedlist_length(const LinkedList list); #endif // __LINKEDLIST_H__ // linkedlist.c
#include "linkedlist.h" LinkedList linkedlist_new() {
// alloc head node
LinkedList list = malloc(sizeof(LinkedListNode));
assert(list); // initialize head node's pointer field
list->prior = list;
list->next = list; return list;
} void linkedlist_destory(LinkedList *list) {
while (linkedlist_length(*list)) {
linkedlist_delete(*list, TRAVELDIR_FORWARD, );
}
free (*list);
*list = NULL;
} void linkedlist_insert(LinkedList list, LinkedListTravelDir dir, int location,
const LinkedListData data) {
LinkedList pCurNode = list;
assert(location > && location <= linkedlist_length(list) + ); // alloc new node
LinkedListNode * pNode = malloc(sizeof(LinkedListNode));
assert(pNode);
memcpy(&(pNode->data), &data, sizeof(LinkedListData)); // move current pointer to prior node
if (dir == TRAVELDIR_FORWARD) {
for (int i = ; i < location - ; i++, pCurNode = pCurNode->next)
; } else {
if (dir == TRAVELDIR_BACKWARD) {
for (int i = ; i < location; i++, pCurNode = pCurNode->prior)
;
}
} // insert new node
pNode->next = pCurNode->next;
pNode->prior = pCurNode;
pCurNode->next = pNode;
pNode->next->prior = pNode;
} void linkedlist_delete(LinkedList list, LinkedListTravelDir dir, int location) {
LinkedList pCurNode = list;
assert(location > && location < linkedlist_length(list) + ); // move current pointer to the node will deleted
if (dir == TRAVELDIR_FORWARD) {
for (int i = ; i < location; i++, pCurNode = pCurNode->next)
;
} else {
if (dir == TRAVELDIR_BACKWARD) {
for (int i = ; i < location; i++, pCurNode = pCurNode->prior)
;
}
} // delete current node
pCurNode->prior->next = pCurNode->next;
pCurNode->next->prior = pCurNode->prior;
free(pCurNode);
} int linkedlist_locate(const LinkedList list, LinkedListTravelDir dir,
const LinkedListData data, int (*fpCompare)(const void *, const void *)) {
static int location = ;
static LinkedList pCurNode = NULL; // if list argument is NULL, continue to start locate
if (list) {
location = ;
pCurNode = list->next;
}
assert(location && pCurNode); // locate data
while (pCurNode != list) {
if (!fpCompare(&(pCurNode->data), &data)) {
return location;
}
location++;
if (dir == TRAVELDIR_FORWARD) {
pCurNode = pCurNode->next;
} else {
if (dir == TRAVELDIR_BACKWARD) {
pCurNode = pCurNode->prior;
}
}
} return -;
} LinkedListData * linkedlist_get(LinkedList list, LinkedListTravelDir dir,
int location) {
LinkedList pCurNode = list;
assert(location > && location < linkedlist_length(list) + ); // move pointer to the node wanna get
if (dir == TRAVELDIR_FORWARD) {
for (int i = ; i < location; i++, pCurNode = pCurNode->next)
;
} else {
if (dir == TRAVELDIR_BACKWARD) {
for (int i = ; i < location; i++, pCurNode = pCurNode->prior)
;
}
} return &(pCurNode->data);
} int linkedlist_length(const LinkedList list) {
int length = ;
assert(list);
LinkedList pCurNode = list->next; while (pCurNode != list) {
length++;
pCurNode = pCurNode->next;
} return length;
}

最新文章

  1. EF查询之性能优化技巧
  2. Javascript字符串
  3. Linux下paste命令
  4. DataTable 更改在有数据列的类型方法+DataTable 导出excel功能
  5. PHP和CS的引用传值
  6. 【转】ios app 应用内购买配置完全指南
  7. Don&#39;t Repeat Yourself (不要重复你自己)
  8. PHP标签的格式
  9. HTML5 canvas绘制雪花飘落动画(需求分析、知识点、程序编写分布详解)
  10. apache日志介绍
  11. Linux Yum仓库介绍及服务端及客户端配置
  12. jQuery获取Select选中的Text和Value,根据Value值动态添加属性等
  13. 数据库系统概论——Chap. 1 Introduction
  14. 【Toll!Revisited(uva 10537)】
  15. Docker安装步骤
  16. python中的__metaclass__
  17. Android异步消息传递机制源码分析
  18. docker 与启动后的镜像进行交互
  19. Python—字符编码转换、函数基本操作
  20. The perception and large margin classifiers

热门文章

  1. ue4加载界面(loadingscreen)的实现
  2. Matlab: 路径的操作
  3. csv导入数据到mysql
  4. 关于Client_Abort_Exception异常的分析和解决
  5. 10.解决VUEX刷新的时候出现数据消失
  6. Java基础(7)-异常处理
  7. 基于GCC的openMP学习与测试(2)
  8. XML 新手入门基础知识
  9. C++ 宏定义#define 中##的使用
  10. JS实现为控件添加倒计时功能