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个功能. ...
随机推荐
- CSS布局-body高度不等于页面高度
记录采坑: 博客记录问题,用作回忆用,不喜勿喷! html,body{width: 100%; height: 100%}这是初始定义的宽高.在布局越写越复杂的时候,布局很容易出现问题,例如一个页面中 ...
- java通过StringToKenizer获取字符串中的单词根据空格分离-简写版
public class StringToKenizer { public static void main(String[] args) { String strin = "Hello J ...
- Java库中的集合
集合类型 描述 ArrayList 一种可以动态增长和缩减的索引序列 LinkedList 一种可以在任何位置进行高效的插入和删除操作的有序序列 ArrayDeque 一种用循环数组实现的双端队列 H ...
- VSTO:使用C#开发Excel、Word【16】
使用工作表对象Worksheet对象表示Excel工作簿中的工作表.Worksheet有一个Name属性,返回工作表的名称(例如“Sheet1”). 工作表管理Worksheet对象具有一个Index ...
- cocos2d-x学习笔记(斗地主代码)
满足百度百科上的出牌规则,电脑可以随着玩家出牌. 百度网盘地址:链接: https://pan.baidu.com/s/1eRLpvJ8 提取密码: tf8w
- xpath提取到的中文乱码时的解决办法
βҳ转换为正常的中文:
- Front-end: Using blurred backgrounds with contents unaffected.
Purpose: Using a picture as the background of a page with blurred effect, while the content not blur ...
- 排序算法练习--JAVA(:内部排序:插入、选择、冒泡、快速排序)
排序算法是数据结构中的经典算法知识点,也是笔试面试中经常考察的问题,平常学的不扎实笔试时候容易出洋相,回来恶补,尤其是碰到递归很可能被问到怎么用非递归实现... 内部排序: 插入排序:直接插入排序 选 ...
- devops的理解
DevOps(Development和Operations的组合词)是一种重视"软件开发人员(Dev)"和"IT运维技术人员(Ops)" DevOps是一组过程 ...
- CSV表格融合
常用表格融合函数 1 merge() 用于融合的函数 https://blog.csdn.net/brucewong0516/article/details/82707492 pd.merge(lef ...