1.#include <stdio.h>
#include <stdlib.h>#include "DLinkList.h"/* run this program using the console pauser or add your own getch, system("pause") or input loop */struct Value{ DLinkListNode header; int v;};int main(int argc, char *argv[]){ int i = 0; DLinkList* list = DLinkList_Create(); struct Value* pv = NULL; struct Value v1; struct Value v2; struct Value v3; struct Value v4; struct Value v5; v1.v = 1; v2.v = 2; v3.v = 3; v4.v = 4; v5.v = 5; DLinkList_Insert(list, (DLinkListNode*)&v1, DLinkList_Length(list)); DLinkList_Insert(list, (DLinkListNode*)&v2, DLinkList_Length(list)); DLinkList_Insert(list, (DLinkListNode*)&v3, DLinkList_Length(list)); DLinkList_Insert(list, (DLinkListNode*)&v4, DLinkList_Length(list)); DLinkList_Insert(list, (DLinkListNode*)&v5, DLinkList_Length(list)); for(i=0; i<DLinkList_Length(list); i++) { pv = (struct Value*)DLinkList_Get(list, i); printf("%d\n", pv->v); } printf("\n"); DLinkList_Delete(list, DLinkList_Length(list)-1); DLinkList_Delete(list, 0); for(i=0; i<DLinkList_Length(list); i++) { pv = (struct Value*)DLinkList_Next(list); printf("%d\n", pv->v); } printf("\n"); DLinkList_Reset(list); DLinkList_Next(list); pv = (struct Value*)DLinkList_Current(list); printf("%d\n", pv->v); DLinkList_DeleteNode(list, (DLinkListNode*)pv); pv = (struct Value*)DLinkList_Current(list); printf("%d\n", pv->v); DLinkList_Pre(list); pv = (struct Value*)DLinkList_Current(list); printf("%d\n", pv->v); printf("Length: %d\n", DLinkList_Length(list)); DLinkList_Destroy(list); return 0;}2.#ifndef _DLINKLIST_H_
#define _DLINKLIST_H_typedef void DLinkList;typedef struct _tag_DLinkListNode DLinkListNode;struct _tag_DLinkListNode{ DLinkListNode* next; DLinkListNode* pre;};DLinkList* DLinkList_Create();void DLinkList_Destroy(DLinkList* list);void DLinkList_Clear(DLinkList* list);int DLinkList_Length(DLinkList* list);int DLinkList_Insert(DLinkList* list, DLinkListNode* node, int pos);DLinkListNode* DLinkList_Get(DLinkList* list, int pos);DLinkListNode* DLinkList_Delete(DLinkList* list, int pos);DLinkListNode* DLinkList_DeleteNode(DLinkList* list, DLinkListNode* node);DLinkListNode* DLinkList_Reset(DLinkList* list);DLinkListNode* DLinkList_Current(DLinkList* list);DLinkListNode* DLinkList_Next(DLinkList* list);DLinkListNode* DLinkList_Pre(DLinkList* list);#endif3.#include <stdio.h>
#include <malloc.h>#include "DLinkList.h"//struct typedef struct _tag_DLinkList{ DLinkListNode header; DLinkListNode* slider; int length;} TDLinkList;DLinkList* DLinkList_Create() // O(1){ TDLinkList* ret = (TDLinkList*)malloc(sizeof(TDLinkList)); if( ret != NULL ) { ret->length = 0; ret->header.next = NULL; ret->header.pre = NULL; ret->slider = NULL; } return ret;}void DLinkList_Destroy(DLinkList* list) // O(1){ free(list);}// clear list allvoid DLinkList_Clear(DLinkList* list) // O(1){ TDLinkList* sList = (TDLinkList*)list; if( sList != NULL ) { sList->length = 0; sList->header.next = NULL; sList->header.pre = NULL; sList->slider = NULL; }}//longint DLinkList_Length(DLinkList* list) // O(1){ TDLinkList* sList = (TDLinkList*)list; int ret = -1; if( sList != NULL ) { ret = sList->length; } return ret;}//insert dataint DLinkList_Insert(DLinkList* list, DLinkListNode* node, int pos) // O(n){ TDLinkList* sList = (TDLinkList*)list; int ret = (sList != NULL) && (pos >= 0) && (node != NULL); int i = 0; if( ret ) { //判断当前指针 DLinkListNode* current = (DLinkListNode*)sList; DLinkListNode* next = NULL; //开始遍历 for(i=0; (i<pos) && (current->next != NULL); i++) { current = current->next; } //进行insert 操作 next = current->next; //1 current->next = node; //2 node->next = next; //查看节点是否存在 if( next != NULL ) { //3 next->pre = node; } //4 node->pre = current; if( sList->length == 0 ) { node->pre = NULL; sList->slider = node; } sList->length++; } return ret;}//with pos get retDLinkListNode* DLinkList_Get(DLinkList* list, int pos) // O(n){ TDLinkList* sList = (TDLinkList*)list; DLinkListNode* ret = NULL; int i = 0; if( (sList != NULL) && (0 <= pos) && (pos < sList->length) ) { DLinkListNode* current = (DLinkListNode*)sList; for(i=0; i<pos; i++) { current = current->next; } ret = current->next; } return ret;}//delete dataDLinkListNode* DLinkList_Delete(DLinkList* list, int pos) // O(n){ TDLinkList* sList = (TDLinkList*)list; DLinkListNode* ret = NULL; int i = 0; if( (sList != NULL) && (0 <= pos) && (pos < sList->length) ) { DLinkListNode* current = (DLinkListNode*)sList; DLinkListNode* next = NULL; for(i=0; i<pos; i++) { current = current->next; } ret = current->next; // next = ret->next; //1 current->next = next; //最后一个元素 if( next != NULL ) { //2 next->pre = current; //若是删除是第一元素 if( current == (DLinkListNode*)sList ) { next->pre = NULL; } } if( sList->slider == ret ) { sList->slider = next; } sList->length--; } return ret;}DLinkListNode* DLinkList_DeleteNode(DLinkList* list, DLinkListNode* node){ TDLinkList* sList = (TDLinkList*)list; DLinkListNode* ret = NULL; int i = 0; if( sList != NULL ) { DLinkListNode* current = (DLinkListNode*)sList; for(i=0; i<sList->length; i++) { if( current->next == node ) { ret = current->next; break; } current = current->next; } if( ret != NULL ) { DLinkList_Delete(sList, i); } } return ret;}DLinkListNode* DLinkList_Reset(DLinkList* list){ TDLinkList* sList = (TDLinkList*)list; DLinkListNode* ret = NULL; if( sList != NULL ) { sList->slider = sList->header.next; ret = sList->slider; } return ret;}DLinkListNode* DLinkList_Current(DLinkList* list){ TDLinkList* sList = (TDLinkList*)list; DLinkListNode* ret = NULL; if( sList != NULL ) { ret = sList->slider; } return ret;}//get next retDLinkListNode* DLinkList_Next(DLinkList* list){ TDLinkList* sList = (TDLinkList*)list; DLinkListNode* ret = NULL; if( (sList != NULL) && (sList->slider != NULL) ) { ret = sList->slider; sList->slider = ret->next; } return ret;}//get Pre retDLinkListNode* DLinkList_Pre(DLinkList* list){ TDLinkList* sList = (TDLinkList*)list; DLinkListNode* ret = NULL; if( (sList != NULL) && (sList->slider != NULL) ) { ret = sList->slider; sList->slider = ret->pre; } return ret;}