1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct linklist
  5. {
  6. unsigned int count;
  7. struct linklist *next;
  8. }linklist;
  9.  
  10. linklist* CreatLinklist(int len)
  11. {
  12. linklist *head = NULL, *pre = NULL;
  13. head = (linklist*)malloc(sizeof(linklist));//头指针。
  14. pre = (linklist*)malloc(sizeof(linklist)); //pre-previous, 前一个;相对于head来说是后一个。
  15. printf("Please input the data:\n");
  16. scanf("%u", &pre->count);
  17. head->next = pre; //head的后一个是pre。
  18. pre->next = NULL; //pre的next是NULL。
  19.  
  20. for (int i = ; i < len; i++)
  21. {
  22. linklist* tmp = (linklist*)malloc(sizeof(linklist));
  23. scanf("%u", &tmp->count);
  24. pre->next = tmp; //新加的节点放在pre后。
  25. tmp->next = NULL; //新加节点的next是NULL。
  26. pre = tmp; //新加的节点变成pre,等待下一个tmp。
  27. }
  28. return head;
  29. }
  30.  
  31. int Outputlinklist(linklist *head)
  32. {
  33. printf("#### Output linklist.\n");
  34. if (head != NULL)
  35. {
  36. while (head->next != NULL)
  37. {
  38. printf("%u\n", head->next->count);
  39. head = head->next;
  40. }
  41. }
  42. }
  43.  
  44. int Lenlinklist(linklist *head)
  45. {
  46. int len = ;
  47. if (NULL == head->next)
  48. return len;
  49. len++;
  50. linklist *p = NULL;
  51. p = head->next;
  52. while(p->next != NULL)
  53. {
  54. len++;
  55. p = p->next;
  56. }
  57. return len;
  58. }
  59.  
  60. int InsertLinklist(linklist *head, int index)
  61. {
  62. int len = ;
  63. len = Lenlinklist(head);
  64. printf("the length of linklist:%d, the insert index:%d\n", len, index);
  65. if (index < || index > len)
  66. return -;
  67. int cur_index = ;
  68. linklist *p = NULL;
  69. p = head->next;
  70. while(cur_index < index)
  71. {
  72. p = p->next;
  73. cur_index++;
  74. }
  75. linklist *node = (linklist*)malloc(sizeof(linklist));
  76. printf("Please input the new node data:\n");
  77. scanf("%u", &node->count);
  78. node->next = p->next;
  79. p->next = node;
  80. return ;
  81. }
  82.  
  83. int DeleteNode(linklist *head, int index)
  84. {
  85. int len = ;
  86. len = Lenlinklist(head);
  87.  
  88. if((index < ) || (index > len))
  89. return -;
  90.  
  91. int cur_index = ;
  92. linklist *p = NULL;
  93. linklist *q = NULL;
  94. p = head->next;
  95. while(cur_index < index)
  96. {
  97. p = p->next;
  98. cur_index++;
  99. }
  100. q = p->next;
  101. p->next = q->next;
  102. free(q);
  103. q = NULL;
  104. }
  105.  
  106. linklist *Reverselinklist(linklist *head)
  107. {
  108. linklist *p=NULL, *pre=NULL, *pnext=NULL;
  109.  
  110. p = head->next;
  111. while(p!=NULL)
  112. {
  113. pnext = p->next;
  114. if(pnext == NULL)
  115. head->next = p;
  116. p->next = pre;
  117. pre = p;
  118. p = pnext;
  119. }
  120. return head;
  121. }
  122.  
  123. int main(void)
  124. {
  125. linklist *mylist = NULL;
  126.  
  127. printf("#### Creat linklist.\n");
  128. mylist = CreatLinklist();
  129. Outputlinklist(mylist);
  130.  
  131. printf("#### Insert new node.\n");
  132. InsertLinklist(mylist, );
  133. Outputlinklist(mylist);
  134.  
  135. printf("#### Delete exist node.\n");
  136. DeleteNode(mylist, );
  137. Outputlinklist(mylist);
  138.  
  139. printf("#### Reverse Linklist.\n");
  140. Reverselinklist(mylist);
  141. Outputlinklist(mylist);
  142.  
  143. printf("Handle complete!");
  144. }

