C语言 链表(Dev C++/分文件版)
头文件:quechain.h
struct Question {
int _id;
struct Question* pre;
struct Question* next;
};
void chain_print(struct Question* qFirst);
void chain_add(struct Question* qFirst, struct Question* qAdd);
void chain_remove(struct Question* qFirst, struct Question* qRemove);
struct Question* chain_get(struct Question* qFirst, int index);
int chain_count(struct Question* qFirst);
struct Question* newQuestion(int id);
void newQuestion2(int id, struct Question** q);
源文件:quechain.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "quechain.h" void chain_print(struct Question* qFirst) {
puts("----------print------------");
struct Question* q = qFirst;
if (qFirst->next == NULL) {
puts("没有元素可以打印");
return;
} else {
q = qFirst->next;
}
// 遍历链表
for(q; (q->next) != NULL; q=q->next ) {
printf("%d\n", q->_id);
}
// 最后一项特殊对待
printf("%d\n", q->_id);
}
void chain_add(struct Question* qFirst, struct Question* qAdd) {
// 遍历链表
struct Question* q = qFirst;
for(q; (q->next) != NULL; q=q->next ) {
}
// 最后一项
q->next = qAdd;
qAdd->pre = q;
} void chain_remove(struct Question* qFirst, struct Question* qRemove) {
struct Question* qPre = NULL;
struct Question* qNext = NULL;
struct Question* q = qFirst; if (qFirst == qRemove) {
(qFirst->next)->pre = NULL;
qFirst = (qFirst->next);
free(q);
return;
} // 遍历链表 for(q; (q->next) != NULL; q=q->next ) {
if (q == qRemove) {
qPre = q->pre;
qNext = q->next;
if (qPre!=NULL) {
qPre->next= qNext;
}
if (qNext!=NULL) {
qNext->pre = qPre;
}
free(qRemove);
return;
}
}
// 最后一项
if (q == qRemove) {
qPre = q->pre;
if (qPre!=NULL) {
qPre->next= qNext;
}
free(qRemove);
}
} struct Question* chain_get(struct Question* qFirst, int index) {
int i = ;
// 遍历链表
struct Question* q = qFirst;
for(q;
(q->next) != NULL;
q=q->next,i++ ) {
if (index == i) {
return q;
}
}
if (index == i) {
// 获取最后一个元素
return q;
}
return NULL;
} int chain_count(struct Question* qFirst) {
int i = ;
// 遍历链表
struct Question* q = qFirst;
for(q;
(q->next) != NULL;
q=q->next,i++ ) {
} return i;
}
struct Question* newQuestion(int id) {
struct Question* q = (struct Question*)malloc(sizeof(struct Question));
memset(q, , sizeof(struct Question));
q->_id = id;
return q;
} void newQuestion2(int id, struct Question** q) {
*q = (struct Question*)malloc(sizeof(struct Question));
memset(*q, , sizeof(struct Question));
(*q)->_id = id;
}
测试文件:testc.c
#include <stdio.h>
#include <stdlib.h>
#include "quechain.h"
int main() {
struct Question* qFirst = NULL;
qFirst = newQuestion(); struct Question* q1 = NULL; newQuestion2(,&q1);
chain_add(qFirst, q1); struct Question* q2 = newQuestion();
chain_add(qFirst, q2); struct Question* q3 = newQuestion();
chain_add(qFirst, q3); struct Question* q4 = newQuestion();
chain_add(qFirst, q4); struct Question* q5 = newQuestion();
chain_add(qFirst, q5); chain_print(qFirst); printf("get %d\n", chain_get(qFirst, )->_id);
printf("get %d\n", chain_get(qFirst, )->_id);
printf("get %d\n", chain_get(qFirst, )->_id);
//------------------------------
chain_remove(qFirst, q3);
chain_print(qFirst);
chain_remove(qFirst, q5);
chain_print(qFirst);
chain_remove(qFirst, q1);
chain_print(qFirst); printf("元素个数: %d\n", chain_count(qFirst));
return ;
}
C语言 链表(Dev C++/分文件版)的更多相关文章
- L脚本语言语法手冊 0.10版
L脚本语言语法手冊 0.10版 简 介 L脚本语言是一个轻量级的,旨在接近自然语言的编程语言,眼下支持在中文.英文基础上的编程.并可扩展为随意语种.L脚本语言的语法结构简单.程序结构相对 ...
- c++学习书籍推荐《面向对象程序设计:C++语言描述(原书第2版)》下载
百度云及其他网盘下载地址:点我 <面向对象程序设计:C++语言描述(原书第2版)>内容丰富,结构合理,写作风格严谨,深刻地论述了c++语言的面向对象编程的各种技术,主要内容包括:面向对象编 ...
- C语言链表实例--玩转链表
下图为最一简单链表的示意图: 第 0 个结点称为头结点,它存放有第一个结点的首地址,它没有数据,只是一个指针变量.以下的每个结点都分为两个域,一个是数据域,存放各种实际的数据,如学号 num,姓名 n ...
- 一份关于Swift语言学习资源的整理文件
一份关于Swift语言学习资源的整理文件 周银辉 在这里下载 https://github.com/ipader/SwiftGuide
- 从Discuz!NT项目文件结构看如何给系统框架分层和类库分文件夹
以下为Discuz!NT的文件夹根目录: 类库图: 从上面两个图可以看出: 1.dnt对于类库的分层是通过名称的层级来区分的,如Discuz.Plugn和Discuz.Plugin.Spread 2. ...
- C语言中 *.c和*.h文件的区别!
C语言中 *.c和*.h文件的区别! http://blog.163.com/jiaoruijun07@126/blog/static/68943278201042064246409/ ...
- 百度云管家-V4.6.1-单文件版绿色版
转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/baidu-is-clouds-butler-v4-6-1-single-file-green-edition ...
- VOL.2 IE6,7,8(windows vista/7 x86/x64 )单文件版三连发,欢迎大家分享
在上期 VOL.1 利用vmware ThinApp 制作非XP下可以运行的IE6 [无插件版](windows vista/7/8 x86/x64 )中,简要介绍了如何利用vmware Thina ...
- C语言 链表
原文:C语言 链表 最近在复习数据结构,想把数据结构里面涉及的都自己实现一下,完全是用C语言实现的. 自己编写的不是很好,大家可以参考,有错误希望帮忙指正,现在正处于编写阶段,一共将要实现19个功能. ...
随机推荐
- 移动web技能总结
对于作为一名前端开发人员,除了能够编写出满足需求的PC端页面之外,同时也是需要懂得怎么去制作移动web页面,毕竟使用移动设备来操作任何处理称为新时代的趋势,所以学好制作一个移动web时必须滴.于是通过 ...
- window配置 mysql 详细步骤
1.配置环境变量:右击“我的电脑”-->"高级"-->"环境变量" 3)在(系统变量)path变量(已存在不用新建)添加变量值:G:\MySQL\m ...
- 201671010142 java类与对象的定义及使用
通过这章学习,了解到面向对象程序设计.面向对象的程序设计是有对象组成的,每个对象对用户公开的特定功能部分和隐藏的实现部分.有类构造对象的过程称为创建类的实例.实现封装的关键在于绝对不能让类中的方法直接 ...
- python基础----基础知识介绍
一 编程语言的划分 编译型:将代码一次性全部编译成二进制,然后运行. 缺点:开发效率低,不能跨平台(windows与linux) 优点:执行效率高 代表语言:c语言 解释型:当程序开始运 ...
- select 取value值
<md-select ng-model="alarmSelect" style="margin: 0px 0px 0px 0px;" ng-change= ...
- Vue - v-for 的延伸用法
1.v-for 合并标签template 一起使用 2.vue.set 1.v-for 合并标签template 一起使用 之前在设计table的时候,如果使用v-for ,会直接放在tr里面,一次产 ...
- sed语法2
sed命令是一个面向字符流的非交互式编辑器,也就是说sed不允许用户与它进行交互操作.sed是按行来处理文本内容的.在shell中,使用sed来批量修改文本内容是非常方便的. sed命令的选项 sed ...
- Qthread的使用方法
1:重载 run()函数 2:将对象移到Qthread对象中 Movetothread 该方法必须通过信号 -槽来激发.
- 生产redis client 链接报:ERR max number of clients reached 含义: 达到最大客户端数错误
1.通过netstat 命令查看TCP又11822个连接 (netstat命令是一个监控TCP/IP网络的非常有用的工具) 2.默认redis最大的连接数10000 ,但是此时无法连接redis客户 ...
- extjs_10_自己定义combotree组件
1.项目截图 2.treedata.json { text : "root", expanded : true, expandable : true, children : [{ ...