老师版

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // 定于Node数据类型
  5. struct Node
  6. {
  7. int data; // 数据域
  8. struct Node *next; // 指针域
  9. };
  10.  
  11. // 创建一个单链表,并把head节点返回;
  12. struct Node* createLinkList(void);
  13. struct Node* createLinkList(void)
  14. {
  15. struct Node *head = NULL;
  16. struct Node *tail = NULL;
  17. struct Node *temp = NULL;
  18.  
  19. int data;
  20.  
  21. scanf("%d",&data);
  22. // data 不等于0
  23. while (data)
  24. {
  25. // malloc 函数申请内存
  26. temp = (struct Node *)malloc(sizeof(struct Node));
  27. temp->data = data;
  28. temp->next = NULL;
  29.  
  30. if (head == NULL)
  31. {
  32. head = temp;
  33. tail = temp;
  34. }
  35. else
  36. {
  37. tail->next = temp;
  38. tail = temp;
  39. }
  40.  
  41. scanf("%d",&data);
  42. }
  43.  
  44. return head;
  45. }
  46.  
  47. // 输出链表元素
  48. void printLinkList(struct Node *head);
  49. void printLinkList(struct Node *head)
  50. {
  51. struct Node *p = head;
  52. if (p == NULL)
  53. {
  54. return;
  55. }
  56. else
  57. {
  58. while (p)
  59. {
  60. printf("%d-》",p->data);
  61. // 指针重定向
  62. p = p->next;
  63. }
  64. }
  65. }
  66.  
  67. // 计算链表的长度
  68. int count(struct Node *head);
  69. int count(struct Node *head)
  70. {
  71. int count = ;
  72. struct Node *p = NULL;
  73. p = head;
  74. while (p)
  75. {
  76. count++;
  77. p = p->next;
  78. }
  79.  
  80. return count;
  81. }
  82.  
  83. int getNode(struct Node *head,int pos);
  84. int getNode(struct Node *head,int pos)
  85. {
  86. if (pos > count(head))
  87. {
  88. return ;
  89. }
  90.  
  91. struct Node *p = head;
  92. for (int i = ; i < pos; i++)
  93. {
  94. p = p->next;
  95. }
  96.  
  97. return p->data;
  98. }
  99.  
  100. void insertIntoHead(struct Node **h,int value);
  101. void insertIntoHead(struct Node **h,int value)
  102. {
  103. struct Node *temp = NULL;
  104. temp = (struct Node *)malloc(sizeof(struct Node));
  105. temp->data = value;
  106. temp->next = NULL;
  107.  
  108. if (h == NULL)
  109. {
  110. *h = temp;
  111. }
  112. else
  113. {
  114. temp->next = *h;
  115. *h = temp;
  116. }
  117. }
  118.  
  119. //2、把单链表中第pos个结点的值修改为x的值,若修改成功返回1,否则返回0
  120. int modifyNode(struct Node *head,int pos,int x);
  121. int modifyNode(struct Node *head,int pos,int x)
  122. {
  123. // 如果链表为空,或者pos 超出链表长度
  124. if (head == NULL || pos > count(head))
  125. {
  126. return ;
  127. }
  128.  
  129. struct Node *p = NULL;
  130. p = head;
  131. for (int i = ; i < pos; i++)
  132. {
  133. p = p->next;
  134. }
  135.  
  136. p->data = x;
  137. return ;
  138. }
  139.  
  140. void insertIntoTail(struct Node **head,int value);
  141. void insertIntoTail(struct Node **head,int value)
  142. {
  143. struct Node *tmp = NULL;
  144. tmp = malloc(sizeof(struct Node));
  145. tmp->data = value;
  146. tmp->next = NULL;
  147. if (*head == NULL)
  148. {
  149. *head = tmp;
  150. }
  151. else
  152. {
  153. struct Node *p;
  154. // 定位最后一个节点
  155. p = *head;
  156. while (p->next)
  157. {
  158. p = p->next;
  159. }
  160. // 将申请的tmp加到链表后面
  161. p->next = tmp;
  162. }
  163. }
  164.  
  165. int insertInto(struct Node **head,int pos,int value);
  166. int insertInto(struct Node **head,int pos,int value)
  167. {
  168. if (*head == NULL)
  169. {
  170. // 在第一个位置添加一个节点
  171. insertIntoHead(head, value);
  172. return ;
  173. }
  174. if (pos > count(*head))
  175. {
  176. // 在最后一个位置添加一个节点
  177. insertIntoTail(head, value);
  178. return ;
  179. }
  180. // 申请一个节点
  181. struct Node *tmp;
  182. tmp = malloc(sizeof(struct Node));
  183. tmp->data = value;
  184. tmp->next = NULL;
  185. if (tmp==NULL)//tmp 申请内存失败
  186. {
  187. return ;
  188. }
  189. else
  190. {
  191. // 声明一个辅助指针
  192. struct Node *p;
  193. p = *head;
  194. // 定位辅助指针,指向pos位置前一个节点
  195. for (int i = ; i<pos-; i++)
  196. {
  197. p = p->next;
  198. }
  199. tmp->next = p->next;
  200. p->next = tmp;
  201. return ;
  202. }
  203. }
  204.  
  205. void insertIntoSortedList(struct Node **head,int value);
  206. void insertIntoSortedList(struct Node **head,int value)
  207. {
  208. // 如果是链表为空,在第一个位置添加
  209. if (*head == NULL)
  210. {
  211. insertIntoHead(head, value);
  212. return;
  213. }
  214.  
  215. // 记录要添加元素的位置
  216. int pos = ;
  217. struct Node *p = NULL;
  218. p = *head;
  219. while (p)
  220. {
  221. if (p->data < value)
  222. {
  223. pos++;
  224. p = p->next;
  225. }
  226. else
  227. {
  228. // 跳出循环
  229. break;
  230. }
  231. }
  232.  
  233. insertInto(head, pos, value);
  234. }
  235.  
  236. int deleteFirstNode(struct Node **head);
  237. int deleteFirstNode(struct Node **head)
  238. {
  239. if (*head == NULL)
  240. {
  241. return ;
  242. }
  243.  
  244. struct Node *p = NULL;
  245. p = *head;
  246.  
  247. *head = p->next;
  248. int result = p->data;
  249.  
  250. // 注意释放内存
  251. free(p);
  252. p = NULL;
  253.  
  254. return result;
  255.  
  256. }
  257.  
  258. int deleteTailNode(struct Node **head);
  259. int deleteTailNode(struct Node **head)
  260. {
  261. if (*head == NULL)
  262. {
  263. return ;
  264. }
  265.  
  266. struct Node *p,*q;
  267. p = q = NULL;
  268. p = *head;
  269. q = p->next;
  270. // 通过循环定位让q指向最后一个节点,p指向q前面一个节点
  271. for (int i = ; i <= count(*head)-; i++)
  272. {
  273. p = q;
  274. q = q->next;
  275. }
  276. // 取最后一个节点的数据
  277. int result = q->data;
  278.  
  279. p->next = NULL;
  280. free(q);
  281. q = NULL;
  282.  
  283. return result;
  284. }
  285.  
  286. int main(int argc, const char * argv[])
  287. {
  288.  
  289. struct Node *head = NULL;
  290. // 产生链表
  291. head = createLinkList();
  292.  
  293. // 输出链表元素个数
  294. int c = count(head);
  295. printf("c = %d\n",c);
  296.  
  297. //insertIntoHead(&head, 10);
  298.  
  299. //modifyNode(head, 2, 10);
  300.  
  301. //insertIntoTail(&head, 100);
  302. //insertInto(&head, 2, 100);
  303.  
  304. //insertIntoSortedList(&head, 6);
  305. //deleteFirstNode(&head);
  306. int r = deleteTailNode(&head);
  307. printf("删除最后一个节点%d\n",r);
  308.  
  309. // 输出链表
  310. printLinkList(head);
  311.  
  312. /*
  313. // 输出第二个节点的数据域;
  314. c = getNode(head, 2);
  315. printf("\n");
  316. printf("c = %d\n",c);
  317. */
  318.  
  319. return ;
  320. }

