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

后来读了一些读物,了解到 数据结构 就是如何表示 数据 的一种形式,计算机什么是计算,计算就是机械式的信息处理,信息是什么,信息就是数字,一列列的 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 }

受益良多

转载于:https://www.cnblogs.com/AI-Cobe/p/9347398.html

Linked list(单链表)相关推荐

  1. 单链表(Singly LInked LIst)

    单链表(Singly Linked List) 单链表是比较常见的链表类型.其中每个节点都有一个指向序列下一个节点的指针.这意味着,对链表执行遍历时,只能在一个方向上从头到尾的遍历:为了能够访问链表中 ...

  2. 链表(Linked List)之单链表

    原文地址:传送门 链表(Linked List)介绍 链表是有序的列表,但是它在内存中是存储如下 小结: 链表是以节点的方式来存储,是链式存储 每个节点包含 data 域, next 域:指向下一个节 ...

  3. python实现单链表快速排序升序linkedqueue_LeetCode 总结 - 搞定 Linked List 面试题

    链表删除 [203] Remove Linked List Elements [19] Remove Nth Node From End of List [83] Remove Duplicates ...

  4. 基本的数据结构:单链表(Singly Linked List)

    一.基本的数据结构:单链表(Singly Linked List) 什么是单链表? 下面是百度百科给出的官方解释: 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素.链表 ...

  5. 数据结构与算法(二)单链表(Singly linked list)

    数据结构与算法(二)单链表(Singly linked list) 链表(Linked list) Python完整功能实现 LeetCode思想实践: 链这个东西大家一定都不陌生, 脖子上有项链, ...

  6. Java数据结构3_单链表 Linked List

    链表(Linked List)介绍 链表是有序的列表,但是它在内存中的存储如下: 链表是以节点的方式来存储 每个节点包含data域,next域:指向下一个节点 如上图,发现链表的各个节点不一定是连续存 ...

  7. 数据结构 - 单链表(Linked List)实现在内存中实现数据以链表形式生成并根据序号排序

    下面实现一个例子来进行学习 1)介绍 单链表的逻辑结构 在内存中的实际结构 具体创建示意图: 2)代码实现 例子 1.第一个程序在添加的时候并没有按照序号排序,如果在添加的时候把位置改变输出的时候序号 ...

  8. 数据结构--单链表single linked list(无表头哨兵)重写

    针对上次写的单链表中不足的地方进行修改: 1.构造函数需要让用户输入(bad) 2.函数功能不单一,既操作链表,还打印输出(bad) 代码链接(包含无头\有头单链表.循环单链表.双链表.循环双链表) ...

  9. 数据结构--单链表single linked list数据结构C++实现

    2018年2月开始学习的 C++ Primer,到今天2019年3月已经整整一年了,非常感谢在一起交流的小伙伴,是你们的无私帮助和分享使得我能跨越很多技术的坑,感谢你们!期待我们2019年一起拿下&l ...

最新文章

  1. 十年AI学者影响力盘点:何恺明排名第一,华人学者呈正向流入
  2. wpf 自定义Button按钮
  3. 驻极体,PN结以及电子管相关问题
  4. ActionScript 3.0 概要
  5. Windows Shell 编程 第六章 【来源:http://blog.csdn.net/wangqiulin123456/article/details/7987951】...
  6. 【LeetCode】剑指 Offer 25. 合并两个排序的链表
  7. consul java_springcloud使用consul作为配置中心
  8. fastjson字符串json转对象(父类转子类)
  9. sql: table,view,function, procedure created MS_Description in sql server
  10. 计算机网络管理的应用,计算机网络管理技术及应用
  11. 基于STM32单片机的精彩设计实例合集
  12. outlook邮件插入HTML格式内容调试
  13. RAID技术及应用总结
  14. efi分区能不能删除 win10_Win10删除了EFI分区:以下是恢复它的方法
  15. 阵列服务器怎么拆硬盘盒,磁盘阵列硬盘盒拆解和安装,很详细
  16. 如何画一块标准的PCB板?SMT工艺PCB要求
  17. 如何用计算机进行照片合成,不借助软件怎样将两张照片合成一张?一招教你搞定!...
  18. 蓝牙信标有哪些附加功能?蓝牙信标的工业用途知多少?
  19. HiveSql工作中常见易错点总结
  20. Codeforces 332B Maximum Absurdity(DP+前缀和处理)

热门文章

  1. Java操作Kafka执行不成功
  2. android控件跟随手势滑动改变位置
  3. shell脚本替换文件中字符
  4. 3.C#中泛型类的进一步探讨
  5. 图像处理之基础---高斯低通滤波在指定区域画放大圆形图
  6. Delphi 2009 中 TStrings 与 TStream 的增强
  7. 这两天被木马程序搞得好烦
  8. vscode进行远程服务器 An SSH installation couldn't be found、
  9. 时序预测的必要条件是基于策略和环境相对稳定前提的
  10. SSH项目的简单table及其分页框架