1. // ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. struct Question{
  9. int _id;
  10. struct Question* pre;
  11. struct Question* next;
  12. };
  13. void chain_print(struct Question* qFirst){
  14. puts("----------print------------");
  15. struct Question* q = qFirst;
  16. if (qFirst->next == NULL){
  17. puts("没有元素可以打印");
  18. return;
  19. }else{
  20. q = qFirst->next;
  21. }
  22. // 遍历链表
  23. for(q; (q->next) != NULL;q=q->next ) {
  24. printf("%d\n", q->_id);
  25. }
  26. // 最后一项特殊对待
  27. printf("%d\n", q->_id);
  28. }
  29. void chain_add(struct Question* qFirst, struct Question* qAdd){
  30. // 遍历链表
  31. struct Question* q = qFirst;
  32. for(q; (q->next) != NULL;q=q->next ) {
  33. }
  34. // 最后一项
  35. q->next = qAdd;
  36. qAdd->pre = q;
  37. }
  38.  
  39. void chain_remove(struct Question* qFirst, struct Question* qRemove){
  40. struct Question* qPre = NULL;
  41. struct Question* qNext = NULL;
  42. struct Question* q = qFirst;
  43.  
  44. if (qFirst == qRemove){
  45. (qFirst->next)->pre = NULL;
  46. qFirst = (qFirst->next);
  47. free(q);
  48. return;
  49. }
  50.  
  51. // 遍历链表
  52.  
  53. for(q; (q->next) != NULL;q=q->next ) {
  54. if (q == qRemove){
  55. qPre = q->pre;
  56. qNext = q->next;
  57. if (qPre!=NULL){
  58. qPre->next= qNext;
  59. }
  60. if (qNext!=NULL){
  61. qNext->pre = qPre;
  62. }
  63. free(qRemove);
  64. return;
  65. }
  66. }
  67. // 最后一项
  68. if (q == qRemove){
  69. qPre = q->pre;
  70. if (qPre!=NULL){
  71. qPre->next= qNext;
  72. }
  73. free(qRemove);
  74. }
  75. }
  76.  
  77. struct Question* chain_get(struct Question* qFirst, int index){
  78. int i = ;
  79. // 遍历链表
  80. struct Question* q = qFirst;
  81. for(q;
  82. (q->next) != NULL;
  83. q=q->next,i++ ) {
  84. if (index == i){
  85. return q;
  86. }
  87. }
  88. if (index == i){
  89. // 获取最后一个元素
  90. return q;
  91. }
  92. return NULL;
  93. }
  94.  
  95. int chain_count(struct Question* qFirst){
  96. int i = ;
  97. // 遍历链表
  98. struct Question* q = qFirst;
  99. for(q;
  100. (q->next) != NULL;
  101. q=q->next,i++ ) {
  102. }
  103.  
  104. return i;
  105. }
  106. struct Question* newQuestion(int id){
  107. struct Question* q = (struct Question*)malloc(sizeof(struct Question));
  108. memset(q, , sizeof(struct Question));
  109. q->_id = id;
  110. return q;
  111. }
  112.  
  113. void newQuestion(int id, struct Question** q){
  114. *q = (struct Question*)malloc(sizeof(struct Question));
  115. memset(*q, , sizeof(struct Question));
  116. (*q)->_id = id;
  117. }
  118.  
  119. int _tmain(int argc, _TCHAR* argv[])
  120. {
  121. struct Question* qFirst = NULL;
  122. qFirst = newQuestion();
  123.  
  124. struct Question* q1 = NULL;
  125.  
  126. newQuestion(,&q1);
  127. chain_add(qFirst, q1);
  128.  
  129. struct Question* q2 = newQuestion();
  130. chain_add(qFirst, q2);
  131.  
  132. struct Question* q3 = newQuestion();
  133. chain_add(qFirst, q3);
  134.  
  135. struct Question* q4 = newQuestion();
  136. chain_add(qFirst, q4);
  137.  
  138. struct Question* q5 = newQuestion();
  139. chain_add(qFirst, q5);
  140.  
  141. chain_print(qFirst);
  142.  
  143. printf("get %d\n", chain_get(qFirst, )->_id);
  144. printf("get %d\n", chain_get(qFirst, )->_id);
  145. printf("get %d\n", chain_get(qFirst, )->_id);
  146. //------------------------------
  147. chain_remove(qFirst, q3);
  148. chain_print(qFirst);
  149. chain_remove(qFirst, q5);
  150. chain_print(qFirst);
  151. chain_remove(qFirst, q1);
  152. chain_print(qFirst);
  153.  
  154. printf("元素个数: %d\n", chain_count(qFirst));
  155. return ;
  156. }
  1. ----------print------------
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. get 1
  8. get 3
  9. get 5
  10. ----------print------------
  11. 1
  12. 2
  13. 4
  14. 5
  15. ----------print------------
  16. 1
  17. 2
  18. 4
  19. ----------print------------
  20. 2
  21. 4
  22. 元素个数: 2

