嵌入式 双向链表的头尾中插入法以及释放

  1. BIN = DoubleListDemo
  2. CC = gcc
  3. SRC = DoubleList.c main.c
  4. OBJS = DoubleList.o main.o
  5. CFLAGS = -Wall -O2
  6. $(BIN): $(OBJS)
  7. $(CC) -o $(BIN) $(OBJS) $(CFLAGS)
  8. $(OBJS): $(SRC)
  9. gcc -c $(SRC) $(CFLAGS)
  10. clean:
  11. rm -rf *.o
  12. rm -rf $(BIN)
  13. distclean:
  14. -rm *.o
BIN     =  DoubleListDemo
CC = gcc

SRC =  DoubleList.c main.c 
OBJS = DoubleList.o main.o

CFLAGS  =  -Wall -O2

$(BIN): $(OBJS)
        $(CC) -o $(BIN) $(OBJS) $(CFLAGS)
$(OBJS): $(SRC) 
        gcc -c $(SRC) $(CFLAGS)


clean:
        rm -rf *.o
        rm -rf $(BIN)
distclean:
        -rm *.o 


DoubleList.h

[cpp] viewplain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef int KEY;
  5. typedef struct {
  6. KEY key;
  7. int date;
  8. }DATE;
  9. typedef structnode{
  10. DATE date;
  11. struct node *pre;
  12. struct node *next;
  13. }NODE;
  14. typedef struct{
  15. int num;
  16. NODE *head;
  17. NODE *tail;
  18. }DOUBLE_LIST;
  19. voidInitDoubleList(DOUBLE_LIST *list);
  20. voidDoubleListPrint(DOUBLE_LIST list);
  21. void PrintNode(NODE *node);
  22. voidHeadInsterNode(DOUBLE_LIST *list, DATEdate);
  23. void XInsterNode(DOUBLE_LIST*list, DATE date, int x);
  24. voidTailInsterNode(DOUBLE_LIST *list, DATEdate);
  25. voidHeadDeleteNode(DOUBLE_LIST *list);
  26. void XDeleteNode(DOUBLE_LIST*list, KEY key);
  27. voidTailDeleteNode(DOUBLE_LIST *list);
  28. void FreeList(DOUBLE_LIST*list);
#include 
#include 

typedef int KEY;

typedef struct {
        KEY key;
        int date;
}DATE;

typedef struct node{
        DATE date;
        struct node * pre;
        struct node * next;
}NODE;

typedef struct{
        int num;
        NODE *head;
        NODE *tail;
}DOUBLE_LIST;


void InitDoubleList(DOUBLE_LIST *list);

void DoubleListPrint(DOUBLE_LIST list);
void PrintNode(NODE * node);

void HeadInsterNode(DOUBLE_LIST *list, DATE date);
void XInsterNode(DOUBLE_LIST *list, DATE date, int x);
void TailInsterNode(DOUBLE_LIST *list, DATE date);

void HeadDeleteNode(DOUBLE_LIST *list);
void XDeleteNode(DOUBLE_LIST *list, KEY key);
void TailDeleteNode(DOUBLE_LIST *list);

void FreeList(DOUBLE_LIST *list);


DoubleList.c

