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的倍数,直到剔除所有 ...
随机推荐
- 正确学习Linux系统的5个建议
摘要: 最近几年Linux系统应用越来越广泛,以至于很多人开始热衷学习Linux.但是我们都是从小都是学习windows系统长大的,从windows 98到现在的windows 10,而根据学习win ...
- 2.7 清除FTP服务器文件
清除服务器文件 from ftptools import FtpTools class CleanAll(FtpTools): '''delete an entire remote tree of s ...
- javascrit--常用互动方法
本文是刚开始学习javascript的一些基础知识 JavaScript--互动 JavaScript输出内容 document.write("内容"+"<br&g ...
- 博弈论:寻找先手必胜策略——Grundy值
选修了人工智能课程,老师布置了调研任务:Grundy,开始看了一些资料并没有看懂. 后来找到了一篇文,写的很棒,里面有好多博弈相关的问题与分析,分享出来给大家: http://endless.logd ...
- linux系统转换root权限
有时候我们用普通用户的权限没办法完成有关权限,这时候我们就需要拿到root权限才可以,拿到root权限有两种方式 方式一: su - 或者su 此时就会提示你输入密码,输入密码成功以后就能以root权 ...
- usermod语法
语法 usermod [-LU][-c <备注>][-d <登入目录>][-e <有效期限>][-f <缓冲天数>][-g <群组>][-G ...
- Java实现数据库与eclipse的连接
JavaBean:用于传递数据,拥有与数据相关的逻辑处理 JSP:从Model接收数据并生成HTML Servlet:接收HTTP请求并控制Model和View jdbc:用于驱动连接 一.[建立数据 ...
- 5.移动终端App测试点归纳
以下所有测试最后必须在真机上完整的执行. 1 安装.卸载测试 1.1 在真机上.第三方软件(xy苹果助手.91.安卓助手)的安装与卸载 1.2 安装在手机卡上 或 SD卡上 (不同的IOS和安卓版本) ...
- Ubuntu16.04上添加用户以及修改用户所属的组
我的问题是这样的,我的本地的电脑上有一个用户以及一个用户组,我还想添加其他的用户,并且这个用户属于这个已有的用户组 <鸟哥的linux私房菜>针对的是centos系统,还是有一些不一样 实 ...
- PyCharm:ModuleNotFoundError: No module named 'selenium'
Mac安装PyCharm后,将已有工程导入,之前使用Mac终端执行脚本时正常,现在报错ModuleNotFoundError: No module named 'selenium',解决方法是在PyC ...