前言
 创建一个双向链表实现对双向链表内元素的
 ——增加(头插 指定位置插入 尾插)
 ——删除(头删 指定位置删除 尾删)
 ——遍历双向链表
  
More info: DoubleLinkedList
构造双向链表(DoubleLinkedList)
构造双向链表的结点 双向链表中所有结点的数据类型应该是相同的
1 2 3 4 5 6 7
   |  typedef int dataType_t; typedef struct DoubleLinkedList {     dataType_t data;                      struct DoubleLinkedList *prev;           struct DoubleLinkedList *next;       } DoubleLList_t;
 
  | 
 
创建一个空链表,空链表应该有一个头结点(DoubleLList_Create)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   | DoubleLList_t* DoubleLList_Create(void) {          DoubleLList_t *Head = (DoubleLList_t*)calloc(1,sizeof (DoubleLList_t));     if(Head == NULL)     {         perror("calloc memory for Head is Failed!\n");         exit(-1);            }
           Head->prev = NULL;     Head->next = NULL;
           return Head; }
   | 
 
创建新的结点,并对新结点进行初始化(数据域 指针域)(DoubleLList_NewNode)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
   | DoubleLList_t *DoubleLList_NewNode(dataType_t data) {          DoubleLList_t *NewNode = (DoubleLList_t *) calloc(1, sizeof(DoubleLList_t));     if(NewNode == NULL)     {         perror("calloc memory for NewNode is Failed!\n");         return NULL;     }
           NewNode->prev = NULL;     NewNode->data = data;     NewNode->next = NULL;
           return NewNode; }
   | 
 
头插(DoubleLList_HeadInsert)
  
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
   | bool DoubleLList_HeadInsert(DoubleLList_t *Head,dataType_t data) {          DoubleLList_t *NewNode = DoubleLList_NewNode(data);     if(NewNode == NULL)     {         printf("Can not insert new node!\n");         return false;     }
           if(Head->next == NULL)     {         Head->next= NewNode;         NewNode->prev = Head;
          return true;     }
           NewNode->next = Head->next;     Head->next->prev = NewNode;     Head->next = NewNode;     NewNode->prev = Head;
      return true; }
   | 
 
尾插(DoubleLList_TailInsert)
  
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
   | bool DoubleLList_TailInsert(DoubleLList_t *Head,dataType_t data) {          DoubleLList_t *NewNode = DoubleLList_NewNode(data);     if(NewNode == NULL)     {         printf("Can not insert new node!\n");         return false;     }
           if(Head->next == NULL)     {         Head->next= NewNode;         NewNode->prev = Head;         return true;     }
                DoubleLList_t *Phead = Head;     while (Phead->next)     {                  Phead = Phead->next;     }     Phead->next=NewNode;     NewNode->prev = Phead;     return true; }
   | 
 
指定插入(DoubleLList_DestInsert)
  
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
   | bool DoubleLList_DestInsert(DoubleLList_t *Head,dataType_t destval,dataType_t data) {          DoubleLList_t *NewNode = DoubleLList_NewNode(data);     if(NewNode == NULL)     {         printf("Can not insert new node!\n");         return false;     }
           if(Head->next == NULL)     {         Head->next= NewNode;         NewNode->prev = Head;         return true;     }
                DoubleLList_t *Phead = Head->next;     while (Phead != NULL && destval != Phead->data)     {                  Phead = Phead->next;         if(Phead == NULL)         {             return false;         }     }
           NewNode->next=Phead->next;     Phead->next->prev = NewNode;
      NewNode->prev = Phead;     Phead->next=NewNode;
      return true; }
   | 
 
遍历链表(DoubleLList_Print)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
   | bool DoubleLList_Print(DoubleLList_t *Head) {          DoubleLList_t *Phead = Head;
           if(Head->next == NULL)     {         printf("Current linkedList is empty!\n");         return false;     }
                int i=0;     while (Phead->next != NULL)     {         i++;
                   Phead = Phead->next;
                   printf("Date[%d] = %d\n",i,Phead->data);     }     return true; }
   | 
 
头删 删除首结点(DoubleLList_HeadDel)
  
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   | bool DoubleLList_HeadDel(DoubleLList_t *Head) {          if(Head->next == NULL)     {         return false;     }
           DoubleLList_t *Phead = Head->next;
           Head->next = Head->next->next;     Head->next->prev = Head;
           Phead->next = NULL;     Phead->prev = NULL;     free(Phead);
      return true; }
   | 
 
尾删 删除尾结点(DoubleLList_TailDel)
  
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
   | bool DoubleLList_TailDel(DoubleLList_t *Head) {          if(Head->next == NULL)     {     return false;     }
           DoubleLList_t *Phead = Head->next;
           DoubleLList_t *Phead_Prev = Head;
           while (Phead->next != NULL)     {                  Phead_Prev = Phead;         Phead = Phead->next;
      }     Phead_Prev->next = NULL;
      free(Phead);
      return true; }
   | 
 
main主程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
   | int main() {
      DoubleLList_t *Head = DoubleLList_Create();            
      DoubleLList_HeadInsert(Head,5);                 DoubleLList_HeadInsert(Head,8);     DoubleLList_HeadInsert(Head,1);     DoubleLList_HeadInsert(Head,6);     DoubleLList_HeadInsert(Head,3);
      DoubleLList_Print(Head);                             printf("\n");
      DoubleLList_TailInsert(Head,4);               
      DoubleLList_Print(Head);                             printf("\n");
      DoubleLList_DestInsert(Head,1,9);     
      DoubleLList_Print(Head);                              printf("\n");
      DoubleLList_HeadDel(Head);                        
      DoubleLList_Print(Head);                              printf("\n");
      DoubleLList_TailDel(Head);                        
      DoubleLList_Print(Head);                              printf("\n");     return 0; }
   | 
 
