当前位置:   article > 正文

数据结构 链式存储 +

数据结构 链式存储 +

int DeleteLinkList(LinkList *list, char *name);
int ReviseLinkList(LinkList *list, char *name, DATATYPE data);
int DestroyLinkList(LinkList *list);
int InsertTailLinkList(LinkList *list, DATATYPE data);

​​​​​​​删除

修改​​​​​​​

 

销毁

​​​​​​​

 插入

 完整代码展示

  1. #include <stdio.h>
  2. #include "doulink.h"
  3. #include <string.h>
  4. int findbyname(DATATYPE*data,void* arg)
  5. {
  6. return (0 == strcmp(data->name,(char*)arg));
  7. }
  8. int findbyage(DATATYPE*data,void* arg)
  9. {
  10. return data->age == *(int*)arg;
  11. }
  12. int main()
  13. {
  14. DATATYPE data[5]={
  15. {"zhangsan",'m',20,70},
  16. {"lisi",'f',21,60},
  17. {"wangmazi",'m',25,80},
  18. {"liubei",'f',30,85},
  19. {"caocao",'f',40,90},
  20. };
  21. DouLinkList* dl = CreateDouLinkList();
  22. InsertHeadLinkList(dl,&data[0]);
  23. InsertHeadLinkList(dl,&data[1]);
  24. InsertHeadLinkList(dl,&data[2]);
  25. ShowDouLinkList(dl,DIR_FORWARD);
  26. printf("-------------back---------------\n");
  27. ShowDouLinkList(dl,DIR_BACKWARD);
  28. printf("-------------find---------------\n");
  29. // char want_name[]="lisi";
  30. // //DouLinkNode* tmp = FindLinkList(dl,findbyname,want_name);
  31. // int want_age = 25;
  32. // DouLinkNode* tmp = FindLinkList(dl,findbyage,&want_age);
  33. // if(NULL == tmp)
  34. // {
  35. // printf("can't find person ,name:%s\n",want_name);
  36. // }
  37. // else
  38. // {
  39. // printf("%s:%d\n",tmp->data.name,tmp->data.score);
  40. // }
  41. // RevertDouLinkList(dl);
  42. // printf("-------------rev---------------\n");
  43. // ShowDouLinkList(dl,DIR_FORWARD);
  44. // DeleteLinkList(dl,findbyname,"lisi");
  45. // printf("-------------del forware---------------\n");
  46. // ShowDouLinkList(dl,DIR_FORWARD);
  47. // printf("-------------back---------------\n");
  48. // ShowDouLinkList(dl,DIR_BACKWARD);
  49. // ModifyDouLinkList(dl,findbyname,"zhangsan",&data[3]);
  50. // printf("-------------modify---------------\n");
  51. // ShowDouLinkList(dl,DIR_FORWARD);
  52. InserPosDouLinkList(dl,&data[3],3);
  53. printf("-------------pos---------------\n");
  54. ShowDouLinkList(dl,DIR_FORWARD);
  55. printf("-------------back---------------\n");
  56. ShowDouLinkList(dl,DIR_BACKWARD);
  57. DestroyDouLinkList(&dl);
  58. printf("Hello World!\n");
  59. return 0;
  60. }
  1. #include "doulink.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. DouLinkList *CreateDouLinkList()
  6. {
  7. //DouLinkList dl ;
  8. DouLinkList* dl = (DouLinkList*)malloc(sizeof(DouLinkList));
  9. if(NULL == dl)
  10. {
  11. perror("CreateDouLinkList malloc");
  12. //exit(1);
  13. return NULL;
  14. }
  15. dl->head =NULL;
  16. dl->clen = 0 ;
  17. return dl;
  18. }
  19. int InsertHeadLinkList(DouLinkList *list, DATATYPE *data)
  20. {
  21. DouLinkNode*newnode = malloc(sizeof(DouLinkNode));
  22. if(NULL == newnode)
  23. {
  24. perror("InsertHeadLinkList malloc");
  25. return 1;
  26. }
  27. memcpy(&newnode->data,data,sizeof(DATATYPE));
  28. newnode->next = NULL;
  29. newnode->prev= NULL;
  30. if(0==list->clen)//empty
  31. {
  32. list->head = newnode;
  33. }
  34. else
  35. {
  36. newnode->next = list->head;
  37. list->head->prev = newnode;
  38. list->head = newnode;
  39. }
  40. list->clen++;
  41. return 0;
  42. }
  43. int ShowDouLinkList(DouLinkList *list, DIRECT direct)
  44. {
  45. int i = 0 ;
  46. DouLinkNode* tmp = list->head;
  47. if(direct==DIR_FORWARD)
  48. {
  49. for(i=0;i<GetSizeDouLinkList(list);i++)
  50. {
  51. printf("%s %c %d %d\n",tmp->data.name,tmp->data.sex,tmp->data.age,tmp->data.score);
  52. tmp=tmp->next;
  53. }
  54. }
  55. else
  56. {
  57. while(tmp->next)
  58. {
  59. tmp=tmp->next;
  60. }
  61. for(i=0;i<GetSizeDouLinkList(list);i++)
  62. {
  63. printf("%s %c %d %d\n",tmp->data.name,tmp->data.sex,tmp->data.age,tmp->data.score);
  64. tmp=tmp->prev;
  65. }
  66. }
  67. return 0;
  68. }
  69. int GetSizeDouLinkList(DouLinkList *list)
  70. {
  71. return list->clen;
  72. }
  73. DouLinkNode *FindLinkList(DouLinkList *list, PFUN fun, void *arg)
  74. {
  75. DouLinkNode* tmp = list->head;
  76. int size = GetSizeDouLinkList(list);
  77. int i = 0;
  78. for(i = 0 ;i<size;i++)
  79. {
  80. //if(0==strcmp(tmp->data.name))
  81. if(fun(&tmp->data,arg))
  82. {
  83. return tmp;
  84. }
  85. tmp= tmp->next;
  86. }
  87. return NULL;
  88. }
  89. int RevertDouLinkList(DouLinkList *list)
  90. {
  91. int size = GetSizeDouLinkList(list);
  92. if(size<2)
  93. {
  94. return 0;
  95. }
  96. DouLinkNode* prev= NULL;
  97. DouLinkNode* tmp = list->head;
  98. DouLinkNode*next= tmp->next;
  99. while(1)
  100. {
  101. tmp->next = prev;
  102. tmp->prev = next;
  103. prev= tmp;
  104. tmp = next;
  105. if(NULL == tmp)
  106. {
  107. break;
  108. }
  109. next =next->next;
  110. }
  111. list->head = prev;
  112. return 0;
  113. }
  114. int DeleteLinkList(DouLinkList *list, PFUN fun, void *arg)
  115. {
  116. if(NULL == list)
  117. {
  118. fprintf(stderr,"DouLinkList is null");
  119. return 1;
  120. }
  121. if(IsEmptyDouLinkList(list))
  122. {
  123. fprintf(stderr,"DouLinkList is empty");
  124. return 1;
  125. }
  126. DouLinkNode* ret = FindLinkList(list,fun,arg);
  127. if(NULL==ret)
  128. {
  129. fprintf(stderr,"DeleteLinkList error,cant find\n");
  130. return 1;
  131. }
  132. if(ret == list->head)
  133. {
  134. list->head = ret->next;
  135. list->head->prev = NULL;
  136. }
  137. else
  138. {
  139. if(ret->next)
  140. ret->next->prev = ret->prev;
  141. ret->prev->next = ret->next;
  142. }
  143. free(ret);
  144. list->clen--;
  145. return 0;
  146. }
  147. int IsEmptyDouLinkList(DouLinkList *list)
  148. {
  149. return 0 == list->clen;
  150. }
  151. int ModifyDouLinkList(DouLinkList *list, PFUN fun, void *arg, DATATYPE *data)
  152. {
  153. DouLinkNode* ret = FindLinkList(list,fun,arg);
  154. if(NULL == ret)
  155. {
  156. fprintf(stderr,"ModifyDouLinkList error,cant find\n");
  157. return 1;
  158. }
  159. memcpy(&ret->data,data,sizeof(DATATYPE));
  160. return 0;
  161. }
  162. int DestroyDouLinkList(DouLinkList **list)
  163. {
  164. DouLinkNode* tmp=(*list)->head;
  165. while(tmp)
  166. {
  167. (*list)->head=(*list)->head->next;
  168. free(tmp);
  169. tmp = (*list)->head;
  170. }
  171. free(*list);
  172. (*list)= NULL;
  173. return 0;
  174. }
  175. int InserPosDouLinkList(DouLinkList *list, DATATYPE *data,int pos)
  176. {
  177. if(pos<0 ||pos>GetSizeDouLinkList(list))
  178. {
  179. fprintf(stderr,"InserPosDouLinkList error,index error\n");
  180. return 1;
  181. }
  182. if(IsEmptyDouLinkList(list) || 0 == pos)
  183. {
  184. return InsertHeadLinkList(list,data);
  185. }
  186. else
  187. {
  188. DouLinkNode* tmp = list->head;
  189. tmp= list->head;
  190. DouLinkNode* newnode = (DouLinkNode*)malloc(sizeof(DouLinkNode));
  191. if(NULL == newnode)
  192. {
  193. perror("InserPosDouLinkList malloc");
  194. return 1;
  195. }
  196. memcpy(&newnode->data,data,sizeof(DATATYPE));
  197. newnode->prev = NULL;
  198. newnode->next = NULL;
  199. int i = pos-1;
  200. while(i--)
  201. {
  202. tmp=tmp->next;
  203. }
  204. newnode ->prev = tmp;
  205. newnode->next = tmp->next;//这时候都是NULL,如果是尾插入不走if
  206. if(tmp->next)
  207. {
  208. tmp->next->prev = newnode;//中间插入
  209. }
  210. tmp->next = newnode;
  211. }
  212. list->clen++;
  213. return 0;
  214. }
  1. #ifndef DOULINK_H
  2. #define DOULINK_H
  3. typedef struct{
  4. char name[32];
  5. char sex;
  6. int age;
  7. int score;
  8. }DATATYPE;
  9. typedef int (*PFUN)(DATATYPE*data,void* arg);//表示fun()中的参数书形式
  10. typedef struct node {
  11. DATATYPE data;
  12. struct node *next,*prev;
  13. }DouLinkNode;
  14. typedef struct{
  15. DouLinkNode *head;
  16. int clen;
  17. }DouLinkList;
  18. typedef enum{DIR_FORWARD,DIR_BACKWARD}DIRECT;
  19. DouLinkList* CreateDouLinkList();
  20. int InsertHeadLinkList(DouLinkList *list, DATATYPE *data);
  21. int ShowDouLinkList(DouLinkList *list,DIRECT direct);
  22. int GetSizeDouLinkList(DouLinkList *list);
  23. DouLinkNode *FindLinkList(DouLinkList *list, PFUN fun,void* arg);
  24. int RevertDouLinkList(DouLinkList *list);
  25. int DeleteLinkList(DouLinkList *list, PFUN fun,void* arg);
  26. int IsEmptyDouLinkList(DouLinkList *list);
  27. int ModifyDouLinkList(DouLinkList *list,PFUN fun,void* arg,DATATYPE *data);
  28. int DestroyDouLinkList(DouLinkList **list);
  29. int InserPosDouLinkList(DouLinkList *list,DATATYPE *data,int pos);
  30. #endif // DOULINK_H

