代码清单
// 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;
}