不才版

  1. #include <stdio.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5. struct Node
  6. {
  7. int data;
  8. struct Node *next;
  9. };
  10.  
  11. struct Node *creatLinkList(void)
  12. {
  13. struct Node *head=NULL;
  14. struct Node *tail=NULL;
  15. struct Node *temp=NULL;
  16.  
  17. int data;
  18. scanf("%d",&data);
  19. while (data)
  20. {
  21. temp=malloc(sizeof(struct Node));
  22. temp->data=data;
  23. temp->next=NULL;
  24.  
  25. if(head==NULL)
  26. head=tail=temp;
  27. else
  28. {
  29. tail->next=temp;
  30. tail=temp;
  31. }
  32. scanf("%d",&data);
  33. }
  34. return head;
  35. }
  36.  
  37. void printLinkList(struct Node *head)
  38. {
  39. struct Node *tmp=head;
  40. if (tmp==NULL) {
  41. return;
  42. }
  43. while (tmp!=NULL) {
  44. printf("%d->>",tmp->data);
  45. tmp=tmp->next;
  46. }
  47. printf("\n");
  48. }
  49.  
  50. int toNode(struct Node *head,int pos)
  51. {
  52. int data;
  53. for (int i=; i<pos; i++) {
  54. if ((i!=pos-)&&(head->next==NULL))
  55. return ;
  56. data=head->data;
  57. head=head->next;
  58. }
  59. return data;
  60.  
  61. //链表长度可以用count(head)算出。
  62. }
  63.  
  64. int changeData(struct Node *head,int pos,int x)
  65. {
  66. for (int i=; i<pos; i++) {
  67. if ((i!=pos-)&&(head->next==NULL))
  68. return ;
  69. if (i==pos-) {
  70. head->data=x;
  71. }
  72. head=head->next;
  73. }
  74. return ;
  75. }
  76.  
  77. void insertNodeInHead(struct Node **head,int value)
  78. {
  79. struct Node *tmp=NULL;
  80. tmp=(struct Node *)malloc(sizeof(struct Node));
  81. tmp->data=value;
  82. tmp->next=*head;
  83. *head=tmp;
  84. }
  85.  
  86. void insertNodeInEnd(struct Node *head,int value)
  87. {
  88. struct Node *tmp=NULL;
  89. tmp=(struct Node *)malloc(sizeof(struct Node));
  90. tmp->data=value;
  91. tmp->next=NULL;
  92. if (head==NULL) {
  93. head=tmp;
  94. }
  95. while (head->next!=NULL) {
  96. head=head->next;
  97. }
  98. head->next=tmp;
  99. }
  100.  
  101. int insertData(struct Node *head,int pos,int value)
  102. {
  103. struct Node *tmp=NULL;
  104. tmp=(struct Node *)malloc(sizeof(struct Node));
  105. if (tmp==NULL) {
  106. return ;
  107. }
  108. tmp->data=value;
  109. if (pos==) {
  110. return ;
  111. }
  112. for (int i=; i<pos; i++) {
  113.  
  114. if ((i!=pos-)&&(head->next==NULL)) {
  115. return ;
  116. }
  117. if (i==pos-) {
  118. tmp->next=head->next;
  119. head->next=tmp;
  120. }
  121. head=head->next;
  122. }
  123. return ;
  124. }
  125.  
  126. void insertSortData(struct Node *head,int value)
  127. {
  128. struct Node *tmp=(struct Node *)malloc(sizeof(struct Node));
  129. tmp->data=value;
  130. while (head->data<tmp->data) {
  131. if (head->next->data>=tmp->data) {
  132. tmp->next=head->next;
  133. head->next=tmp;
  134. return;
  135. }
  136. if (head->next==NULL) {
  137. tmp->next=head->next;
  138. head->next=tmp;
  139. return;
  140. }
  141. head=head->next;
  142. }
  143.  
  144. }
  145.  
  146. int deleteHead(struct Node **pointhead)
  147. {
  148. int data;
  149. data=(*pointhead)->data;
  150. struct Node *p=*pointhead;
  151. *pointhead=(*pointhead)->next;
  152. free(p);
  153. p=NULL;
  154. if (*pointhead==NULL||(*pointhead)->next==NULL) {
  155. return ;
  156. }
  157. return data;
  158. }
  159.  
  160. int deleteEnd(struct Node *head)
  161. {
  162. struct Node *tmp=head;
  163. int data;
  164. if(head==NULL||head->next==NULL)
  165. return ;
  166. while (tmp->next!=NULL) {
  167. if (tmp->next->next==NULL) {
  168. data=tmp->next->data;
  169. free(tmp->next);
  170. tmp->next=NULL;
  171. break;
  172. }
  173. tmp=tmp->next;
  174. }
  175. return data;
  176. }
  177.  
  178. int main(int argc,const char *argv[])
  179. {
  180. struct Node *h = creatLinkList();
  181. printLinkList(h);
  182.  
  183. //1、返回单链表中第pos个结点中的元素,若pos超出范围,则返回0
  184. printf("%d\n",toNode(h,));
  185.  
  186. //2、把单链表中第pos个结点的值修改为x的值,若修改成功返回1,否则返回0
  187. if (changeData(h,,)) {
  188. printLinkList(h);
  189. }
  190.  
  191. //3、向单链表的表头插入一个元素
  192. insertNodeInHead(&h, );
  193. printLinkList(h);
  194.  
  195. //4、向单链表的末尾添加一个元素
  196. insertNodeInEnd(h, );
  197. printLinkList(h);
  198.  
  199. //5、向单链表中第pos个结点位置插入元素为x的结点,若插入成功返回1,否则返回0
  200. if(insertData(h, , ))
  201. printLinkList(h);
  202.  
  203. //6、向有序单链表中插入元素x结点,使得插入后仍然有序
  204. insertSortData(h, );
  205. printLinkList(h);
  206.  
  207. //7、从单链表中删除表头结点,并把该结点的值返回,若删除失败则返回0
  208. printf("%d\n",deleteHead(&h));
  209. printLinkList(h);
  210.  
  211. //8、从单链表中删除表尾结点并返回它的值,若删除失败则返回0
  212. printf("%d\n",deleteEnd(h));
  213. printLinkList(h);
  214.  
  215. return ;
  216. }