序表和链表 优缺点


    存储方式:
        顺序表是一段连续的存储单元
        链表是逻辑结构连续物理结构(在内存中的表现形式)不连续
    时间性能,
        查找 顺序表O(1)
             链表  O(n)
        插入和删除
            顺序表 O(n)
            链表   O(1)
            
    空间性能
            顺序表 需要预先分配空间,大小固定
            链表, 不需要预先分配,大小可变,动态分配
            
            
    循环链表
        简单的来说,就是将原来单链表中最有一个元素的next指针指向第一个元素或头结点,链表就成了一个环,头尾相连,就成了循环链表。circultlar linker list.
        
        注意非空表,和空表。多数会加入头结点。
        原来结束的条件是
        p->next != NULL ------->>>>> p-next != Head 
        
    双向链表
    double link list。
    
    typedef struct DulNode
    {
    
        ElemType date;
        struct DulNode *pri;
        sturct DulNode *next;
    }DulNode,*DuLinkList;
    

 习题

1)双向链表逆序

2)实现mplay的播放列表

main.c

  1. #include <stdio.h>
  2. #include "doulink.h"
  3. #include <string.h>
  4. #include "func.h"
  5. #include <stdlib.h>
  6. void show_menu( DouLinkList* dl)
  7. {
  8. printf("1.show_list\n");
  9. printf("2.prev\n");
  10. printf("3.next\n");
  11. printf("4.end\n");
  12. char buf[10]={0};
  13. fgets(buf,sizeof(buf),stdin);
  14. int num = atoi(buf);
  15. switch (num) {
  16. case 1:
  17. ShowDouLinkList(dl,DIR_FORWARD);
  18. break;
  19. case 2:
  20. GetPrev(dl);
  21. break;
  22. case 3:
  23. Getnext(dl);
  24. break;
  25. case 4:
  26. exit(1);
  27. break;
  28. default:
  29. break;
  30. }
  31. }
  32. int main()
  33. {
  34. DouLinkList* dl = CreateDouLinkList();
  35. do_ls("/home/linux",dl);
  36. ShowDouLinkList(dl,DIR_FORWARD);
  37. char *pathname=NULL;
  38. while(1)
  39. {
  40. show_menu(dl);
  41. pathname = GetCurrent(dl);
  42. printf("currnt play file:%s\n",pathname);
  43. }
  44. //atexit();
  45. printf("Hello World!\n");
  46. return 0;
  47. }

