Chinaunix首页 | 论坛 | 博客
  • 博客访问: 384569
  • 博文数量: 75
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 645
  • 用 户 组: 普通用户
  • 注册时间: 2015-06-03 18:24
文章分类

全部博文(75)

文章存档

2019年(1)

2018年(20)

2017年(14)

2016年(10)

2015年(30)

分类: LINUX

2018-04-08 21:44:07

链表的基本操作和实现

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. /**/
  4. typedef struct _list_node_{
  5.     int val;
  6.     struct _list_node_ *next_node;
  7. }list_node_t;


  8. list_node_t *Creat_head_node()
  9. {
  10.     list_node_t *head_node = (list_node_t *)malloc(sizeof(list_node_t));
  11.     if(head_node == NULL)
  12.     {
  13.         printf("malloc Failed\n");
  14.         return NULL;
  15.     }

  16.     /**/
  17.     head_node->next_node = NULL;
  18.     return head_node;
  19. }

  20. /**/
  21. list_node_t *Creat_node(int val)
  22. {
  23.     list_node_t *new_node = (list_node_t *)malloc(sizeof(list_node_t));
  24.     if(new_node == NULL)
  25.     {
  26.         printf("malloc Failed\n");
  27.         return NULL;
  28.     }

  29.     new_node->val = val;
  30.     new_node->next_node = NULL;
  31.     return new_node;
  32. }

  33. /*insert list in head*/
  34. int Insert_list_head(list_node_t *pHead, int val)
  35. {
  36.     list_node_t *pNew_node = NULL;

  37.     /**/
  38.     pNew_node = Creat_node(val);
  39.     if(pNew_node == NULL)
  40.     {
  41.         printf("Creat node Failed\n");
  42.         return -1;
  43.     }
  44.     pNew_node->next_node = pHead->next_node;
  45.     pHead->next_node = pNew_node;

  46.     return 0;
  47. }

  48. /**/
  49. int Delete_node(list_node_t *pHead, int val)
  50. {
  51.     list_node_t *p = NULL;
  52.     list_node_t *q = NULL;

  53.     q = pHead;
  54.     p = pHead->next_node;
  55.     while(p != NULL)
  56.     {
  57.         if(p->val == val)
  58.         {
  59.             printf("delete a node\n");
  60.             q->next_node = p->next_node;
  61.             free(p);
  62.             p = q->next_node;
  63.             continue;
  64.         }

  65.         q = p;
  66.         p = p->next_node;
  67.     }

  68.     return 0;
  69. }

  70. /*1:is empty 0:is not empty*/
  71. int Is_empty_list(list_node_t *pHead)
  72. {
  73.     if(pHead->next_node == NULL)
  74.     {
  75.         return 1;
  76.     }
  77.     else
  78.     {
  79.         return 0;
  80.     }
  81. }

  82. /**/
  83. int Get_length_of_list(list_node_t *pHead)
  84. {
  85.     list_node_t *pCur_node = NULL;
  86.     int length = 0;

  87.     /**/
  88.     pCur_node = pHead->next_node;
  89.     while(pCur_node != NULL)
  90.     {
  91.         length ++;
  92.         pCur_node = pCur_node->next_node;
  93.     }

  94.     return length;
  95. }

  96. /**/
  97. void Print_list(list_node_t *pHead)
  98. {
  99.     list_node_t *pCur_node = NULL;

  100.     /**/
  101.     pCur_node = pHead->next_node;
  102.     while(pCur_node != NULL)
  103.     {
  104.         printf("%d ", pCur_node->val);
  105.         pCur_node = pCur_node->next_node;
  106.     }
  107.     printf("\n");
  108. }


  109. /**/
  110. int main(void)
  111. {
  112.     list_node_t *pHead = NULL;
  113.     int length = -1;

  114.     /**/
  115.     pHead = Creat_head_node();
  116.     if(pHead == NULL)
  117.     {
  118.         printf("Creat_head_node Failed\n");
  119.         return -1;
  120.     }

  121.     /**/
  122.     Insert_list_head(pHead, 7);
  123.     Insert_list_head(pHead, 5);
  124.     Insert_list_head(pHead, 48);
  125.     Insert_list_head(pHead, 46);
  126.     Insert_list_head(pHead, 46);
  127.     Insert_list_head(pHead, 46);
  128.     Insert_list_head(pHead, 48);
  129.     Insert_list_head(pHead, 46);
  130.     length = Get_length_of_list(pHead);
  131.     printf("length:%d\n", length);
  132.     Print_list(pHead);

  133.     /**/
  134.     Delete_node(pHead, 46);
  135.     Print_list(pHead);

  136.     return 0;


执行结果:
46 48 46 46 46 48 5 7 
delete a node
delete a node
delete a node
delete a node
48 48 5 7 

阅读(2237) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~