1.线性表的链式存储和顺序存储的比较。

常见的数组就是线性表的顺序存储,各个结点之间被分配连续的物理内存,因此在查找上直接可以使用下标,速度很快,但是对于插入和删除操作需要在操作点之后执行移动操作,较为麻烦,并且数组在之前需要分配内存的大小,也就是需要我们知道结点的数量,这样就有一定的限制。
链表就是线性表的链式存储,各个结点之间在物理内存中是随机分配的,在查找时需要逐个遍历,但是在插入和删除操作时仅仅是局部性的指针切换,并且链表只有创建结点和删除结点时才会对内存进行操作,事先不需要知道结点数量。

2.单向链表属于线性表链式存储中最简单的一类,常见的操作包括:创建、删除、查找、插入、反序等。

July_One_Week—linked list的更多相关文章

  1. [LeetCode] Linked List Random Node 链表随机节点

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  2. [LeetCode] Plus One Linked List 链表加一运算

    Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...

  3. [LeetCode] Odd Even Linked List 奇偶链表

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  4. [LeetCode] Delete Node in a Linked List 删除链表的节点

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  5. [LeetCode] Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...

  6. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  7. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  8. [LeetCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  9. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

随机推荐

  1. 力扣(LeetCode)1016. 子串能表示从 1 到 N 数字的二进制串

    给定一个二进制字符串 S(一个仅由若干 '0' 和 '1' 构成的字符串)和一个正整数 N,如果对于从 1 到 N 的每个整数 X,其二进制表示都是 S 的子串,就返回 true,否则返回 false ...

  2. 《剑指offer》第六十一题(扑克牌的顺子)

    // 面试题61:扑克牌的顺子 // 题目:从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的. // 2-10为数字本身,A为1,J为11,Q为12,K为13,而大.小王可以看成任意 ...

  3. 《剑指offer》第四十四题(数字序列中某一位的数字)

    // 面试题44:数字序列中某一位的数字 // 题目:数字以0123456789101112131415…的格式序列化到一个字符序列中.在这 // 个序列中,第5位(从0开始计数)是5,第13位是1, ...

  4. python实战小程序之购物车

    # Author:南邮吴亦凡 # 商品列表 product_list = [ ('Iphone',5800), # 逗号一定不可以省略! ('Mac',4800), ('smartphone',400 ...

  5. boke练习: springboot整合springSecurity出现的问题,传递csrf

    boke练习: springboot整合springSecurity出现的问题,传递csrf freemarker模板 在html页面中加入: <input name="_csrf&q ...

  6. tornado web

    tornado web frame: 非阻塞服务器,速度快,运用epoll 模板语言+render(),实现根据用户输入,自动渲染页面的动态效果. 在使用模板前需要在setting中设置模板路径: s ...

  7. Winform开发框架之框架演化

    Winform开发框架方面的文章我介绍很多了,有宏观介绍,也有部分技术细节的交流,每次我希望能从不同角度,不同方面来介绍我的WInform开发框架,这些其实都是来源于客户的需求,真实的项目场景.本文主 ...

  8. C#使用 System.Net.Mail发送邮件功能

    .NET 里包含了很多很丰富的邮件发送与接受的API在 System.Net.Mail命名空间里,使得我们开发发送和接受邮件相关功能变得简单,下面是一个简单发送邮件的功能: private void ...

  9. hdu6331 Walking Plan

    题意: sol: 考虑floyed 直接暴力做的话复杂度是kn^3会炸. 考虑一个比较神仙的分块做法. 注意到我们是可以直接求单独某个k的矩阵,使用矩阵快速幂即可(取min的矩阵乘法). 单独求一次的 ...

  10. MySQL表类型和存储引擎

    一.基本介绍 从事务安全性的角度,可以把存储引擎分为两大类: 事务安全: BDB和innodb; 事务非安全性: myisam 和 memory 二.存储引擎的比较图 看你的mysql当前默认的存储引 ...