Blame view

Common/gen_linklist.c 2.12 KB
9d8bbd1c   李外   改升级前备份
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  #include    "gen_linklist.h"
  #include "platform_interface.h"
  
  // https://blog.csdn.net/swwlqw/article/details/22498833
  
  
  //#include    "platform_extern.h"
  /*-------------- insert ------------------------------
  * insert data to any postion
  */
  unsigned char gen_linklist__insert(GenLinkList* const list,void *pele,unsigned short index)
  {
      if(list->length < index) return 0;
  platform.disable_interrupt();//	
      GenLinkNode *node = (GenLinkNode *)malloc(sizeof(GenLinkNode));
  		
  		while(node ==NULL){//debug only
  			//my_debug.printf_error("gen_linklist__insert() malloc fail \n");
  			//platform.system_intfp->delay_ms(500);
  			platform.enable_interrupt();//
  			return 0;
  		}
  	
      node->next = NULL;
      node->pdata = pele;
  
      GenLinkNode *pnode_tmp= list->front;
      for(unsigned short i=0;i<index;i++){
          pnode_tmp =pnode_tmp->next;
      }//for
  
      node->next =pnode_tmp->next;
      pnode_tmp->next =node;
  platform.enable_interrupt();//
      (list->length)++;  
  
      return 1;  
  }
  
  /* -------------- detele  ------------------------------
  * detele node at any postion
  */
  void* gen_linklist__detele(GenLinkList* const list,unsigned short index)
  {
      if(list->length < index) return NULL;
  platform.disable_interrupt();//
      GenLinkNode *pnode_tmp= list->front;
      for(unsigned short i=0;i<index;i++){
          pnode_tmp =pnode_tmp->next;
      }//for
  
      GenLinkNode *pnode_record= pnode_tmp->next;
      pnode_tmp->next = pnode_tmp->next->next;
      void * re =pnode_record->pdata;
      free(pnode_record);
  		if(list->length)
  			(list->length)--;
  platform.enable_interrupt();//		
      return re;
  }
  
  /*-------------- get ele ------------------------------
  * get element of index;
  */
  void* gen_linklist__get_ele(GenLinkList* const list,unsigned short index)
  {
      if( list->length < index) return NULL;
  
      GenLinkNode *pnode_tmp= list->front->next;//diff with no head
      for(unsigned short i=0;i<index;i++){
          pnode_tmp =pnode_tmp->next;
      }//for
  
      return pnode_tmp->pdata;
  }
  
  /*-------------- get len ------------------------------
  * get length of linklist 
  */
  unsigned short gen_linklist__get_len(GenLinkList* const list)
  {
      return list->length;
  }