(C)单链表的更多相关文章

  1. 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法

    有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...

  2. 单链表的C++实现(采用模板类)

    采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作.  链表结构定义 定义单链表 ...

  3. Java实现单链表的各种操作

    Java实现单链表的各种操作 主要内容:1.单链表的基本操作 2.删除重复数据 3.找到倒数第k个元素   4.实现链表的反转   5.从尾到头输出链表 6.找到中间节点 7.检测链表是否有环 8.在 ...

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

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

  5. c++单链表基本功能

    head_LinkNode.h /*单链表类的头文件*/#include<assert.h>#include"compare.h"typedef int status; ...

  6. 单链表、循环链表的JS实现

    数据结构系列前言: 数据结构作为程序员的基本知识,需要我们每个人牢牢掌握.近期我也展开了对数据结构的二次学习,来弥补当年挖的坑......   当时上课的时候也就是跟着听课,没有亲自实现任何一种数据结 ...

  7. C代码实现非循环单链表

    C代码实现非循环单链表, 直接上代码. # include <stdio.h> # include <stdlib.h> # include <malloc.h> ...

  8. 分离的思想结合单链表实现级联组件:CascadeView

    本文介绍自己最近做省市级联的类似的级联功能的实现思路,为了尽可能地做到职责分离跟表现与行为分离,这个功能拆分成了2个组件并用到了单链表来实现关键的级联逻辑,下一段有演示效果的gif图.虽然这是个很常见 ...

  9. 数据结构:单链表结构字符串(python版)添加了三个新功能

    #!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...

  10. 数据结构:单链表结构字符串(python版)改进

    此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...

