- #include <stdio.h>
- #include <stdlib.h>
-
- typedef enum _DListReturn
- {
- DLIST_RETURN_OK,
- DLIST_RETURN_FAIL
- }DListReturn;
-
- typedef struct _DStu
- {
- int score;
- }DStu;
-
- typedef struct _DListNode
- {
- struct _DListNode* prev;
- struct _DListNode* next;
-
- DStu* data;
-
- }DListNode;
-
- typedef struct _DList
- {
- DListNode* head;
- }DList;
-
- typedef DListReturn (*DListPrintFunction)(void* data);
-
- DListNode* dlist_node_create(void* data)
- {
- DListNode* node;
- if((node = (DListNode*) malloc(sizeof(DListNode)))==NULL)
- {
- printf("分配空间失败!");
- exit(0);
- }
-
- if(node != NULL)
- {
- node->prev = NULL;
- node->next = NULL;
- node->data =(DStu*)data;
- }
-
- return node;
- }
-
- DList* dlist_head_create(void)
- {
- DList* thiz;
- if((thiz = (DList*)malloc(sizeof(DList)))==NULL)
- {
- printf("分配空间失败!");
- exit(0);
- }
-
- if(thiz != NULL)
- {
- thiz->head = NULL;
- }
-
- return thiz;
- }
-
- DListReturn dlist_append(DList* thiz, void* data)
- {
- DListNode* node = NULL;
- DListNode* cursor = NULL;
-
- if((node = dlist_node_create(data)) == NULL)
- {
- return DLIST_RETURN_FAIL;
- }
-
- if(thiz->head == NULL)
- {
- thiz->head = node;
-
- return DLIST_RETURN_OK;
- }
-
- cursor = thiz->head;
- while(cursor != NULL && cursor->next != NULL)
- {
- cursor = cursor->next;
- }
-
- cursor->next = node;
- node->prev = cursor;
-
- return DLIST_RETURN_OK;
- }
-
- <span style="color:#ff0000;">DListReturn dlist_prepend(DList* thiz, void* data)
- {
- DListNode* node = NULL;
- DListNode* cursor = NULL;
-
- if((node = dlist_node_create(data)) == NULL)
- {
- return DLIST_RETURN_FAIL;
- }
-
- if(thiz->head == NULL)
- {
- thiz->head = node;
-
- return DLIST_RETURN_OK;
- }
-
- cursor = thiz->head;
-
- if(thiz->head == cursor)
- thiz->head = node;
-
- node->next = cursor;
- cursor->prev = node;
-
- return DLIST_RETURN_OK;
- }
-
- </span>DListReturn dlist_print(DList* thiz, DListPrintFunction print)
- {
- DListNode* iter = thiz->head;
-
- while(iter != NULL)
- {
- print(iter->data);
- iter = iter->next;
- }
- printf("\n");
- return DLIST_RETURN_OK;
- }
-
- DListReturn print_int(void* data)
- {
- DStu* ss=(DStu*)data;
- printf("%d\t ", ss->score);
-
- return DLIST_RETURN_OK;
- }
-
- DListReturn dlist_node_destroy(DListNode* node)
- {
- if(node != NULL)
- {
- node->next = NULL;
- node->prev = NULL;
- free(node);
- }
-
- return DLIST_RETURN_OK;
- }
-
- DListReturn dlist_destroy(DList* thiz)
- {
- DListNode* iter = thiz->head;
- DListNode* next = NULL;
-
- while(iter != NULL)
- {
- next = iter->next;
- dlist_node_destroy(iter);
- iter = next;
- }
-
- thiz->head = NULL;
- free(thiz);
-
- return DLIST_RETURN_OK;
- }
-
- int main(int argc, char* argv[])
- {
- int i = 0;
-
- DList* dlist = dlist_head_create();
-
- for(i = 0; i < 7; i++)
- {
- DStu* stu =(DStu*) malloc(sizeof(DStu));
- stu->score = i;
- dlist_append(dlist, (void*)stu);
- }
- <span style="color:#ff0000;"> for(i = 0; i < 7; i++)
- {
- DStu* stu =(DStu*) malloc(sizeof(DStu));
- stu->score = i;
- dlist_prepend(dlist, (void*)stu);
- }</span>
- dlist_print(dlist, print_int);
- dlist_destroy(dlist);
- return 0;
- }
|