[cpp] viewplain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #include "DoubleList.h"
  2. voidInitDoubleList(DOUBLE_LIST *list)
  3. {
  4. list->head = NULL;
  5. list->tail = NULL;
  6. list->num = 0;
  7. return ;
  8. }
  9. voidDoubleListPrint(DOUBLE_LIST list)
  10. {
  11. int i =0;
  12. NODE * p = NULL;
  13. p = list.head;
  14. if(0 ==list.num){
  15. printf("Your List isEmpty!\n");
  16. return;
  17. }
  18. for(i = 0; i < list.num;i++){
  19. PrintNode(p);
  20. p = p->next;
  21. }
  22. return;
  23. }
  24. void PrintNode(NODE *node)
  25. {
  26. printf("key:%d\tdate:%d\n",node->date.key,node->date.date);
  27. return;
  28. }
  29. voidHeadInsterNode(DOUBLE_LIST * list, DATEdate)
  30. {
  31. NODE * newnode = (NODE *)malloc(sizeof(NODE));
  32. newnode->date.key = date.key;
  33. newnode->date.date =date.date;
  34. if(0 ==list->num){
  35. newnode->next = NULL;
  36. newnode->pre = NULL;
  37. list->head = newnode;
  38. list->tail = newnode;
  39. list->num++;
  40. return;
  41. }
  42. newnode->pre = NULL;
  43. newnode->next = list->head;
  44. list->head->pre = newnode;
  45. list->head = newnode;
  46. list->num++;
  47. return;
  48. }
  49. void XInsterNode(DOUBLE_LIST*list, DATE date, int x)
  50. {
  51. int i;
  52. NODE * newnode = NULL;
  53. NODE * prenode = NULL;
  54. NODE * nextnode = NULL;
  55. if(x < 0 ||x >=list->num){
  56. printf("X Inster: x is notright!\n");
  57. return;
  58. }
  59. if(0 ==x){
  60. HeadInsterNode(list, date);
  61. return;
  62. }
  63. if(list->num == x -1){
  64. TailInsterNode(list, date);
  65. return;
  66. }
  67. prenode = NULL;
  68. nextnode = list->head;
  69. for(i = 0; i < x;i++){
  70. prenode = nextnode;
  71. nextnode = nextnode->next;
  72. }
  73. newnode = (NODE *)malloc(sizeof(NODE));
  74. newnode->date.key = date.key;
  75. newnode->date.date =date.date;
  76. prenode->next = newnode;
  77. newnode->pre = prenode;
  78. newnode->next = nextnode;
  79. nextnode->pre = newnode;
  80. list->num++;
  81. return ;
  82. }
  83. voidTailInsterNode(DOUBLE_LIST *list, DATEdate)
  84. {
  85. NODE * newnode = (NODE *)malloc(sizeof(NODE));
  86. newnode->date.key = date.key;
  87. newnode->date.date = date.date;
  88. if(0 ==list->num){
  89. newnode->next = NULL;
  90. newnode->pre = NULL;
  91. list->head = newnode;
  92. list->tail = newnode;
  93. list->num++;
  94. return;
  95. }
  96. newnode->next = NULL;
  97. newnode->pre = list->tail;
  98. list->tail->next = newnode;
  99. list->tail = newnode;
  100. list->num++;
  101. return;
  102. }
  103. voidHeadDeleteNode(DOUBLE_LIST *list)
  104. {
  105. NODE * tmpnode = NULL;
  106. if(0 == list->num){
  107. printf("%s:NoNode InList!\n",__FUNCTION__);
  108. return;
  109. }
  110. if(1 ==list->num){
  111. free(list->head);
  112. list->head = NULL;
  113. list->tail = NULL;
  114. list->num = 0;
  115. return ;
  116. }
  117. tmpnode = list->head;
  118. list->head = list->head->next;
  119. free(tmpnode);
  120. tmpnode = NULL;
  121. list->num--;
  122. return ;
  123. }
  124. void XDeleteNode(DOUBLE_LIST*list, KEY key)
  125. {
  126. ;
  127. }
  128. voidTailDeleteNode(DOUBLE_LIST *list)
  129. {
  130. NODE * tmpnode = NULL;
  131. if(0 == list->num){
  132. printf("%s:NoNode InList!\n",__FUNCTION__);
  133. return;
  134. }
  135. if(1 ==list->num){
  136. free(list->head);
  137. list->head = NULL;
  138. list->tail = NULL;
  139. list->num = 0;
  140. return ;
  141. }
  142. tmpnode = list->tail;
  143. list->tail = list->tail->pre;
  144. list->tail->next = NULL;
  145. free(tmpnode);
  146. tmpnode = NULL;
  147. list->num--;
  148. return;
  149. }
  150. void FreeList(DOUBLE_LIST*list)
  151. {
  152. int i;
  153. NODE * tmpnode = NULL;
  154. int num;
  155. num = list->num;
  156. for(i = 0; i < num;i++){
  157. tmpnode = list->head;
  158. list->head =list->head->next;
  159. free(tmpnode);
  160. tmpnode = NULL;
  161. list->num--;
  162. }
  163. list->head = NULL;
  164. list->tail = NULL;
  165. return;
  166. }
#include "DoubleList.h"


void InitDoubleList(DOUBLE_LIST *list)
{
        list->head = NULL;
        list->tail = NULL;
        list->num = 0;

        return ;
}

void DoubleListPrint(DOUBLE_LIST list)
{
        int i = 0;
        NODE * p = NULL;

        p = list.head;
        
        if(0 == list.num){
                printf("Your List is Empty!\n");
                return;
        }
        
        for(i = 0; i < list.num; i++){
                PrintNode(p);
                p = p->next;
        }

        return;
}

void PrintNode(NODE * node)
{
        printf("key:%d\tdate:%d\n",node->date.key,node->date.date);
        
        return;
}

void HeadInsterNode(DOUBLE_LIST * list, DATE date)
{
        NODE * newnode = (NODE *)malloc(sizeof(NODE));

        newnode->date.key = date.key;
        newnode->date.date = date.date;

        if(0 == list->num){
                newnode->next = NULL;
                newnode->pre = NULL;
                list->head = newnode;
                list->tail = newnode;

                list->num++;

                return;
        }

        newnode->pre = NULL;
        newnode->next = list->head;
        list->head->pre = newnode;
        list->head = newnode;

        list->num++;

        return;
        
}

void XInsterNode(DOUBLE_LIST *list, DATE date, int x)
{
        int i;
        NODE * newnode = NULL;
        NODE * prenode = NULL;
        NODE * nextnode = NULL;

        if(x < 0 ||x >= list->num){
                printf("X Inster: x is not right!\n");
                return;
        }

        if(0 == x){
                HeadInsterNode(list, date);
                return;
        }

        if(list->num == x - 1){
                TailInsterNode(list, date);
                return;
        }
        prenode = NULL;
        nextnode = list->head;

        for(i = 0; i < x; i++){
                prenode = nextnode;
                nextnode = nextnode->next;
        }
        
        newnode = (NODE *)malloc(sizeof(NODE));

        newnode->date.key = date.key;
        newnode->date.date = date.date;
        
        prenode->next = newnode;
        newnode->pre = prenode;
        newnode->next = nextnode;
        nextnode->pre = newnode;
        
        list->num++;
        
        return ;
}

