头文件: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. JJTree Tutorial for Advanced Java Parsing

    The Problem JJTree is a part of JavaCC is a parser/scanner generator for Java. JJTree is a preproces ...

  2. Electron "jQuery/$ is not defined" 解决方法

    参考问题:https://stackoverflow.com/questions/32621988/electron-jquery-is-not-defined <!-- Insert this ...

  3. vue轮播图实现

    1.先安装组件 cnpm install vue-awesome-swiper; 2.在main.js下引入文件: import VueAwsomeSwiper from 'vue-awesome-s ...

  4. MCNN多层神经网络论文笔记

    论文原文 https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Zhang_Single-Image_Crowd_Coun ...

  5. བྱ་དེ་ཁྲུང་ཁྲུང་དཀར་པོ།།--洁白的仙鹤/仓央嘉措情歌--IPA--藏语

    <<洁白的仙鹤>>, 也就是大家熟悉的仓央嘉措情歌之一.

  6. C++ 抽象类与接口

    1. 抽象类  在面向对象编程中,抽象类是一种只能定义类型,不能生成对象的类,它是对一系列看上去不同,但是本质相同的具体概念的抽象.最典型的的抽象类就是”图形”,三角形.矩形.梯形都是图形,它们都具有 ...

  7. kolla-ansible配置cinder 的ceph的多种后端存储池【转】

    在实际的使用中,openstack后端采用ceph存储,存储磁盘类型有SATA和SSD,这里如果想基于存储驱动器提供多种云硬盘类型,可以基于下面的配置完成 点击查看配置详情

  8. componentWillMount和componentDidMount的区别

    1.componentWillMount  将要装载,在render之前调用: componentDidMount,(装载完成),在render之后调用 2.componentWillMount  每 ...

  9. 利用教育邮箱注册JetBrains产品(pycharm、idea等)的方法

    转载:http://www.cnblogs.com/wang-meng/p/8887436.html 1,申请邮箱   地址为:http://mdu.edu.rs/  邮箱的前缀可以改成自己喜欢的字符 ...

  10. WEBBASE篇: 第九篇, JavaScript知识4

    JavaScript 4 练习1 <!doctype html> <html lang="en"> <head> <meta charse ...