C语言 链表(VS2012版)
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Question{
int _id;
struct Question* pre;
struct Question* next;
};
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 newQuestion(int id, struct Question** q){
*q = (struct Question*)malloc(sizeof(struct Question));
memset(*q, , sizeof(struct Question));
(*q)->_id = id;
} int _tmain(int argc, _TCHAR* argv[])
{
struct Question* qFirst = NULL;
qFirst = newQuestion(); struct Question* q1 = NULL; newQuestion(,&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 ;
}
----------print------------
1
2
3
4
5
get 1
get 3
get 5
----------print------------
1
2
4
5
----------print------------
1
2
4
----------print------------
2
4
元素个数: 2
C语言 链表(VS2012版)的更多相关文章
- C语言实现单链表-03版
在C语言实现单链表-02版中我们只是简单的更新一下链表的组织方式: 它没有更多的更新功能,因此我们这个版本将要完成如下功能: Problem 1,搜索相关节点: 2,前插节点: 3,后追加节点: 4, ...
- C语言实现单链表-02版
我们在C语言实现单链表-01版中实现的链表非常简单: 但是它对于理解单链表是非常有帮助的,至少我就是这样认为的: 简单的不能再简单的东西没那么实用,所以我们接下来要大规模的修改啦: Problem 1 ...
- 狗屁不通的“视频专辑:零基础学习C语言(小甲鱼版)”(2)
前文链接:狗屁不通的“视频专辑:零基础学习C语言(小甲鱼版)”(1) 小甲鱼在很多情况下是跟着谭浩强鹦鹉学舌,所以谭浩强书中的很多错误他又重复了一次.这样,加上他自己的错误,错谬之处难以胜数. 由于拙 ...
- C语言 链表
原文:C语言 链表 最近在复习数据结构,想把数据结构里面涉及的都自己实现一下,完全是用C语言实现的. 自己编写的不是很好,大家可以参考,有错误希望帮忙指正,现在正处于编写阶段,一共将要实现19个功能. ...
- C语言链表操作模板(添加,删除,遍历,排序)
C语言链表操作模板,摘自郝斌的C语言视频教程,简单的修改成了纯C格式.当年照着视频学习的时候记录下来的,在使用的时候直接拿来修改修改修改能节约不少时间的. /********************* ...
- 《OpenCV3 计算机视觉--Python语言实现 第二版》源代码及纠错
1.源代码下载地址 <OpenCV3 计算机视觉--Python语言实现 第二版>由我们翻译,英文书名<Learning OpenCV3 Computer Vision with P ...
- 俄罗斯方块-C语言-详注版
代码地址如下:http://www.demodashi.com/demo/14818.html 俄罗斯方块-C语言-详注版 概述 本文详述了C语言版俄罗斯方块游戏的原理以及实现方法,对游戏代码进行了详 ...
- ZT C语言链表操作(新增单向链表的逆序建立)
这个不好懂,不如看 转贴:C语言链表基本操作http://www.cnblogs.com/jeanschen/p/3542668.html ZT 链表逆序http://www.cnblogs.com/ ...
- 坦克大战-C语言-详注版
代码地址如下:http://www.demodashi.com/demo/14259.html 坦克大战-C语言-详注版 概述 本文详述了C语言版坦克大战游戏的原理以及实现方法,对游戏代码进行了详细的 ...
- C语言链表结构体(学习笔记)
#include <stdio.h> #define LENTEST 100 // 采取逐步删除的方法求的素数 //先假设1-100都是素数,然后剔除2的倍数, //3的倍数,直到剔除所有 ...
随机推荐
- Python查看与安装
官网下载最新的版本 https://www.python.org mac系统,最近版本的os系统默认自带python 2.7,可以通过在终端输入python或python -V zhanyunjiu ...
- python之路---面向对象编程(一)
一.设计思想的发展 面向机器(代码复杂,效率低,学习成本高,开发周期长)-------->面向过程(扩展性差,不适用多变的需求改变)----------->面向对象(扩展性好,但是可控性差 ...
- guava-retrying 源码解析(停止策略详解)
一.停止策略相关类 1.停止策略接口:StopStrategy接口,只有一个抽象方法 // 是否应该停止重试.不同的停止策略有不同的实现.boolean shouldStop(Attempt fail ...
- 【原创】MIPS相关
MIPS是单字长定点指令平均执行速度 Million Instructions Per Second的缩写. 路由器等嵌入式系统多采用MIPS和ARM两种指令架构,最近在研究路由器,借机总结一下基于M ...
- 过滤函数 filter
filter(lambda x:x.endswith('居'),house_type_list) 过滤函数,作用就是将“以‘居’结尾的字段都过滤出来,其它的字段都删除掉.”
- [BUG]读配置文件中文, 查询不到数据库
配置文件编码, 要和数据库编码一致
- 无法新建EXCLE
Regedit 进入注册表,找到HKEY_CLASSES_ROOT>.xls和.xlsx的ShellNew的值设置为:C:\Windows\ShellNew\EXCEL12.XLSX
- Linux 堆溢出原理分析
堆溢出与堆的内存布局有关,要搞明白堆溢出,首先要清楚的是malloc()分配的堆内存布局是什么样子,free()操作后又变成什么样子. 解决第一个问题:通过malloc()分配的堆内存,如何布局? 上 ...
- WEBBASE篇: 第九篇, JavaScript知识4
JavaScript 4 练习1 <!doctype html> <html lang="en"> <head> <meta charse ...
- oracle 简单crud
-- 商品表 -- CREATE TABLE "SCOTT"."GOODS" ( "id" NUMBER NOT NULL, "n ...