doulink.c

  1. #include "doulink.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. DouLinkList *CreateDouLinkList()
  6. {
  7. //DouLinkList dl ;
  8. DouLinkList* dl = (DouLinkList*)malloc(sizeof(DouLinkList));
  9. if(NULL == dl)
  10. {
  11. perror("CreateDouLinkList malloc");
  12. //exit(1);
  13. return NULL;
  14. }
  15. dl->head =NULL;
  16. dl->clen = 0 ;
  17. dl->currnet =NULL;
  18. return dl;
  19. }
  20. int InsertHeadLinkList(DouLinkList *list, DATATYPE *data)
  21. {
  22. DouLinkNode*newnode = malloc(sizeof(DouLinkNode));
  23. if(NULL == newnode)
  24. {
  25. perror("InsertHeadLinkList malloc");
  26. return 1;
  27. }
  28. memcpy(&newnode->data,data,sizeof(DATATYPE));
  29. newnode->next = NULL;
  30. newnode->prev= NULL;
  31. if(0==list->clen)//empty
  32. {
  33. list->head = newnode;
  34. }
  35. else
  36. {
  37. newnode->next = list->head;
  38. list->head->prev = newnode;
  39. list->head = newnode;
  40. }
  41. list->clen++;
  42. return 0;
  43. }
  44. int ShowDouLinkList(DouLinkList *list, DIRECT direct)
  45. {
  46. int i = 0 ;
  47. DouLinkNode* tmp = list->head;
  48. if(direct==DIR_FORWARD)
  49. {
  50. for(i=0;i<GetSizeDouLinkList(list);i++)
  51. {
  52. printf("%d %s\n",i,tmp->data.pathname);
  53. tmp=tmp->next;
  54. }
  55. }
  56. return 0;
  57. }
  58. int GetSizeDouLinkList(DouLinkList *list)
  59. {
  60. return list->clen;
  61. }
  62. DouLinkNode *FindLinkList(DouLinkList *list, PFUN fun, void *arg)
  63. {
  64. DouLinkNode* tmp = list->head;
  65. int size = GetSizeDouLinkList(list);
  66. int i = 0;
  67. for(i = 0 ;i<size;i++)
  68. {
  69. //if(0==strcmp(tmp->data.name))
  70. if(fun(&tmp->data,arg))
  71. {
  72. return tmp;
  73. }
  74. tmp= tmp->next;
  75. }
  76. return NULL;
  77. }
  78. int RevertDouLinkList(DouLinkList *list)
  79. {
  80. int size = GetSizeDouLinkList(list);
  81. if(size<2)
  82. {
  83. return 0;
  84. }
  85. DouLinkNode* prev= NULL;
  86. DouLinkNode* tmp = list->head;
  87. DouLinkNode*next= tmp->next;
  88. while(1)
  89. {
  90. tmp->next = prev;
  91. tmp->prev = next;
  92. prev= tmp;
  93. tmp = next;
  94. if(NULL == tmp)
  95. {
  96. break;
  97. }
  98. next =next->next;
  99. }
  100. list->head = prev;
  101. return 0;
  102. }
  103. char *GetCurrent(DouLinkList *list)
  104. {
  105. return list->currnet->data.pathname;
  106. }
  107. int GetPrev(DouLinkList *list)
  108. {
  109. list->currnet = list->currnet->prev;
  110. if(NULL == list->currnet)
  111. {
  112. list->currnet = list->head;
  113. while(list->currnet->next)
  114. {
  115. list->currnet = list->currnet->next;
  116. }
  117. }
  118. return 0;
  119. }
  120. int Getnext(DouLinkList *list)
  121. {
  122. list->currnet = list->currnet->next;
  123. if(NULL == list->currnet)
  124. {
  125. list->currnet = list->head;
  126. }
  127. return 0;
  128. }