C语言 链表(VS2012版)的更多相关文章

  1. C语言实现单链表-03版

    在C语言实现单链表-02版中我们只是简单的更新一下链表的组织方式: 它没有更多的更新功能,因此我们这个版本将要完成如下功能: Problem 1,搜索相关节点: 2,前插节点: 3,后追加节点: 4, ...

  2. C语言实现单链表-02版

    我们在C语言实现单链表-01版中实现的链表非常简单: 但是它对于理解单链表是非常有帮助的,至少我就是这样认为的: 简单的不能再简单的东西没那么实用,所以我们接下来要大规模的修改啦: Problem 1 ...

  3. 狗屁不通的“视频专辑:零基础学习C语言(小甲鱼版)”(2)

    前文链接:狗屁不通的“视频专辑:零基础学习C语言(小甲鱼版)”(1) 小甲鱼在很多情况下是跟着谭浩强鹦鹉学舌,所以谭浩强书中的很多错误他又重复了一次.这样,加上他自己的错误,错谬之处难以胜数. 由于拙 ...

  4. C语言 链表

    原文:C语言 链表 最近在复习数据结构,想把数据结构里面涉及的都自己实现一下,完全是用C语言实现的. 自己编写的不是很好,大家可以参考,有错误希望帮忙指正,现在正处于编写阶段,一共将要实现19个功能. ...

  5. C语言链表操作模板(添加,删除,遍历,排序)

    C语言链表操作模板,摘自郝斌的C语言视频教程,简单的修改成了纯C格式.当年照着视频学习的时候记录下来的,在使用的时候直接拿来修改修改修改能节约不少时间的. /********************* ...

  6. 《OpenCV3 计算机视觉--Python语言实现 第二版》源代码及纠错

    1.源代码下载地址 <OpenCV3 计算机视觉--Python语言实现 第二版>由我们翻译,英文书名<Learning OpenCV3 Computer Vision with P ...

  7. 俄罗斯方块-C语言-详注版

    代码地址如下:http://www.demodashi.com/demo/14818.html 俄罗斯方块-C语言-详注版 概述 本文详述了C语言版俄罗斯方块游戏的原理以及实现方法,对游戏代码进行了详 ...

  8. ZT C语言链表操作(新增单向链表的逆序建立)

    这个不好懂,不如看 转贴:C语言链表基本操作http://www.cnblogs.com/jeanschen/p/3542668.html ZT 链表逆序http://www.cnblogs.com/ ...

  9. 坦克大战-C语言-详注版

    代码地址如下:http://www.demodashi.com/demo/14259.html 坦克大战-C语言-详注版 概述 本文详述了C语言版坦克大战游戏的原理以及实现方法,对游戏代码进行了详细的 ...

  10. C语言链表结构体(学习笔记)

    #include <stdio.h> #define LENTEST 100 // 采取逐步删除的方法求的素数 //先假设1-100都是素数,然后剔除2的倍数, //3的倍数,直到剔除所有 ...

随机推荐

  1. 正确学习Linux系统的5个建议

    摘要: 最近几年Linux系统应用越来越广泛,以至于很多人开始热衷学习Linux.但是我们都是从小都是学习windows系统长大的,从windows 98到现在的windows 10,而根据学习win ...

  2. 2.7 清除FTP服务器文件

    清除服务器文件 from ftptools import FtpTools class CleanAll(FtpTools): '''delete an entire remote tree of s ...

  3. javascrit--常用互动方法

    本文是刚开始学习javascript的一些基础知识 JavaScript--互动 JavaScript输出内容 document.write("内容"+"<br&g ...

  4. 博弈论:寻找先手必胜策略——Grundy值

    选修了人工智能课程,老师布置了调研任务:Grundy,开始看了一些资料并没有看懂. 后来找到了一篇文,写的很棒,里面有好多博弈相关的问题与分析,分享出来给大家: http://endless.logd ...

  5. linux系统转换root权限

    有时候我们用普通用户的权限没办法完成有关权限,这时候我们就需要拿到root权限才可以,拿到root权限有两种方式 方式一: su - 或者su 此时就会提示你输入密码,输入密码成功以后就能以root权 ...

  6. usermod语法

    语法 usermod [-LU][-c <备注>][-d <登入目录>][-e <有效期限>][-f <缓冲天数>][-g <群组>][-G ...

  7. Java实现数据库与eclipse的连接

    JavaBean:用于传递数据,拥有与数据相关的逻辑处理 JSP:从Model接收数据并生成HTML Servlet:接收HTTP请求并控制Model和View jdbc:用于驱动连接 一.[建立数据 ...

  8. 5.移动终端App测试点归纳

    以下所有测试最后必须在真机上完整的执行. 1 安装.卸载测试 1.1 在真机上.第三方软件(xy苹果助手.91.安卓助手)的安装与卸载 1.2 安装在手机卡上 或 SD卡上 (不同的IOS和安卓版本) ...

  9. Ubuntu16.04上添加用户以及修改用户所属的组

    我的问题是这样的,我的本地的电脑上有一个用户以及一个用户组,我还想添加其他的用户,并且这个用户属于这个已有的用户组 <鸟哥的linux私房菜>针对的是centos系统,还是有一些不一样 实 ...

  10. PyCharm:ModuleNotFoundError: No module named 'selenium'

    Mac安装PyCharm后,将已有工程导入,之前使用Mac终端执行脚本时正常,现在报错ModuleNotFoundError: No module named 'selenium',解决方法是在PyC ...