双链表
头文件
#ifndef _DLINKED_LIST_H__
#define _DLINKED_LIST_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int ETYPE;
typedef struct DNode{
ETYPE elem;
struct DNode *prev;
struct DNode *next;
}DNode,*DLinkedList;
#define DNODESIZE sizeof(struct DNode)
DLinkedList dlinked_list_create(void);
bool dlinked_list_empty(DLinkedList head);
size_t dlinked_list_size(DLinkedList head);
int dlinked_list_insert(DLinkedList head,size_t pos,ETYPE elem);
int dlinked_list_delete(DLinkedList head,size_t pos,ETYPE *pelem);
int dlinked_list_get(DLinkedList head,size_t pos,ETYPE *pelem);
ETYPE *dlinked_list_index(DLinkedList head,size_t pos);
ETYPE *dlinked_list_find(DLinkedList head,ETYPE *pelem);
void dlinked_list_clear(DLinkedList head);
void dlinked_list_destroy(DLinkedList head);
#endif
实现函数
#include "dlinkedlist.h"
static struct DNode *create_dnode(ETYPE elem,struct DNode *prev,struct DNode *next){
struct DNode *node = (struct DNode *)malloc(DNODESIZE);
if(node != NULL){
node->elem = elem;
node->prev = prev;
node->next = next;
}
return node;
}
DLinkedList dlinked_list_create(void){
return create_dnode(0,NULL,NULL);
}
bool dlinked_list_empty(DLinkedList head){
return head->next == NULL;
}
size_t dlinked_list_size(DLinkedList head){
size_t size = 0;
struct DNode *node = head->next;
for(;node!=NULL;node=node->next){
++size;
}
return size;
}
static struct DNode *dlinked_list_get_prev_node(DLinkedList head,size_t pos){
size_t i;
struct DNode *node = head;
for(i=0;i<pos&&node!=NULL;++i){
node = node->next;
}
return node;
}
int dlinked_list_insert(DLinkedList head,size_t pos,ETYPE elem){
struct DNode *prev = dlinked_list_get_prev_node(head,pos);
if(prev == NULL){
return -1;
}
struct DNode *curr = create_dnode(elem,prev,prev->next);
if(curr == NULL){
return -1;
}
if(prev->next != NULL)
prev->next->prev = curr;
prev->next = curr;
return 0;
}
int dlinked_list_delete(DLinkedList head,size_t pos,ETYPE *pelem){
struct DNode *prev = dlinked_list_get_prev_node(head,pos);
if(prev == NULL || prev->next == NULL){
return -1;
}
struct DNode *curr = prev->next;
*pelem = curr->elem;
if(curr->next!=NULL)
curr->next->prev = prev;
prev->next = curr->next;
free(curr);
return 0;
}
int dlinked_list_get(DLinkedList head,size_t pos,ETYPE *pelem){
struct DNode *curr = dlinked_list_get_prev_node(head,pos+1);
if(curr == NULL){
return -1;
}
*pelem = curr->elem;
return 0;
}
ETYPE *dlinked_list_index(DLinkedList head,size_t pos){
struct DNode *curr = dlinked_list_get_prev_node(head,pos+1);
if(curr == NULL){
return NULL;
}
return &curr->elem;
}
ETYPE *dlinked_list_find(DLinkedList head,ETYPE *pelem){
struct DNode *node = head->next;
for(;node!=NULL;node = node->next){
if(node->elem == *pelem){
return &node->elem;
}
}
return NULL;
}
void dlinked_list_clear(DLinkedList head){
struct DNode *node,*next;
for(node=head->next;node!=NULL;node = next){
next = node->next;
free(node);
}
head->next = NULL;
}
void dlinked_list_destroy(DLinkedList head){
dlinked_list_clear(head);
free(head);
}