void TailInsterNode(DOUBLE_LIST *list, DATE date)
{
        NODE * newnode = (NODE *)malloc(sizeof(NODE));

        newnode->date.key = date.key;
        newnode->date.date = date.date;

        if(0 == list->num){
                newnode->next = NULL;
                newnode->pre = NULL;
                list->head = newnode;
                list->tail = newnode;

                list->num++;

                return;
        }

        newnode->next = NULL;
        newnode->pre = list->tail;
        list->tail->next = newnode;
        list->tail = newnode;

        list->num++;
        
        return;
}

void HeadDeleteNode(DOUBLE_LIST *list)
{
        NODE * tmpnode = NULL;
        
        if(0 == list->num ){
                printf("%s:No Node In List!\n",__FUNCTION__);
                return;
        }
        if(1 == list->num){
                free(list->head);
                list->head = NULL;
                list->tail = NULL;
                list->num = 0;
                return ;
        }
        
        tmpnode = list->head;
        list->head = list->head->next;
        free(tmpnode);
        tmpnode = NULL;
        list->num--;

        return ;
}

void XDeleteNode(DOUBLE_LIST *list, KEY key)
{
        ;
}

void TailDeleteNode(DOUBLE_LIST *list)
{
        NODE * tmpnode = NULL;
        
        if(0 == list->num ){
                printf("%s:No Node In List!\n",__FUNCTION__);
                return;
        }
        if(1 == list->num){
                free(list->head);
                list->head = NULL;
                list->tail = NULL;
                list->num = 0;
                return ;
        }
        
        tmpnode = list->tail;
        list->tail = list->tail->pre;
        list->tail->next = NULL;
        free(tmpnode);
        tmpnode = NULL;
        list->num--;
        
        return;
}

void FreeList(DOUBLE_LIST *list)
{
        int i;
        NODE * tmpnode = NULL;
        int num;

        num = list->num;
        
        for(i = 0; i < num; i++){
                tmpnode = list->head;
                list->head = list->head->next;
                free(tmpnode);
                tmpnode = NULL;
                list->num--;
        }

        list->head = NULL;
        list->tail = NULL;

        return;
}


main.c

[cpp] viewplain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #include "DoubleList.h"
  2. int main(int argc,char*argv[])
  3. {
  4. DOUBLE_LIST list;
  5. int i;
  6. DATE date[10];
  7. for(i = 0; i < 10;i++){
  8. date[i].key = i + 1;
  9. date[i].date = i + 11;
  10. }
  11. #if1
  12. printf("init listtest\n");
  13. InitDoubleList(&list);
  14. DoubleListPrint(list);
  15. #endif
  16. #if1
  17. //Head inster test
  18. printf("headinster test\n");
  19. HeadInsterNode(&list,date[0]);
  20. HeadInsterNode(&list, date[1]);
  21. HeadInsterNode(&list,date[2]);
  22. HeadInsterNode(&list, date[3]);
  23. HeadInsterNode(&list,date[4]);
  24. DoubleListPrint(list);
  25. #endif
  26. #if1
  27. //tail inster test
  28. printf("\ntailinster test\n");
  29. TailInsterNode(&list,date[0]);
  30. TailInsterNode(&list, date[1]);
  31. TailInsterNode(&list,date[2]);
  32. TailInsterNode(&list, date[3]);
  33. TailInsterNode(&list,date[4]);
  34. DoubleListPrint(list);
  35. #endif
  36. #if1
  37. //x inster test
  38. printf("\nxinster test\n");
  39. XInsterNode(&list, date[5],5);
  40. XInsterNode(&list, date[6], 6);
  41. XInsterNode(&list, date[7],7);
  42. XInsterNode(&list, date[8], 8);
  43. XInsterNode(&list, date[9],9);
  44. DoubleListPrint(list);
  45. #endif
  46. #if1
  47. //head delete test
  48. printf("\nheaddelete test\n");
  49. HeadDeleteNode(&list);
  50. HeadDeleteNode(&list);
  51. HeadDeleteNode(&list);
  52. HeadDeleteNode(&list);
  53. HeadDeleteNode(&list);
  54. DoubleListPrint(list);
  55. #endif
  56. #if 1
  57. //tail deletetest
  58. printf("\ntail deletetest\n");
  59. TailDeleteNode(&list);
  60. TailDeleteNode(&list);
  61. TailDeleteNode(&list);
  62. TailDeleteNode(&list);
  63. TailDeleteNode(&list);
  64. DoubleListPrint(list);
  65. #endif
  66. #if 1
  67. //free listtest
  68. printf("\nfree listtest\n");
  69. FreeList(&list);
  70. DoubleListPrint(list);
  71. #endif
  72. return 0;
  73. }

你可能感兴趣的:(嵌入式 双向链表的头尾中插入法以及释放)