头文件: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++/分文件版)的更多相关文章

  1. L脚本语言语法手冊 0.10版

    L脚本语言语法手冊 0.10版       简  介 L脚本语言是一个轻量级的,旨在接近自然语言的编程语言,眼下支持在中文.英文基础上的编程.并可扩展为随意语种.L脚本语言的语法结构简单.程序结构相对 ...

  2. c++学习书籍推荐《面向对象程序设计:C++语言描述(原书第2版)》下载

    百度云及其他网盘下载地址:点我 <面向对象程序设计:C++语言描述(原书第2版)>内容丰富,结构合理,写作风格严谨,深刻地论述了c++语言的面向对象编程的各种技术,主要内容包括:面向对象编 ...

  3. C语言链表实例--玩转链表

    下图为最一简单链表的示意图: 第 0 个结点称为头结点,它存放有第一个结点的首地址,它没有数据,只是一个指针变量.以下的每个结点都分为两个域,一个是数据域,存放各种实际的数据,如学号 num,姓名 n ...

  4. 一份关于Swift语言学习资源的整理文件

    一份关于Swift语言学习资源的整理文件     周银辉 在这里下载 https://github.com/ipader/SwiftGuide

  5. 从Discuz!NT项目文件结构看如何给系统框架分层和类库分文件夹

    以下为Discuz!NT的文件夹根目录: 类库图: 从上面两个图可以看出: 1.dnt对于类库的分层是通过名称的层级来区分的,如Discuz.Plugn和Discuz.Plugin.Spread 2. ...

  6. C语言中 *.c和*.h文件的区别!

    C语言中 *.c和*.h文件的区别!  http://blog.163.com/jiaoruijun07@126/blog/static/68943278201042064246409/        ...

  7. 百度云管家-V4.6.1-单文件版绿色版

    转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/baidu-is-clouds-butler-v4-6-1-single-file-green-edition ...

  8. VOL.2 IE6,7,8(windows vista/7 x86/x64 )单文件版三连发,欢迎大家分享

    在上期 VOL.1 利用vmware ThinApp 制作非XP下可以运行的IE6 [无插件版](windows vista/7/8  x86/x64 )中,简要介绍了如何利用vmware Thina ...

  9. C语言 链表

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

随机推荐

  1. Nginx的使用(一)Nginx+IIS实现一个网站绑定多个https域名

    使用nginx最初的目的是为了解决iis7下无法配置多个443端口的问题,iis7下不同的域名无法同时绑定443端口,据说iis8是可以的,但是iis8的话需要安装windows server2012 ...

  2. hashlib使用

    提供摘要算法:主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法 import hashlib m = hashlib.md5("ddddd".e ...

  3. python语法之函数

    函数: 将特定功能代码编写在一个函数里 便于阅读和复用 对一组表达特定功能表达式的封装 使程序模块化 python内置函数: input(),print(),eval()... 函数定义 函数语法格式 ...

  4. RSA加密及加签

    1.对方要求我们的私钥是pkcs8格式,但是实际的公钥没有用pkcs8转换之后的私钥完成,所以是可以不是pkcs8的格式的.我们加签跟格式没有关系. 2.数据格式很重要,to_mpint而非crypt ...

  5. ulimit系统资源的设定

    使用ulimit -a 可以查看系统使用的资源 core file size 设定core文件的最大值,单位为区块,如果指定为0,不会产生core文件 data seg size 设定数据段的最大值, ...

  6. 剑指Offer 17. 树的子结构 (二叉树)

    题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 题目地址 https://www.nowcoder.com/practice/6e196c44c7 ...

  7. less的入门教程

    CSS的短板 作为前端学习者的我们 或多或少都要学些 CSS ,它作为前端开发的三大基石之一,时刻引领着 Web 的发展潮向. 而 CSS 作为一门标记性语言,可能 给初学者第一印象 就是简单易懂,毫 ...

  8. _proto_ && prototype (原型 && 原型链)

    原型一直都是JavaScript基础里面的痛点,因为在JavaScript里面没有类的概念,都是通过原型对象来实现继承,下面的这个图很好的说明几者之间的关系! a.__proto__ = A.prot ...

  9. ubuntu下安装mysql和配置远程访问

    ubuntu下安装mysql和配置远程访问   下载安装mysql sudo apt-get install mysql-client mysql-server 安装的时候会提示要设置root密码,如 ...

  10. oracle存储结构

    数据库的物理存储结构 select * from v$datafile; 数据库的逻辑存储结构,从表空间开始查起一个数据库对象的逻辑存储结构如下表空间-段-区-块 select * from dba_ ...