fun.c

  1. #include "func.h"
  2. #include <dirent.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. int do_ls(char *path,DouLinkList*dl)
  7. {
  8. {
  9. DIR* dir = opendir(path);
  10. if(NULL == dir)
  11. {
  12. printf("opendir");
  13. return 1;
  14. }
  15. DATATYPE data;
  16. while(1)
  17. {
  18. struct dirent *info = readdir(dir);
  19. //procintf("%s %lu",info->d_name,info->d_ino);
  20. if(NULL == info)
  21. {
  22. break;
  23. }
  24. if(strlen(info->d_name ) >3)// 1.flv /home/linux/1.flv
  25. {
  26. if(0==strcmp(&info->d_name[strlen(info->d_name)-3],"mp3")
  27. ||0==strcmp(&info->d_name[strlen(info->d_name)-3],"flv")
  28. ||0==strcmp(&info->d_name[strlen(info->d_name)-3],"mp4"))
  29. {
  30. bzero(&data,sizeof(data));
  31. sprintf(data.pathname,"%s/%s",path,info->d_name);
  32. //sprintf(song.songlist[song.total++],"%s/%s",path,info->d_name);
  33. InsertHeadLinkList(dl,&data);
  34. }
  35. }
  36. else
  37. {
  38. continue;
  39. }
  40. }
  41. closedir(dir);
  42. }
  43. dl->currnet = dl->head;
  44. return 0;
  45. }