随机推荐

  1. Android应用开发基础篇(2)-----Notification(状态栏通知)

    一.概述      Notification这个部件的功能是在状态栏里显示消息提醒,比如有未读的短信或者是未接的电话,那么状态栏里都会有显示,更或者是从某个应用(比如QQ,酷我音乐等等)里按Home键 ...

  2. OpenCV---图片生成视频

    /** It is a batch processing interface. */ #include "stdafx.h" #include <windows.h> ...

  3. UVa340 Master-Mind Hints

    #include <stdio.h>#include <string.h> #define MIN(a,b) (((a) < (b)) ? (a) : (b)) int ...

  4. C++的常量折叠(一)

    前言 前几天女票问了我一个阿里的面试题,是有关C++语言的const常量的,其实她一提出来我就知道考察的点了:肯定是const常量的内存不是分配在read-only的存储区的,const常量的内存分配 ...

  5. 设计模式的PHP实现示例(转)

    symfony2 很多设计模式思想,下面的资料会有点帮助:http://www.open-open.com/lib/view/open1414996676559.html 阅读目录 Creationa ...

  6. Protel99 SE快捷键大全

    为了方便观看我们的protel99 se视频教程的朋友,我们在这里发布了protel99 se的所有的键盘的快捷分健大全,希望大家在学习我们的视频教程的时候,可以熟悉以下这些快捷键,因为平时我们用pr ...

  7. Android开发中怎样调用系统Email发送邮件(多种调用方式)

    在Android中调用其他程序进行相关处理,几乎都是使用的Intent,所以,Email也不例外,所谓的调用Email,只是说Email可以接收Intent并做这些事情 我们都知道,在Android中 ...

  8. 使用Win32 API创建不规则形状&带透明色的窗口

    前一阵突然想起了9月份电面某公司实习时的二面题,大概就是说怎么用Win32 API实现一个透明的窗口,估计当时我的脑残答案肯定让面试官哭笑不得吧.所以本人决定好好研究下这个问题.经过一下午的摸索,基本 ...

  9. HDU 1983 Kaitou Kid - The Phantom Thief (2)

    神题,搜索太差,来自网络的题解与程序 思路: 封锁出口或者入口周围的格子. 最多需要4个封锁点. 所以我们可以采取这样的策略: 1.寻找一条盗贼的可行路线,如果没有,返回0. 2.计算封锁出口和入口四 ...

  10. VS2010/MFC:模态对话框及其弹出过程

    模态对话框及其弹出过程 加法计算器对话框程序大家照着做一遍后,相信对基于对话框的程序有些了解了,有个好的开始对于以后的学习大有裨益.趁热打铁,这一节讲讲什么是模态对话框和非模态对话框,以及模态对话框怎 ...