Chinaunix首页 | 论坛 | 博客
  • 博客访问: 257096
  • 博文数量: 49
  • 博客积分: 110
  • 博客等级: 民兵
  • 技术积分: 510
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-13 00:59
个人简介

make it run,make it better,make it fast. https://github.com/liulanghaitun

文章分类

全部博文(49)

文章存档

2023年(1)

2022年(2)

2020年(4)

2019年(4)

2017年(15)

2016年(3)

2014年(3)

2013年(14)

分类: C/C++

2017-10-22 10:12:46


点击(此处)折叠或打开

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

  3. typedef struct node
  4. {
  5.     int index;
  6.     struct node* next;
  7. }list_node;

  8. list_node *create_node(int data)
  9. {
  10.     list_node * head = malloc(sizeof(list_node));
  11.     if(head)
  12.     {
  13.         head->index = data;
  14.         head->next = NULL;
  15.     }
  16.     return head;
  17. }


  18. void add_node(list_node* head ,int date)
  19. {
  20.     while(head->next)
  21.     {
  22.         head = head->next;
  23.     }
  24.     head->next = create_node(date);
  25. }

  26. int insert_node(list_node** current,int position,int data)
  27. {
  28.     list_node* baseNode = NULL;
  29.     list_node* newNode = NULL;
  30.     int index = 0;
  31.     while(*current)
  32.     {
  33.         baseNode = *current;
  34.         if(index++ == position)
  35.         {
  36.             newNode = create_node(data);
  37.             newNode->next = baseNode;
  38.             *current = newNode;
  39.             return 0;
  40.         }
  41.         current = &baseNode->next;
  42.     }
  43.     if(index == position)
  44.      {
  45.          newNode = create_node(data);
  46.         *current = newNode;
  47.      }
  48.     return 0;
  49. }

  50. list_node* insert_list_node(list_node** current,int position,int data)
  51. {
  52.     if(*current)
  53.     {
  54.         if(position == -1)
  55.         {
  56.              list_node* message= create_node(data);
  57.              message->next = *current;
  58.              return message;
  59.         }else{
  60.             (*current)->next = insert_list_node(&(*current)->next,--position,data);
  61.             return *current;
  62.         }
  63.     }
  64.     return position == -1?create_node(data):NULL;
  65. }

  66. list_node* del_list_node(list_node** current,int position)
  67. {
  68.     if(*current)
  69.     {
  70.         if(position == -1)
  71.         {
  72.             list_node* message = (*current)->next;
  73.             free(*current);
  74.             *current =  NULL;
  75.             return message;
  76.         }else{
  77.              (*current)->next = del_list_node(&(*current)->next,--position);
  78.             return *current;
  79.         }
  80.     }
  81.     return NULL;
  82. }

  83. list_node* lookup_list_node(list_node** current, int position)
  84. {
  85.     if(*current)
  86.     {
  87.         if(position == -1)
  88.         {
  89.             return *current;
  90.         }else{
  91.             return lookup_list_node(&(*current)->next,--position);
  92.         }
  93.     }
  94.     return NULL;
  95. }

  96. list_node* replace_list_node(list_node** current, int position,int data)
  97. {
  98.     if(*current)
  99.     {
  100.         if(position == -1)
  101.         {
  102.             (*current)->index = data;
  103.         }else{
  104.              (*current)->next = replace_list_node(&(*current)->next,--position,data);
  105.         }
  106.         return *current;
  107.     }
  108.     return NULL;
  109. }


  110. void del_node(list_node **current,int data)
  111. {
  112.     list_node* baseNode = NULL;
  113.     while(*current)
  114.     {
  115.         baseNode = *current;
  116.         if(baseNode->index == data)
  117.         {
  118.             *current = baseNode->next;
  119.             free(baseNode);
  120.             baseNode = NULL;
  121.             return;
  122.         }
  123.         current = &baseNode->next;
  124.     }
  125. }


  126. list_node* create_list(const int *input,int length)
  127. {
  128.     if(length == 1)
  129.     {
  130.         return create_node(*input);
  131.     }
  132.     list_node* current = create_node(*input);
  133.     current->next = create_list(++input,--length);
  134.     return current;
  135. }


  136. list_node* reverse_list(list_node* input,struct node* pre_input)
  137. {
  138.     list_node* current = input;
  139.     if(current->next)
  140.     {
  141.         current = reverse_list(current->next,current);
  142.     }
  143.     input->next = pre_input;
  144.     return current;
  145. }

  146. void del_list(list_node* input)
  147. {
  148.     list_node* current = input;
  149.     while(current)
  150.     {
  151.         printf("delete:%d\r\n",input->index);
  152.         current = input->next;
  153.         free(input);
  154.         input = current;
  155.     }
  156. }

  157. void print_list(list_node* head)
  158. {
  159.     while(head->next)
  160.     {
  161.         printf("%d ",head->next->index);
  162.         head = head->next;
  163.     }
  164. }


  165. int main(void)
  166. {
  167.        int value[] = {0,13,26,39,41,56,67,70};
  168.        list_node *head = create_node(-1);
  169.        head->next = create_list(value,sizeof(value)/sizeof(value[0]));
  170.        print_list(head);
  171.        head->next = reverse_list(head->next,NULL);
  172.        printf("\r\n");
  173.        print_list(head);
  174.        del_node(&head,56);
  175.        printf("\r\n");
  176.        print_list(head);
  177.        insert_node(&head->next,7,110);
  178.        printf("\r\n");
  179.        print_list(head);
  180.        printf("\r\n");
  181.        printf("insert==%d\r\n",insert_list_node(&head,0,876)->index);
  182.        print_list(head);
  183.        del_list_node(&head,5);
  184.        printf("\r\n");
  185.        print_list(head);
  186.        printf("\r\n");
  187.        list_node * lookup_node = lookup_list_node(&head,5);
  188.        if(lookup_node)
  189.        {
  190.             printf("result==%d",lookup_node->index);
  191.        }else{
  192.             printf("not found!");
  193.        }
  194.        replace_list_node(&head,7,150);
  195.        printf("\r\n");
  196.        print_list(head);
  197.        printf("\r\n");
  198.        del_list(head);
  199.        return 0;
  200. }


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