---------------------------------------------------------------------------------------

可变数组:

array.h

#ifndef _ARRAY_H_
#define _ARRAY_H_ typedef struct {
int *array;
int size;
} Array; // Array不定义成指针类型 *Array 的原因:定义成变量使用范围更广,如果定义一个指针类型,那么 array p 其实不容易看出是指针 // 函数原型
Array array_create(int init_size);
void array_free(Array *a);
int array_size(const Array *a);
int* array_at(Array *a, int index);
void array_set(Array *a, int index, int value);
int array_get(const Array *a, int index);
void array_inflate(Array *a, int more_size); #endif
//  main.c
// Created by weichen on 15/7/7.
// Copyright (c) 2015年 weichen. All rights reserved. #include "array.h"
#include <stdio.h>
#include <stdlib.h> const int BLOCK_SIZE = ; Array array_create(int init_size)
{
Array a;
a.size = init_size;
a.array = (int*)malloc(sizeof(int) * a.size);
return a; // 返回变量本身
} void array_free(Array *a)
{
free(a->array);
a->array = NULL;
a->size = ;
} // 封装
int array_size(const Array *a)
{
return a->size;
} // 返回指针
int* array_at(Array *a, int index)
{
if ( index >= a->size) {
// index位于哪个block,加1后乘以block,得到下一个block,最后减去原来的size
array_inflate(a, (index/BLOCK_SIZE+)*BLOCK_SIZE - a->size);
}
return &(a->array[index]);
} void array_set(Array *a, int index, int value)
{
a->array[index] = value;
} int array_get(const Array *a, int index)
{
return a->array[index];
} void array_inflate(Array *a, int more_size)
{
// 重新分配一块内存空间
int *p = (int*)malloc(sizeof(int) * (a->size + more_size));
int i;
// 复制原数组到新空间
for (i=; i>a->size; a++) {
p[i] = a->array[i];
}
// 释放内存空间
free(a->array);
// 赋值到a
a->array = p;
a->size += more_size;
} int main(int argc, const char * argv[]) {
/*
可变数组(Resizable Array) Think about a set of functions that provide a mechanism of resizable array of int.
Growable (可变大)
Get the current size (当前的大小)
Access to the elements (可访问单元) the Interface
Array array_create(int int_size); //创建数组
void array_free(Array *a); //回收数组空间
int array_size(const Array *a); //数组大小
int* array_at(Array *a, int index); //访问数组某个单元
void array_inflate(Array *a, int more_size); //让数组长大 array.h #ifndef _ARRAY_H_
#define _ARRAY_H_ typeof struct {
int *array;
int size;
} Array; // Array 不定义成指针类型 *Array 的原因:定义成变量使用范围更广,如果定义一个指针类型,那么 array p 其实不容易看出是指针 Array array_create(int init_size);
void array_free(Array *a);
int array_size(const Array *a);
int* array_at(Array *a, int index);
void array_inflate(Array *a, int more_size); #endif */ Array a = array_create();
//printf("%d\n", a.size);
//printf("%d\n", array_size(&a)); // 如果版本升级,直接用a.size不容易更改,封装保护具体实现细节 //*array_at(&a, 0) = 10; //函数调用返回指针,加*号指向指针所指的东西,变量可以赋值
//printf("%d\n", *array_at(&a, 0)); //上面写法的转换
array_set(&a, , );
array_set(&a, , );
printf("%d\n", array_get(&a, )); //10 // 可变数组自动增长
int number = ;
int cnt = ;
while(number != -) {
scanf("%d", &number); // 随便输入数字,循环输出数组a的值,-1停止
if(number != -) {
number = *array_at(&a, cnt++);
printf("%d\n", number);
}
} array_free(&a); return ;
}

链表操作:

node.h

#ifndef _NODE_H_
#define _NODE_H_ typedef struct _node {
int value; // 数据
struct _node *next; // 指针,下一个指向它自己(不能写成 Node *next,因为在这之后才定义的Node)
} Node; // 定义Node类型 #endif
//  main.c
// Created by weichen on 15/7/8.
// Copyright (c) 2015年 weichen. All rights reserved. #include "node.h"
#include <stdio.h>
#include <stdlib.h> //typedef struct _node {
// int value;
// struct _node *next;
//} Node; int main(int argc, const char * argv[]) {
/*
链表 可变数组的缺陷:
拷贝需要花时间,如果原数组很大,会很慢
由于新数组需要申请更大的空间,由于之前还没free掉,总有一个时间点会出现尽管总内存够但无法再申请足够使用的情况
所以可变数组不够高效 解决办法:
linked blocks,no copy
就只申请block那么大的内存,与原来的内存空间链起来 理想中的效果:
|-------------|
| 内存1 |---+
|-------------| |
+--------------------+
| |-------------|
+->| block |---+
|-------------| |
+--------------------+
| |-------------|
+->| block |
|-------------| 实际中:
head
|
|-------------| |--------------| |-------------|
| 数据 | point |--->| 数据 | point |--->| 数据 | point |
|-------------| |--------------| |---------|---|
-
整个结构叫做链表,其中带有数据的叫做结点 让last一开始等于第一个的数据,看next有没有东西,让last所指那个东西的下一个,于是last指向了第二个的数据
*/ Node *head = NULL;
int number;
do {
scanf("%d\n", &number);
if ( number != - ) {
// add to linked-list
Node *p = (Node*)malloc(sizeof(Node)); // 为结构分配一块内存空间
p->value = number; // 初始化值为number
p->next = NULL; // 初始化第一个的next为null,即下一个为null
// find the last
Node *last = head; // 初始化:最后一个就是当前的这个
if ( last ) { // 如果last不为null了
while ( last->next ) { // 如果最后一个还有next的话,last就是last的next
last = last->next; // 当循环停止时,last所指的就是最后一个
}
// attach
last->next = p;
} else {
head = p; // 只有第一个,head为p
}
}
} while ( number != - ); return ;
}

 改进:

