• 双向循环链表


    dllist.h

     1 #ifndef _DLLIST_H
     2 #define _DLLIST_H
     3 
     4 #ifdef __cplusplus
     5 extern "C" {
     6 #endif 
     7 
     8 struct DLLNode_T{
     9     void *pData;                 //数据域指针
    10     struct DLLNode_T *pPrev;     //前驱指针
    11     struct DLLNode_T *pNext;     //后驱指针
    12 };
    13 
    14 #define DLLNODE struct DLLNode_T
    15 typedef DLLNODE *pNode;
    16 #define DLLNODE_SIZE sizeof(struct DLLNode_T)
    17 
    18 
    19 pNode DLLNODE_Init(void *pData);
    20 
    21 void DLLNODE_InsertToTail(pNode head, void *pData);
    22 
    23 int DLLNODE_GetCount(pNode head);
    24 
    25 pNode DLLNODE_Search(pNode head, char *acCmp, int (*fCmp)(void *pData, char *acCmp));
    26 
    27 int DLLNODE_GetIndex(pNode head, pNode pOperate);
    28 
    29 void DLLNODE_Print(pNode head, void (*fPrint)(void *pData));
    30 
    31 void DLLNODE_FreeFromHead(pNode head);
    32 
    33 void DLLNODE_TDelete(pNode pTDel);
    34 
    35 bool DLLNODE_FDelete(pNode pFDel, void (*fDel)(void *pData));
    36 
    37 #ifdef __cplusplus
    38 }
    39 #endif
    40 
    41 #endif

    dllist.cpp

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include "./dllist.h"
      5 
      6 pNode DLLNODE_Init(void *pData)
      7 {
      8     pNode DDLNode=NULL;
      9 
     10     DDLNode=(pNode)malloc(DLLNODE_SIZE);
     11     if(DDLNode==NULL)
     12     {
     13         printf("Error!");
     14     }
     15     else
     16     {
     17         DDLNode->pData=pData;
     18         // 初始化结点的前驱结点与后驱结点就是自身
     19         DDLNode->pPrev=DDLNode->pNext=DDLNode; 
     20     }
     21     
     22     return DDLNode;
     23 }
     24 
     25 
     26 void DLLNODE_InsertToTail(pNode head, void *pData)
     27 {
     28     pNode pNew=NULL;
     29 
     30     pNew=DLLNODE_Init(pData);
     31 
     32     pNew->pNext=head;
     33     head->pPrev->pNext=pNew;
     34     pNew->pPrev=head->pPrev;
     35     head->pPrev=pNew;
     36 }
     37 
     38 int DLLNODE_GetCount(pNode head)
     39 {
     40     pNode pTmp=NULL;
     41     int count=0;
     42 
     43     for(pTmp=head->pNext; pTmp!=head; pTmp=pTmp->pNext)
     44     {
     45         count++;
     46     }
     47 
     48     return count;
     49 }
     50 
     51 pNode DLLNODE_Search(pNode head, char *acCmp, int (*fCmp)(void *pData, char *acCmp))
     52 {
     53     pNode pTmp=NULL;
     54 
     55     for(pTmp=head->pNext; pTmp!=head; pTmp=pTmp->pNext)
     56     {
     57         if(fCmp(pTmp->pData, acCmp))
     58         {
     59             return pTmp;
     60         }
     61     }
     62     
     63     return NULL;
     64 }
     65 
     66 int DLLNODE_GetIndex(pNode head, pNode pOperate)
     67 {
     68     pNode pTmp=NULL;
     69     int iIndex=0;
     70     
     71     for(pTmp=head->pNext; pTmp!=head; pTmp=pTmp->pNext)
     72     {
     73         iIndex++;
     74         if(pTmp==pOperate)
     75         {
     76             break;
     77         }
     78     }
     79 
     80     return iIndex;
     81 }
     82 
     83 void DLLNODE_Print(pNode head, void (*fPrint)(void *pData))
     84 {
     85     pNode pTmp=NULL;
     86     int count=0;
     87     int iOption=0;
     88 
     89     if(head->pNext==head)
     90     {
     91         printf("---链表无数据---
    ");
     92     }
     93     else
     94     {
     95         for(pTmp=head->pNext; pTmp!=head; pTmp=pTmp->pNext)
     96         {
     97             fPrint(pTmp->pData);
     98         }
     99     }    
    100 }
    101 
    102 void DLLNODE_FreeFromHead(pNode head)
    103 {
    104     pNode pTmp=NULL;
    105 
    106     for(pTmp=head->pNext; pTmp!=head; pTmp=head->pNext)
    107     {
    108         pTmp->pNext->pPrev=head;
    109         head->pNext=pTmp->pNext;
    110         free(pTmp->pData);
    111         free(pTmp);
    112     }
    113     free(head);
    114 }
    115 
    116 void DLLNODE_TDelete(pNode pTDel)
    117 {
    118     pTDel->pPrev->pNext=pTDel->pNext;
    119     pTDel->pNext->pPrev=pTDel->pPrev;
    120 
    121     free(pTDel->pData);
    122     free(pTDel);
    123 }
    124 
    125 bool DLLNODE_FDelete(pNode pFDel, void (*fDel)(void *pData))
    126 {
    127     if(pFDel==NULL)
    128     {
    129         return false;
    130     }
    131     else
    132     {
    133         fDel(pFDel->pData);
    134         return true;
    135     }
    136 }
  • 相关阅读:
    oracle mybatis 模糊查询
    IntelliJ IDEA各种引入jar包的方式及其关系
    eclipse中新建Java工程的三个JRE选项区别
    @Autowired注解和@resource注解的区别
    结合测试,黑盒测试
    Oracle左连接、右连接、全外连接以及(+)号用法
    samePropertyValuesAs()
    Collection.sort 的用法
    junit withcapture
    单元测试
  • 原文地址:https://www.cnblogs.com/paullam/p/3732683.html
Copyright © 2020-2023  润新知