当前位置:   article > 正文

单向链表-C语言实现_c语言实现单项链表

c语言实现单项链表

只实现了几个简单的操作。在Linux内核中链表的使用非常多,不过使用方法与本例完全不一样。后续有时间再详述。

ChainNode.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX_DATA_LEN 128
  5. #define CN_TRUE 0
  6. #define CN_FALSE 1
  7. #define CN_ERROR -1
  8. typedef struct ChainNode ChainNode;
  9. struct ChainNode {
  10. char data[MAX_DATA_LEN+1];
  11. ChainNode *next;
  12. };
  13. static int get_chain_len(ChainNode *head)
  14. {
  15. int len = 0;
  16. ChainNode *node;
  17. node = head;
  18. while(node) {
  19. len++;
  20. node = node->next;
  21. }
  22. return len;
  23. }
  24. /*
  25. * Create a new node, and append it to the end of chain.
  26. */
  27. static int chain_append(ChainNode *head, char *content, int content_len)
  28. {
  29. ChainNode *node;
  30. ChainNode *new_node;
  31. if(NULL == head || NULL == content || content_len < 1 || content_len > MAX_DATA_LEN)
  32. return CN_ERROR;
  33. new_node = (ChainNode *)malloc(sizeof(ChainNode));
  34. if(NULL == new_node)
  35. return CN_ERROR;
  36. st
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/945189
推荐阅读
相关标签
  

闽ICP备14008679号