fun.h

  1. #ifndef FUNC_H
  2. #define FUNC_H
  3. #include "doulink.h"
  4. int do_ls(char *path,DouLinkList*dl);
  5. #endif // FUNC_H

doulink.h

  1. #ifndef DOULINK_H
  2. #define DOULINK_H
  3. typedef struct{
  4. char pathname[512];
  5. }DATATYPE;
  6. typedef int (*PFUN)(DATATYPE*data,void* arg);
  7. typedef struct node {
  8. DATATYPE data;
  9. struct node *next,*prev;
  10. }DouLinkNode;
  11. typedef struct{
  12. DouLinkNode *head;
  13. struct node *currnet;
  14. int clen;
  15. }DouLinkList;
  16. typedef enum{DIR_FORWARD,DIR_BACKWARD}DIRECT;
  17. DouLinkList* CreateDouLinkList();
  18. int InsertHeadLinkList(DouLinkList *list, DATATYPE *data);
  19. int ShowDouLinkList(DouLinkList *list,DIRECT direct);
  20. int GetSizeDouLinkList(DouLinkList *list);
  21. DouLinkNode *FindLinkList(DouLinkList *list, PFUN fun,void* arg);
  22. int RevertDouLinkList(DouLinkList *list);
  23. char* GetCurrent(DouLinkList *list);
  24. int GetPrev(DouLinkList *list);
  25. int Getnext(DouLinkList *list);
  26. #endif // DOULINK_H

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/913092
推荐阅读
相关标签
  

闽ICP备14008679号