HeadFirstC笔记_6 数据结构与动态存储:牵线搭桥
保存可变数量的数据
typedef struct island { // 你必须为这个结构命名。
char *name;
char *opens;
char *closes;
struct island *next; // 你在结构中保存了一个指针,指向下一座岛。
} island;
island amity = {"Amity", "09:00", "17:00", NULL};
island craggy = {"Craggy", "09:00", "17:00", NULL};
island isla_nublar = {"Isla Nublar", "09:00", "17:00", NULL};
island shutter = {"Shutter", "09:00", "17:00", NULL};
amity.next = &craggy;
craggy.next = &isla_nublar;
isla_nublar.next = &shutter;
island skull = {"Skull", "09:00", "17:00", NULL};
isla_nublar.next = &skull;
skull.next = &shutter;
#include <stdio.h>
typedef struct island { // 你必须为这个结构命名。
char *name;
char *opens;
char *closes;
struct island *next; // 你在结构中保存了一个指针,指向下一座岛。
} island;
void display(island *start) {
island *i = start;
for (; i!=NULL ; i = i->next ) { //需要一直循环下去,直到当前island没有next值,在每次循环的最后,跳到下一座岛
printf("Name: %s\n open: %s-%s\n", i->name, i->opens , i->closes );
}
}
int main() {
island amity = {"Amity", "09:00", "17:00", NULL};
island craggy = {"Craggy", "09:00", "17:00", NULL};
island isla_nublar = {"Isla Nublar", "09:00", "17:00", NULL};
island shutter = {"Shutter", "09:00", "17:00", NULL};
amity.next = &craggy;
craggy.next = &isla_nublar;
isla_nublar.next = &shutter;
island skull = {"Skull", "09:00", "17:00", NULL};
isla_nublar.next = &skull;
skull.next = &shutter;
display(&amity);
}
很快就会发生存储器泄漏。
#include <stdlib.h> // 为了使用malloc()和free()函数,需要包含stdlib.h头文件
...
island *p = malloc(sizeof(island)); // 表示“给我足够的空间来保存island结构”
free(p); // 表示“释放你分配的存储器,从堆地址 p 开始”
#include <stdio.h>
#include <stdlib.h>
typedef struct island { // 你必须为这个结构命名。
char *name;
char *opens;
char *closes;
struct island *next; // 你在结构中保存了一个指针,指向下一座岛。
} island;
void display(island *start) {
island *i = start;
for (; i!=NULL ; i = i->next ) {
printf("Name: %s\n open: %s-%s\n", i->name, i->opens , i->closes );
}
}
island* create(char *name) {
island *i = malloc(sizeof(island));
i->name = name;
i->opens = "09:00";
i->closes = "17:00";
i->next = NULL;
return i;
}
int main() {
char name[80];
fgets(name, 80, stdin);
island *p_island0 = create(name);
fgets(name, 80, stdin);
island *p_island1 = create(name);
p_island0->next = p_island1;
display(p_island0);
}
char *s = "MONA LISA";
char *copy = strdup(s);
...
island* create(char *name) {
island *i = malloc(sizeof(island));
i->name = strdup(name); // 这里改用strdup将字符串复制到堆上
i->opens = "09:00";
i->closes = "17:00";
i->next = NULL;
return i;
}
int main() {
char name[80];
fgets(name, 80, stdin);
island *p_island0 = create(name);
fgets(name, 80, stdin);
island *p_island1 = create(name);
p_island0->next = p_island1;
display(p_island0);
// 记得释放
- free(p_island1->name);
- free(p_island1);
- free(p_island0->name);
- free(p_island0);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct island {
char *name;
char *opens;
char *close;
struct island *next; // 表示链表的下一个island
} island;
// 在堆内存中开辟一个island变量
island* create(char* name) {
island* i = malloc(sizeof(island));
i->name = strdup(name);
i->opens = "09:00";
i->close = "17:00";
i->next = NULL;
return i;
}
//释放岛链
void release(island *start) {
island *i = start;
island *next = NULL;
for (; i != NULL; i = next) {
next = i->next;
free(i->name);
free(i);
}
}
// 打印岛链
void printIslandLink(island* start) {
island* i;
for( i = start; i !=NULL; i = i->next) {
printf("Name: %s: open: %s-%s\n", i->name, i->opens , i->close);
}
}
void displayIslandLinkFromKeybord() {
island *start = NULL;
island *i = NULL;
island *next = NULL;
char name[80];
for (; fgets(name, 80, stdin) != NULL; i = next) {
next = create(name);
if (start == NULL)
start = next;
if (i != NULL)
i->next = next;
}
printIslandLink(start);
release(start);
}
int main() {
displayIslandLinkFromKeybord();
}
运行结果:
/*
* find_suspect.c
* Created on: 2016年11月23日
* Author: Administrator
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
char *question;
struct node *no;
struct node *yes;
} node;
int yes_no(char *question) {
char answer[3];
printf("%s? (y/n): ", question);
fgets(answer, 3, stdin);
return answer[0] == 'y';
}
node* create(char *question) {
node *n = malloc(sizeof(node));
n->question = strdup(question);
n->no = NULL;
n->yes = NULL;
return n;
}
void release(node *n) {
if (n) {
if (n->no)
release(n->no);
if (n->yes)
release(n->yes);
if (n->question)
free(n->question);
free(n);
}
}
int main() {
char question[80];
char suspect[20];
node *start_node = create("Does suspect have a mustache");
start_node->no = create("Loretta Barnsworth");
start_node->yes = create("Vinny the Spoon");
node *current;
do {
current = start_node;
while (1) {
if (yes_no(current->question)) {
if (current->yes) {
current = current->yes;
} else {
printf("SUSPECT IDENTIFIED\n");
break;
}
} else if (current->no) {
current = current->no;
} else {
/* Make the yes-node the new suspect name */
printf("Who's the suspect?");
fgets(suspect, 20, stdin);
node *yes_node = create(suspect);
current->yes = yes_node;
/* Make the no-node a copy of this question */
node *no_node = create(current->question);
current->no = no_node;
/* Then replace this question with the new question */
printf(
"Give me a question that is TRUE for %s but not for %s? ",
suspect, current->question);
fgets(question, 80, stdin);
// 这里current -> question已经指向了堆上的某个question,因此在分配新的question之前要先释放它
free(current->question); // 这句很容易漏掉!
current->question = strdup(question);
break;
}
}
} while (yes_no("Run again"));
release(start_node);
return 0;
}
译源代码:
执行命令 :gcc -g ./find_suspect.c -o ./find_suspect
// -g开关告诉编译器要记录要编译代码的行号。
linux下执行命令:valgrind --leak-check=full ./find_suspect
current->question = strdup(question);
...
free(current->question);
current->question = strdup(question);
...
HeadFirstC笔记_6 数据结构与动态存储:牵线搭桥相关推荐
- python的基本数据结构_Python学习笔记——基本数据结构
列表list List是python的一个内置动态数组对象,它的基本使用方式如下: shoplist = ['apple', 'mango', 'carrot', 'banana'] print 'I ...
- 软件设计师笔记之数据结构
数据结构是指数据对象及其相互关系和构造方法.在软件设计过程中,选用不同的数据结构对系统最终效果的影响极大.因此,该知识点是软件设计师核心考点. 本章我们需要掌握数组.图.广义表.树与二叉树.线性表.排 ...
- 梓益C语言学习笔记之链表&动态内存&文件
梓益C语言学习笔记之链表&动态内存&文件 一.定义: 链表是一种物理存储上非连续,通过指针链接次序,实现的一种线性存储结构. 二.特点: 链表由一系列节点(链表中每一个元素称为节点)组 ...
- 数据结构 (计算机存储、组织数据方式)
数据结构是计算机存储.组织数据的方式.数据结构是指相互之间存在一种或多种特定关系的数据元素的集合.通常情况下,精心选择的数据结构可以带来更高的运行或者存储效率.数据结构往往同高效的检索算法和索引技术有 ...
- 堆/栈/动态存储方式/静态存储方式
动态存储方式 所谓动态存储方式是指在程序运行期间根据需要进行动态的分配存储空间的方式.动态存储变量是在程序执行过程中,使用它时才分配存储单元, 使用完毕立即释放. 典型的例子是函数的形式参数,在函数定 ...
- 动态存储和静态存储区域区别
动态存储方式 所谓动态存储方式是指在程序运行期间根据需要进行动态的分配存储空间的方式.动态存储变量是在程序执行过程中,使用它时才分配存储单元, 使用完毕立即释放. 典型的例子是函数的形式参数,在函数定 ...
- mysql 临时表 事务_MySQL学习笔记十:游标/动态SQL/临时表/事务
逆天十三少 发表于:2020-11-12 08:12 阅读: 90次 这篇教程主要讲解了MySQL学习笔记十:游标/动态SQL/临时表/事务,并附有相关的代码样列,我觉得非常有帮助,现在分享出来大家一 ...
- 《算法思维——一种问题驱动的思维方式》之第11篇:数据结构之动态栈篇——采用C#编程语言实现
<算法思维--一种问题驱动的思维方式>之第11篇:数据结构之动态栈篇--采用C#编程语言实现 在数据结构的学习中,动态栈是最常见的一种线性数据结构,使用范围广. 栈,存储货物或供旅客住宿的 ...
- 《算法思维——一种问题驱动的思维方式》之第8篇:数据结构之动态栈篇——采用Java编程语言实现
<算法思维--一种问题驱动的思维方式>之第8篇:数据结构之动态栈篇--采用Java编程语言实现 在数据结构的学习中,动态栈是最常见的一种线性数据结构,使用范围广. 栈,存储货物或供旅客住宿 ...
最新文章
- 极限脱出 量子计算机,《极限脱出3:零时困境》50条指向zero身份线索
- oracle命令行导出、导入dmp文件
- asp.net 验证正则表达式
- maven项目转成web项目
- 处理UTF-8编码的不连续的字节流
- undertow服务器分析_使用undertow构建和测试Websocket服务器
- 在HTML中嵌入PHP代码,有以下几种方法,其中错误的是( )
- Linux环境下实现unsigned char*向string的转换
- 最佳的46+14款免费软件
- 大学论文的研究框架是什么?
- 推荐一些逐步深入学习mysql的书籍
- git简介以及windows下的git安装配置gitgub推送
- java id 锁_java 多线程synchronized同步锁锁住相同用户Id
- Vue路由对象属性 .meta $route.matched
- 计算机学frm,FRM金融计算器使用教程
- sklearn笔记29 线性回归 天猫双十一销量预测
- Tracup|拒绝低效办公,8个Tips让你芜湖起飞,工作效率MAX
- mac强制关机后悲剧了
- Android 标题栏及导航栏设计与实现
- 如何VXWORKS7.0 在应用程序里访问寄存器