//  main.c

#include "node.h"
#include <stdio.h>
#include <stdlib.h> // 单独定义一个数据类型表示list
typedef struct _list {
Node* head;
} List; void add(List* list, int number); int main(int argc, const char * argv[]) { List list;
int number;
list.head = NULL; do {
scanf("%d\n", &number); if (number != -) {
       add(&list, number);
}
} while(number != -); return ;
} // 添加函数独立出来
void add(List* pList, int number)
{
Node *p = (Node*)malloc(sizeof(Node)); p->value = number;
p->next = NULL; Node *last = pList->head;
if (last) {
while (last->next) {
last = last->next;
}
last->next = p;
} else {
pList->head = p;
}
}

搜索

删除

清除

Link:http://www.cnblogs.com/farwish/p/4628952.html

[C语言]进阶|链表的更多相关文章

  1. C语言之链表

    这两天在复习C语言的知识,为了给下个阶段学习OC做准备,以下的代码的编译运行环境是Xcode5.0版本,写篇博文把昨天复习的C语言有关链表的知识给大家分享一下,以下是小菜自己总结的内容,代码也是按照自 ...

  2. C语言习题 链表建立,插入,删除,输出

    Problem B: C语言习题 链表建立,插入,删除,输出 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 222  Solved: 92 [Subm ...

  3. 【R笔记】R语言进阶之4:数据整形(reshape)

    R语言进阶之4:数据整形(reshape) 2013-05-31 10:15 xxx 网易博客 字号:T | T 从不同途径得到的数据的组织方式是多种多样的,很多数据都要经过整理才能进行有效的分析,数 ...

  4. YTU 2430: C语言习题 链表建立,插入,删除,输出

    2430: C语言习题 链表建立,插入,删除,输出 时间限制: 1 Sec  内存限制: 128 MB 提交: 576  解决: 280 题目描述 编写一个函数creatlink,用来建立一个动态链表 ...

  5. 《C语言进阶剖析》课程目录

    <C语言进阶剖析>学习笔记                                                         本文总结自狄泰软件学院唐佐林老师的<C语言 ...

  6. 关于c语言单项链表尾添加

    犹豫了几天,看了很多大牛写的关于c语言链表,感触很多,终于下定决心,把自己对于链表的理解随之附上,可用与否,自行裁夺.由于作者水平有限也是第一次写,不足之处,竭诚希望得到各位大神的批评指正.制作不易, ...

  7. Go语言核心36讲(Go语言进阶技术二)--学习笔记

    08 | container包中的那些容器 我们在上次讨论了数组和切片,当我们提到数组的时候,往往会想起链表.那么 Go 语言的链表是什么样的呢? Go 语言的链表实现在标准库的container/l ...

  8. 彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-项目入口与路由EP01

    书接上回,我们已经安装好Iris框架,并且构建好了Iris项目,同时配置了fresh自动监控项目的实时编译,万事俱备,只欠东风,彩虹女神蓄势待发.现在我们来看看Iris的基础功能,如何编写项目入口文件 ...

  9. c语言进阶14-线性表之链表

    一.  线性表的链式存储结构 1.        顺序存储结构不足的解决办法 前面我们讲的线性表的顺序存储结构.它是有缺点的,最大的缺点就是插入和删除时需要移动大量元素,这显然就需要耗费时间.能不能想 ...

随机推荐

  1. mysql中外键的创建与删除

    外键的创建 方法1:创建表的时候设置(外键名随机生成) 1.前提条件,必须要有一个主表,这里设为persons 2.主表中必须设置主键字段primary key,这里设为id_p //创建数据库dat ...

  2. 创建一个vue 项目 必备的几个插件

    第一步npm安装 首先:先从nodejs.org中下载nodejs 打开控制命令行程序(CMD),node -v 检查是否正常 使用淘宝NPM 镜像 npm  install  -g  cnpm  - ...

  3. single-cell RNA-seq 工具大全

    [怪毛匠子-整理] awesome-single-cell List of software packages (and the people developing these methods) fo ...

  4. MYSQL数据模型

    DROP TABLE IF EXISTS `sh_category`; CREATE TABLE `sh_category` ( `id` int(11) NOT NULL AUTO_INCREMEN ...

  5. 在Struts.xml中的result元素指的是:指定动作类的动作方法执行完后的结果视图.

    result结果集 上一篇文章主要讲Struts2框架(4)---Action类访问servlet这篇主要讲result结果集 在Struts.xml中的result元素指的是:指定动作类的动作方法执 ...

  6. sklearn.preprocessing.LabelBinarizer

    sklearn.preprocessing.LabelBinarizer

  7. scanf连续输入字符,中间不要忘记\n

    #include <stdio.h> int main(void) { int precipitating; int temperature; printf("\nInput p ...

  8. linux日常命令之三

    一.换行符 linux换行符为\n,而windows换行符为\r\n. 因此,linux的原生文本文件,换行符为\n,而windows为\r\n:将linux文件拷贝至windows,换行符保持不变, ...

  9. git创建分支并上传仓库

    1. 新建分支 xxx 2.  git pull (目录下 命令行将线上分支拉倒本地) 3. git checkout xxx (切换到到该分支 ) (可使用 git status 查看目前处于哪一个 ...

  10. day01计算机基础

    今日内容 1.计算机初步认识 1.计算机认识 1. 计算机基础 1.1硬件:cpu/内存/硬盘/主板/网卡 1.2操作系统 linux:免费开源 windows mac 1.3解释器/编译器 补充:编 ...