Linked list(单链表)

不记得是大一还是大二开的《数据结构》,当时可真是可爱,按照我当时的想法就是这东西有什么用,目光短浅的我。。。学什么都是学这个有什么用啊?典型的一枚功利分子。

后来读了一些读物,了解到 数据结构 就是如何表示 数据 的一种形式,计算机什么是计算,计算就是机械式的信息处理,信息是什么,信息就是数字,一列列的 binary number。

但是在处理信息时,考虑到了 效率,人就是懒,不懒不进步。把信息以一种什么样的形式交给计算机,就可以立马得出 result。这就用到了 数据结构 + 算法,虽然算法还没看。。。

在GeeksforGeeks学DS,我也真的是有意思。

Linked list 单链表,优点和缺点也已经写过了。

  1 #include<stdlib.h>
  2 #include<stdio.h>
  3 
  4 //Linked list from FengSF
  5 struct node
  6 {
  7     int data;
  8     struct node * next;
  9 };
 10 void printList(struct node * n)
 11 {
 12     while(NULL != n)
 13     {
 14         printf("%d\t", n->data);
 15         n = n->next;
 16     }
 17 
 18 }
 19 //在头结点之前插入新节点
 20 void push(struct node ** head_ref, int new_data)
 21 {
 22     struct node * new_node = (struct node*) malloc(sizeof(struct node));
 23 
 24     new_node->data = new_data;
 25     new_node->next = (*head_ref);
 26     (*head_ref) = new_node;
 27 }
 28 //链表中间插入新节点
 29 void afterAdd(struct node * prev_node, int new_data)
 30 {
 31     if(NULL == prev_node)
 32     {
 33         printf("ERROR!");
 34         return;
 35     }
 36 
 37     struct node * new_node = (struct node *) malloc (sizeof(struct node));
 38     new_node->data = new_data;
 39     new_node->next = prev_node->next;
 40     prev_node->next = new_node;
 41 }
 42 //在尾节点之后插入新节点
 43 void endAdd(struct node ** head_ref, int new_data)
 44 {
 45     struct node * new_node = (struct node *) malloc (sizeof(struct node));
 46     struct node * last = (*head_ref);
 47     new_node->data = new_data;
 48     new_node->next = NULL;
 49 
 50     if(NULL == (*head_ref))
 51     {
 52         (*head_ref) = new_node;
 53         return ;
 54     }
 55 
 56     while(NULL != last->next)
 57     {
 58         last = last->next;
 59     }
 60 
 61     last->next = new_node;
 62 }
 63 //移除结点通过data
 64 void deleteElementByKey(struct node ** head_ref, int key)
 65 {
 66     struct node * temp = (*head_ref), * pre;
 67 
 68     if(NULL != temp && temp->data  == key)
 69     {
 70         (*head_ref) = temp->next;
 71         free(temp);
 72         return ;
 73     }
 74 
 75     while(NULL != temp && temp->data != key)
 76     {
 77         pre = temp;
 78         temp = temp->next;
 79     }
 80     if(NULL == temp)
 81     {
 82         printf("The element isn`t exist!");
 83         return ;
 84     }
 85     pre->next = temp->next;
 86     free(temp);
 87 }
 88 //移除结点通过位置
 89 void deleteElementByPosition(struct node ** head_ref, int position)
 90 {
 91     struct node * temp = (*head_ref), * pre;
 92     int listLength = linkedListLength(temp);
 93 
 94     if(position < 0 && position > listLength)
 95     {
 96         printf("The position is error!");
 97         return ;
 98     }
 99     temp = (*head_ref);
100     if(NULL != temp && 1 == position)
101     {
102         (*head_ref) = temp->next;
103         free(temp);
104         return ;
105     }
106     int times = 0;
107     while(NULL != temp && (position-1) != times)
108     {
109         pre = temp;
110         temp = temp->next;
111         times++;
112     }
113     if(NULL == temp)
114     {
115         printf("The position %d isn`t exist! and the linkedlist`s length is %d\n", position, listLength);
116         return ;
117     }
118     pre->next = temp->next;
119     free(temp);
120 }
121 //销毁链表
122 void destroyLinkedlist(struct node ** head_ref)
123 {
124     struct node * current = (*head_ref), *next;
125 
126     while(NULL != current)
127     {
128         next = current->next;
129         free(current);
130         current = next;
131     }
132 
133     (*head_ref) = NULL;
134 }
135 //链表的长度
136 int linkedListLength(struct node * head_ref)
137 {
138     int length = 0;
139 
140     while(NULL != head_ref)
141     {
142         length++;
143         head_ref = head_ref->next;
144     }
145 
146     return length;
147 }
148 //搜索元素值是否存在于链表中
149 int searchElement(struct node * head_ref, int key)
150 {
151     int judge;
152 
153     while(NULL != head_ref && key != head_ref->data)
154     {
155         head_ref = head_ref->next;
156     }
157     if(NULL == head_ref)
158     {
159         judge = 0;
160         return judge;
161     }
162 
163     judge = 1;
164     return judge;
165 }
166 //通过结点的位置获取结点
167 struct node * getNodeByPosition(struct node * head_ref, int position)
168 {
169     int length = linkedListLength(head_ref);
170 
171     if(NULL == head_ref)
172     {
173         printf("\n The linked list no elements");
174     }
175     if(position <= 0 && position > length)
176     {
177         printf("\n Error ");
178         return ;
179     }
180     int times = 0;
181     while(NULL != head_ref && times != position-1)
182     {
183         head_ref = head_ref->next;
184         times++;
185     }
186     return head_ref;
187 }
188 //获取倒数的元素
189 struct node * getNodeByPositionFromLast(struct node * head_ref, int position)
190 {
191     int length = linkedListLength(head_ref);
192 
193     if(NULL == head_ref)
194     {
195         printf("\n The linked list no elements");
196     }
197     if(-1 == boolPosition(head_ref, position))
198     {
199         printf("\nError");
200     }
201 
202     for(int i = 0; i < length-position; i++)
203     {
204         head_ref = head_ref->next;
205     }
206     return head_ref;
207 }
208 //判断所求位置是否正确
209 int boolPosition(struct node * head_ref, int position)
210 {
211     int length = linkedListLength(head_ref);
212 
213     if(position <= 0 && position > length)
214     {
215         return -1;
216     }
217 
218     return 1;
219 }
220 //返回中间的元素
221 int findMiddleElement(struct node * head_ref)
222 {
223     int length = linkedListLength(head_ref)/2 + 1;
224     //int length1 = (length%2 == 0) ? length/2+1 : length/2+1;
225 
226     //printf("\nlength1 = %d and length = %d", length1, length);
227     struct node * temp = getNodeByPosition(head_ref, length);
228     return temp->data;
229 }
230 int findMiddleElement_2(struct node * head_ref)
231 {
232     struct node * fast_ptr = head_ref;
233     struct node * slow_ptr = head_ref;
234 
235     if(NULL == head_ref)
236     {
237         printf("\n ERROR");
238         return -1;
239     }
240     while(NULL != fast_ptr && NULL != fast_ptr->next)
241     {
242         fast_ptr = fast_ptr->next->next;
243         slow_ptr = slow_ptr->next;
244     }
245 
246     return slow_ptr->data;
247 }
248 int findMiddleElement_3(struct node * head_ref)
249 {
250     struct node * mid = head_ref;
251     int counter = 0;
252 
253     while(NULL != head_ref)
254     {
255         if(counter & 1)
256         {
257             mid = mid->next;
258         }
259 
260         counter++;
261         head_ref = head_ref->next;
262     }
263 
264     return mid->data;
265 }
266 //不用递归返回元素个数
267 int countWhithoutRecursion(struct node * head_ref, int key)
268 {
269     int counter = 0;
270     int length = linkedListLength(head_ref);
271 
272     if(-1 == headIsNULL(head_ref))
273     {
274         printf("The linked list is null!");
275         return -1;
276     }
277     for(int i = 0; (i < length && head_ref != NULL); i++)
278     {
279         if(key == head_ref->data)
280         {
281             counter++;
282             head_ref = head_ref->next;
283         }
284         else
285         {
286             head_ref = head_ref->next;
287         }
288     }
289 
290     return counter;
291 }
292 /*void sortList(struct node ** head_ref)
293 {
294     struct node * temp = (*head_ref);
295     struct node * pre = NULL;
296 
297     if(-1 == headIsNULL(*head_ref))
298     {
299         printf("ERROR!");
300         return ;
301     }
302     else
303     {
304         if(temp->data > temp->next->data)
305         {
306             temp->next = temp->next->next;
307             temp->next->next = temp;
308             (*head_ref) = temp;
309         }
310         temp = (*head_ref);
311 
312         while(temp != NULL)
313         {
314             if(temp->data <= temp->next->data)
315             {
316                 pre = temp;
317                 temp = temp->next;
318             }
319             else
320             {
321                 pre->next = temp->next;
322                 temp->next = temp->next->next;
323                 temp->next->next = temp;
324                 temp =
325             }
326         }
327     }
328 }*/
329 //判断是否为循环链表
330 int isCircular(struct node * head_ref)
331 {
332     if(NULL == head_ref)
333     {
334         return 1;
335     }
336 
337     struct node * temp = head_ref->next;
338 
339     while(temp != NULL && temp != head_ref)
340     {
341         temp = temp->next;
342     }
343 
344     return (temp == head_ref) ? 1 : -1;
345 }
346 static int counter = 0;
347 //通过递归返回节点个数
348 int countWithRecursion(struct node * head_ref, int key)
349 {
350     if(NULL == head_ref)
351     {
352         return counter;
353     }
354     if(key == head_ref)
355     {
356         counter++;
357     }
358     countWhithoutRecursion(head_ref->next, key);
359 }
360 //判断是否为空链表
361 int headIsNULL(struct node * head_ref)
362 {
363     return (NULL == head_ref)? -1 : 1;
364 }
365 int main(void)
366 {
367     struct node * first = NULL;
368     struct node * second = NULL;
369     struct node * third = NULL;
370 
371     first = (struct node *)malloc(sizeof(struct node));
372     second = (struct node *)malloc(sizeof(struct node));
373     third = (struct node *)malloc(sizeof(struct node));
374 
375     first->data = 1;
376     first->next = second;
377 
378     second->data = 2;
379     second->next = third;
380 
381     third->data = 3;
382     third->next = NULL;
383 
384     endAdd(&first, 100);
385     push(&first, 99);
386     push(&first, 88);
387     push(&first, 77);
388     push(&first, 11);
389     push(&first, 66);
390     push(&first, 99);
391     push(&first, 88);
392     push(&first, 77);
393     push(&first, 11);
394     push(&first, 66);
395     //destroyLinkedlist(&first);
396     int length = linkedListLength(first);
397     printList(first);
398     printf("\nThe length of singly linked list is %d", length);
399     printf("\n The value of bool is %d", searchElement(first, 44));
400     struct node * temp = getNodeByPosition(first, 4);
401     printf("\n The node`s value is %d", temp->data);
402     struct node * temp1 = getNodeByPositionFromLast(first, 2);
403     printf("\n The node`s value is %d", temp1->data);
404     printf("\n The middle element of linked list is %d", findMiddleElement(first));
405     printf("\n The middle element of linked list is %d", findMiddleElement_2(first));
406     printf("\n The middle element of linked list is %d", findMiddleElement_3(first));
407     printf("\n Count the %d is %d", 88, countWhithoutRecursion(first, 88));
408     printf("\n Count the %d is %d", 88, countWhithoutRecursion(first, 88));
409     printf("\n Count the %d is %d", 88, countWhithoutRecursion(first, 11));
410     if(0 < isCircular(first))
411     {
412         printf("\nThe linked list is circularLinked list!------%d");
413     }
414     else
415     {
416         printf("\nThe linked list isn`t circularLinked list!");
417     }
418     return 0;
419 }

受益良多

猜你喜欢

转载自www.cnblogs.com/AI-Cobe/p/9347398.html
今日推荐