头文件: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. 聚合函数count()

    2018-08-12

  2. Android7.0手机程序保活(附源码下载)

    项目地址,求starhttps://github.com/979451341/AppAlive 本人在学习音视频时无意发现的黑科技,发现在使用AudioTrack播放音乐时,使用手机的一键清理发现程序 ...

  3. 为什么企业需要IT资产管理

    为什么企业需要IT资产管理 为什么企业需要IT资产管理?其实这个问题,如同企业为什么要管理一样重要. 企业需要管理.因为有了管理,人.物可尽其用,在不同的位置.角色上发挥作用,然后按照一定的运维规则, ...

  4. select 取value值

    <md-select ng-model="alarmSelect" style="margin: 0px 0px 0px 0px;" ng-change= ...

  5. css.1

    background-repeat背景平铺background-x水平平铺 background-y垂直平铺 background:url()no-repeat;不平铺 background-size ...

  6. Python基础01_介绍_HelloWorld

    首先,当然是python的版本了,老师讲课学习都是以python3为主, 我的centos中还是python2.6.6 然后参照老师的教程升级到了2.7.14 谢谢! 平时主要练习3的,有时间的情况下 ...

  7. nginx 504 Gateway Time-out

    #设定http服务器 http { include mime.types; #文件扩展名与文件类型映射表 default_type application/octet-stream; #默认文件类型 ...

  8. C# 引用类型公共变量的影响

    public int[] a =new int[2]; private void button1_Click(object sender, EventArgs e) { bing(a); } priv ...

  9. 入门项目 A5-3 interface-user 第三方接口3

    ''' 用户接口层 ''' # 导入数据库包下面的处理数据模,为了使用其内部名称空间 from db import db_handler # 注册接口函数,接收名字与密码两个参数 def regist ...

  10. Flex布局-容器的属性

    本文部分内容参考阮一峰大神博客,原文地址:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html Flex布局即弹性布局,使用起来十分方便灵活 ...