数据结构---单向链表的实现(内含c代码,已调试,可用)

10 个数据结构:数组链表队列散列表二叉树跳表Trie 树

本节先对单向链表的学习(主要是使用代码进行实现

 

1:单向链表的理论介绍:

链表原理如下图所示:

 

 

  上面展示的是一个单链表的存储原理图,简单易懂,head为头节点,他不存放任何的数据,只是充当一个指向链表中真正存放数据的第一个节点的作用,而每个节点中都有一个next引用,指向下一个节点,就这样一节一节往下面记录,直到最后一个节点,其中的next指向null。

 链表有很多种,比如单链表,双链表等等。我们就对单链表进行学习,其他的懂了原理其实是一样的。

(上面的理论也是粘贴复制的因为有太多的理论介绍了。)

 

2:单向链表的代码实现

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 
  4 
  5 typedef enum
  6 {
  7     //MAC_TIMESYNC_UNKNOW_STATE     =    -1,
  8     MAC_TIMESYNC_SET_INIT         =    0,//If it is in this state, the next cycle will still execute the program
  9     MAC_TIMESYNC_SET_EXECTUING     =    1,//function is executing
 10     MAC_TIMESYNC_SET_SUCCESS     =     2,//This state indicates that the formal execution of program logic is completed this time, and the state is set
 11     MAC_TIMESYNC_SET_FAIL         =    3,//func function execution failed
 12 }DpsPortMacTimeState;
 13 
 14 typedef struct  dpsi_mac_time_Sync_State_s{
 15     int timesync_sates;
 16     int bcmport;
 17 }dpsi_mac_time_Sync_State_t;
 18 
 19 typedef struct node{
 20      struct node * next;
 21      dpsi_mac_time_Sync_State_t data;
 22 }dpsi_mac_time_sync_state_list_t;
 23 
 24 dpsi_mac_time_sync_state_list_t* dpsi_mac_list_head = NULL;
 25 
 26 static void __dpsi_mac_state_list_init(void)
 27 {
 28     //Apply for linked list heap space
 29     if(NULL == dpsi_mac_list_head)
 30     {
 31         dpsi_mac_list_head = (dpsi_mac_time_sync_state_list_t*)malloc(sizeof(dpsi_mac_time_sync_state_list_t));
 32         if(NULL == dpsi_mac_list_head)
 33         {
 34              printf("Error: __dpsi_mac_state_list_init\n");  
 35             return;
 36         }
 37     }
 38     
 39     return;
 40 }
 41 
 42 dpsi_mac_time_sync_state_list_t *  __dpsi_mac_state_list_get_ptr(int bcmPort)
 43 {
 44     /*Request a temporary head pointer. 
 45         Avoid direct operation on the head pointer and lose the position of the head pointer*/
 46     dpsi_mac_time_sync_state_list_t* tmp_list_ptr = NULL;
 47 
 48     tmp_list_ptr = dpsi_mac_list_head;
 49     while(NULL != tmp_list_ptr && NULL != tmp_list_ptr->next)
 50     {
 51         /*Get next node*/
 52         if(bcmPort == tmp_list_ptr->data.bcmport)
 53         {
 54                 printf("end  p54:%d\n",bcmPort);
 55             return tmp_list_ptr;
 56         }
 57         tmp_list_ptr = tmp_list_ptr->next;
 58     }
 59     printf("end  p59:%p\n",tmp_list_ptr);
 60     return NULL;
 61 }
 62 
 63 int dpsi_mac_state_list_set(int bcmPort, int timesync_sates)
 64 {
 65     dpsi_mac_time_sync_state_list_t* tmp_list_ptr = NULL;
 66     dpsi_mac_time_sync_state_list_t * insert_list_data = NULL;
 67 
 68     tmp_list_ptr = __dpsi_mac_state_list_get_ptr(bcmPort);
 69     if(NULL != tmp_list_ptr)
 70     {
 71             printf("end  p70:%d\n",bcmPort);
 72         tmp_list_ptr->data.timesync_sates = timesync_sates;
 73         return 1;
 74     }
 75     else
 76     {
 77             tmp_list_ptr = NULL;
 78         
 79     }
 80     
 81     if(NULL == dpsi_mac_list_head)
 82     {
 83         printf("%s dpsi_mac_port_phy_timesync_set 1 Failed: line:%d, bcmPort=%d\n", 
 84                 __FUNCTION__, __LINE__, bcmPort);
 85         return -1;
 86     }
 87     tmp_list_ptr = dpsi_mac_list_head;
 88 
 89     while(NULL != tmp_list_ptr->next)
 90     {
 91         /*Get the last node*/
 92         tmp_list_ptr = tmp_list_ptr->next;
 93     }
 94 
 95     /*Apply for heap space of linked list nodes*/
 96     insert_list_data = (dpsi_mac_time_sync_state_list_t*)malloc(sizeof(dpsi_mac_time_sync_state_list_t));
 97     if(NULL == insert_list_data)
 98     {
 99         printf("%s dpsi_mac_port_phy_timesync_set 1 Failed: line:%d, bcmPort=%d\n", 
100                 __FUNCTION__, __LINE__, bcmPort);
101         return -1;
102     }
103     printf("end  p102 : %d\n",bcmPort);
104     insert_list_data->data.bcmport = bcmPort;
105     insert_list_data->data.timesync_sates = timesync_sates;
106     insert_list_data->next = NULL;
107     tmp_list_ptr->next = insert_list_data;
108     return 1;
109 }
110 
111 
112 int dpsi_mac_state_list_get_value(int bcmPort)
113 {
114     /*Request a temporary head pointer. 
115         Avoid direct operation on the head pointer and lose the position of the head pointer*/
116     dpsi_mac_time_sync_state_list_t* tmp_list_ptr = NULL;
117 
118     tmp_list_ptr = dpsi_mac_list_head->next;
119     while(NULL != tmp_list_ptr )//&& NULL != tmp_list_ptr->next)
120     {
121         /*Get next node*/
122         printf("dpsi_mac_list_head is  %d:%d:%d \n",bcmPort,tmp_list_ptr->data.bcmport,tmp_list_ptr->data.timesync_sates);
123         if(bcmPort == tmp_list_ptr->data.bcmport)
124         {
125             return tmp_list_ptr->data.timesync_sates;
126         }
127         tmp_list_ptr = tmp_list_ptr->next;
128     }
129     
130     return -1;//MAC_TIMESYNC_SET_INIT;
131 }
132 
133 void print_dpsi_mac_state_list()
134 {
135     //申请一个临时的head指针。避免对head指针的直接操作,丢失头指针的位置
136     dpsi_mac_time_sync_state_list_t* tmp_list_ptr = NULL;
137 
138     tmp_list_ptr = dpsi_mac_list_head->next;
139     while(NULL != tmp_list_ptr )//&& NULL != tmp_list_ptr->next)
140     {
141         //获取下一个节点
142         printf("%d:%d\n",tmp_list_ptr->data.bcmport,tmp_list_ptr->data.timesync_sates);
143         tmp_list_ptr = tmp_list_ptr->next;
144         
145     }
146     return;
147 }
148 int main()
149 {
150 
151     __dpsi_mac_state_list_init();
152     printf("start \n");
153     if(NULL != dpsi_mac_list_head)
154     {
155         
156         dpsi_mac_state_list_set(1,11);
157         dpsi_mac_state_list_set(2,22);
158         dpsi_mac_state_list_set(3,33);
159         dpsi_mac_state_list_set(4,44);
160         dpsi_mac_state_list_set(5,55);
161         dpsi_mac_state_list_set(4,99);
162         
163     }
164     else
165     {
166             printf("dpsi_mac_list_head is  null \n");
167         
168     }
169     
170     //printf("end  3:%d\n",dpsi_mac_state_list_get_value(10));
171     print_dpsi_mac_state_list();
172     return 0;
173 
174 }

3:运行结果:

 

posted @ 2022-03-08 11:35  怎因一双媚眼惹尘埃  阅读(70)  评论(0编辑  收藏  举报