LinkedList.h
/**
* 模拟实现双向循环链表
*/
typedef struct _Entry Entry;
typedef struct _LinkedList LinkedList;
/*定义节点类型*/
struct _Entry {
int data; //元素值
struct _Entry *next; //下一个节点
struct _Entry *prev; //上一个节点
};
/*定义集合*/
struct _LinkedList {
int size; //数量
struct _Entry *head; //头节点
} ;
LinkedList* initList();
Entry* createEntry(int data, Entry *next, Entry *prev);
Entry* add(LinkedList *list, int data);
Entry* addFirst(LinkedList *list,int data);
Entry* add1(LinkedList *list, int index, int data);
int removeEntry(LinkedList *list, int data);
int removeIndex(LinkedList *list, int index);
Entry* entry(LinkedList *list, int index);
Entry* addBefore(int data, Entry *next);
int size(LinkedList *list);
void freeEntry(Entry *entry);
int isEmpty (LinkedList *list);
void iteratorList(LinkedList *list);
void freeList(LinkedList *list);
LinkedList.c
/*定义节点类型*/ #include "LinkedList.h"
#include
#include
#include
/* 初始化*/
LinkedList* initList() {
LinkedList *list = (LinkedList *) malloc(sizeof(LinkedList));
//printf("申请list空间:%p\n",list);
//构造头节点
Entry *head = createEntry(0, NULL, NULL);
//printf("申请head空间:%p\n",head);
list->size = 0;
list->head = head;
//默认是空循环链表
head->next = head->prev = head;
return list;
}
/*创建节点元素*/
Entry* createEntry(int data, Entry *next, Entry *prev) {
Entry *entry = (Entry *) malloc(sizeof(Entry));
entry->data = data;
entry->next = next;
entry->prev = prev;
return entry;
}
/*添加节点至集合*/
Entry* add(LinkedList *list, int data) {
Entry *newEntry = addBefore(data, list->head);
list->size++;
return newEntry;
}
/*新节点添加在首位*/
Entry* addFirst(LinkedList *list, int data) {
Entry *newEntry = addBefore(data, list->head->next);
list->size++;
return newEntry;
}
/*添加节点在index下标之前*/
Entry* add1(LinkedList *list, int index, int data) {
//先查找下标元素
Entry *e = entry(list, index);
return addBefore(data, e);
}
/*添加在next节点之前*/
Entry* addBefore(int data, Entry *next) {
Entry *newEntry = createEntry(data, next, next->prev);
//printf("申请data%d空间:%p\n",data,newEntry);
newEntry->next->prev = newEntry;
newEntry->prev->next = newEntry;
return newEntry;
}
/*删除元素*/
int removeEntry(LinkedList *list, int data) {
Entry *e = list->head->next;
while (e != list->head) {
if (data == e->data) {
e->prev->next = e->next;
e->next->prev = e->prev;
freeEntry(e);
list->size--;
return 1;
}
e = e->next;
}
return 0;
}
/*删除指定下标的元素*/
int removeIndex(LinkedList *list, int index) {
Entry *e = entry(list, index);
//没找到对应的元素
if(e == NULL) {
return 0;
}
else {
return removeEntry(list,e->data);
}
}
/*通过下标查找元素*/
Entry* entry(LinkedList *list, int index) {
int i;
Entry *e = list->head;
if (index < 0 || index >= list->size) {
return 0;
}
if (index < (list->size >> 1)) {
for (i = 0; i <= index; i++) {
e = e->next;
}
} else {
for (i = list->size; i > index; i--) {
e = e->prev;
}
}
return (e == list->head) ? NULL : e;
}
/*当前集合数量*/
int size(LinkedList *list) {
return list->size;
}
/*遍历集合*/
void iteratorList(LinkedList *list) {
puts("开始遍历集合");
if (isEmpty(list)) {
printf("集合为空!");
return;
}
Entry *e = list->head->next;
while (e != list->head) {
printf("data:%d\n", e->data);
e = e->next;
}
puts("遍历完成");
}
/*释放节点分配的空间*/
void clearList(LinkedList *list) {
//puts("开始释放节点");
//printf("head:%d,地址:%p\n",list->head->data,list->head);
Entry *e = list->head->next;
Entry *next = NULL;
while (e != list->head) {
next = e->next;
//printf("释放地址:%d,地址:%p\n", e->data,e);
freeEntry(e);
e = next;
}
list->size = 0;
//puts("完成释放节点");
//free(list);
}
/*释放LinkedList空间*/
void freeList(LinkedList *list) {
puts("开始释放内存!");
clearList(list);
list->size = 0;
freeEntry(list->head);
//printf("释放完成头节点:%p\n",list->head);
free(list);
//printf("释放list:%p\n",list);
puts("结束释放内存!");
}
/*释放节点*/
void freeEntry(Entry *entry) {
if(entry !=NULL) {
free(entry);
}
}
/*判断集合是否为空*/
int isEmpty(LinkedList *list) {
return (list->size == 0) ? 1 : 0;
}
Main函数测试
int main() { setbuf(stdout, NULL); //初始化集合 LinkedList *list = initList(); int i ; //循环添加节点 Entry *entry = NULL; for(i = 1; i < 5; i++) { entry = add(list,i); printf("new entry:%d,%p\n",entry->data,entry); } //添加节点至第一位 addFirst(list,20); //添加在下标=1节点之前 add1(list,1,15); printf("size:%d\n",size(list)); //遍历节点 iteratorList(list); //删除元素为40的节点 removeEntry(list, 40); //删除元素下标为2的节点 removeIndex(list,2); //遍历节点 iteratorList(list); printf("size:%d\n",size(list)); //释放空间 freeList(list); exit(0); }