结果验证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
   | Date[1] = 3 Date[2] = 6 Date[3] = 1 Date[4] = 8 Date[5] = 5
  Date[1] = 3 Date[2] = 6 Date[3] = 1 Date[4] = 8 Date[5] = 5 Date[6] = 4
  Date[1] = 3 Date[2] = 6 Date[3] = 1 Date[4] = 9 Date[5] = 8 Date[6] = 5 Date[7] = 4
  Date[1] = 6 Date[2] = 1 Date[3] = 9 Date[4] = 8 Date[5] = 5 Date[6] = 4
  Date[1] = 6 Date[2] = 1 Date[3] = 9 Date[4] = 8 Date[5] = 5
 
  进程已结束,退出代码0
   | 
 
汇总
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
   | #include <stdio.h> #include <stdbool.h> #include <stdlib.h>
 
  typedef int dataType_t;
 
 
  typedef struct DoubleLinkedList {     dataType_t data;                      struct DoubleLinkedList *prev;           struct DoubleLinkedList *next;       } DoubleLList_t;
 
 
  DoubleLList_t* DoubleLList_Create(void) {          DoubleLList_t *Head = (DoubleLList_t*)calloc(1,sizeof (DoubleLList_t));     if(Head == NULL)     {         perror("calloc memory for Head is Failed!\n");         exit(-1);            }
           Head->prev = NULL;     Head->next = NULL;
           return Head; }
 
 
  DoubleLList_t *DoubleLList_NewNode(dataType_t data) {          DoubleLList_t *NewNode = (DoubleLList_t *) calloc(1, sizeof(DoubleLList_t));     if(NewNode == NULL)     {         perror("calloc memory for NewNode is Failed!\n");         return NULL;     }
           NewNode->prev = NULL;     NewNode->data = data;     NewNode->next = NULL;
           return NewNode; }
 
 
  bool DoubleLList_HeadInsert(DoubleLList_t *Head,dataType_t data) {          DoubleLList_t *NewNode = DoubleLList_NewNode(data);     if(NewNode == NULL)     {         printf("Can not insert new node!\n");         return false;     }
           if(Head->next == NULL)     {         Head->next= NewNode;         NewNode->next = NULL;          return true;     }
           NewNode->next = Head->next;     Head->next->prev = NewNode;     Head->next = NewNode;
      return true; }
 
 
  bool DoubleLList_TailInsert(DoubleLList_t *Head,dataType_t data) {          DoubleLList_t *NewNode = DoubleLList_NewNode(data);     if(NewNode == NULL)     {         printf("Can not insert new node!\n");         return false;     }
           if(Head->next == NULL)     {         Head->next= NewNode;         return true;     }
                DoubleLList_t *Phead = Head;     while (Phead->next)     {                  Phead = Phead->next;     }     Phead->next=NewNode;     NewNode->prev = Phead;     return true; }
 
 
  bool DoubleLList_DestInsert(DoubleLList_t *Head,dataType_t destval,dataType_t data) {          DoubleLList_t *NewNode = DoubleLList_NewNode(data);     if(NewNode == NULL)     {         printf("Can not insert new node!\n");         return false;     }
           if(Head->next == NULL)     {         Head->next= NewNode;         return true;     }
                DoubleLList_t *Phead = Head->next;     while (Phead != NULL && destval != Phead->data)     {                  Phead = Phead->next;         if(Phead == NULL)         {             return false;         }     }
           NewNode->next=Phead->next;     Phead->next->prev = NewNode;
      NewNode->prev = Phead;     Phead->next=NewNode;
      return true; }
 
 
  bool DoubleLList_Print(DoubleLList_t *Head) {          DoubleLList_t *Phead = Head;
           if(Head->next == NULL)     {         printf("Current linkedList is empty!\n");         return false;     }
                int i=0;     while (Phead->next != NULL)     {         i++;
                   Phead = Phead->next;
                   printf("Date[%d] = %d\n",i,Phead->data);     }     return true; }
 
 
  bool DoubleLList_HeadDel(DoubleLList_t *Head) {          if(Head->next == NULL)     {         return false;     }
           DoubleLList_t *Phead = Head->next;
           Head->next = Head->next->next;     Head->next->prev = Head;
           Phead->next = NULL;     Phead->prev = NULL;     free(Phead);
      return true; }
 
 
  bool DoubleLList_TailDel(DoubleLList_t *Head) {          if(Head->next == NULL)     {     return false;     }
           DoubleLList_t *Phead = Head->next;
           DoubleLList_t *Phead_Prev = Head;
           while (Phead->next != NULL)     {                  Phead_Prev = Phead;         Phead = Phead->next;
      }     Phead_Prev->next = NULL;
      free(Phead);
      return true; }
 
  int main() {
      DoubleLList_t *Head = DoubleLList_Create();            
      DoubleLList_HeadInsert(Head,5);                 DoubleLList_HeadInsert(Head,8);     DoubleLList_HeadInsert(Head,1);     DoubleLList_HeadInsert(Head,6);     DoubleLList_HeadInsert(Head,3);
      DoubleLList_Print(Head);                             printf("\n");
      DoubleLList_TailInsert(Head,4);               
      DoubleLList_Print(Head);                             printf("\n");
      DoubleLList_DestInsert(Head,1,9);     
      DoubleLList_Print(Head);                              printf("\n");
      DoubleLList_HeadDel(Head);                        
      DoubleLList_Print(Head);                              printf("\n");
      DoubleLList_TailDel(Head);                        
      DoubleLList_Print(Head);                              printf("\n");     return 0; }
   |