3-6

内存:全局区+代码区+堆区+栈区
全局区:全局变量,static修饰的变量,const修饰的变量,常量,的存储区域
代码区:普通代码存放的区域
栈区(stack):局部变量存放的区域
堆区(heap):我们手动通过函数malloc()/calloc()/recalloc()/new()分配的内存所在区域。该区分配的空间的生命周期起始于malloc()等函数,终止于free()/delete(),空间被释放

在堆上开辟空间:
函数:malloc()
头文件:stdlib.h
函数功能:在堆上面开辟某字节长度的空间,成功,返回该内存的首地址,但是默认为void*型,所以要相应的进行类型强制转换成所需要的数据类型。
malloc(100);
char a;
a='c'

例:

include <stdio.h>

include <stdlib.h>

void main()
{
//void *
char *p=(char *)malloc(sizeof(char));
*p='a';

printf("%c\n",*p);
free(p);//释放指针p所对应的内存

}
//手动分配的区域,只能通过它返回的地址来找到它,所以该指针别搞丢了。

注:free(p) 释放的只能是nalloc分配的空间。p指的是那块空间的首地址。

include <stdio.h>

include <stdlib.h>

void main()
{
int *p=(int *)malloc(sizeof(int));
p=100;
printf("%d\n",
p);
free(p);
// p=NULL;//指针置空。
// p=200;
// printf("%d\n",
p);
}
注:系统所有的资源都是由操作系统负责调度和控制的,通过malloc分配一块内存,系统会把这块内存的使用权交出来,通过free释放,则收回使用权,名义上这块内存不能再私自访问了,但是偷偷的用有时候也可以,但说不定什么时候就报错了,所以对于一块内存,malloc了就能用,free了就不能用了。
练习:手动分配一块空间给学生,输入并输出学生的个人信息
struct student
{
int num;
char *name;
char sex;
float score;
};

include <stdio.h>

include <stdlib.h>

struct student
{
int num;
char *name;
char sex;
float score;
};

void main()
{
struct student p=(struct student )malloc(sizeof(struct student));
/
p->num=1;
p->name="asdfg";
p->sex='m';
p->score=91;
/
*p={1,"aaaaa",'m',91};
printf("%d %s %c %.1f\n",p->num,p->name,p->sex,p->score);

}

问题:新建一块内存区域,初步用来存放5个学生,后面陆续会增加学生和减少学生。
//节点的数据类型
struct student
{
int data;//数据
struct student *next;//指针,用来存放你下一个节点的地址。
};
//给节点开辟空间
  struct student *p=(struct student *)malloc(sizeof(struct student));

//定义一个结构体类型的时候,成员变量不能是本身结构体类型定义的变量,但可以是本身结构体类型定义的指针。

练习:新建一条含有n个节点的链表,n的值手动键盘输入。

include <stdio.h>

include <stdlib.h>

//确定节点的数据类型
struct student
{
//数据域
int data;
//指针域
struct student *next;
};
//新建链表
struct student *Create()
{
int n,i;
printf("请输入要创建的节点数:");
scanf("%d",&n);getchar();
struct student *head=(struct student *)malloc(sizeof(struct student));
struct student *p=(struct student *)malloc(sizeof(struct student));
printf("请输入数据:");
scanf("%d",&p->data); getchar();
head->next=p;//连接head和p节点
for(i=1;i<n;i++)
{
struct student *q=(struct student *)malloc(sizeof(struct student));
printf("请输入数据:");
scanf("%d",&q->data); getchar();
p->next=q;//连接p和q节点
p=q;
}
p->next=NULL;//最后一个节点是p节点,它没有下一个节点
return head;
}
//打印链表
void Print(struct student *head)
{
struct student *p=head->next;
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}
}
//头插
struct student *T_insert(struct student *head)
{
struct student *q=(struct student *)malloc(sizeof(struct student));
printf("请输入数据(头插):");
scanf("%d",&q->data);getchar();
q->next=head->next;
head->next=q;
return head;
}
//尾插
struct student *W_insert(struct student *head)
{
struct student *q=(struct student *)malloc(sizeof(struct student));
printf("请输入数据(尾插):");
scanf("%d",&q->data);getchar();
struct student *p=head->next;
while(p->next!=NULL)
{
p=p->next;
}
p->next=q;
q->next=NULL;
return head;
}
//中插
struct student *Z_insert(struct student *head)
{
int loc,i;
printf("在第几个节点后插入:");
scanf("%d",&loc);
struct student *q=(struct student *)malloc(sizeof(struct student));
printf("请输入数据:");
scanf("%d",&q->data);getchar();
struct student *p=head->next;
for(i=1;i<loc;i++)
p=p->next;
q->next=p->next;
p->next=q;
return head;
}
//查:
//1.按节点位置查找
struct student *Find1(struct student *head)
{
int n,i=1; printf("查找第几个节点:");
scanf("%d",&n);getchar();
struct student *p=head->next;
while( p->next!=NULL && i<n)
{ p=p->next; i++;}
if(i==n) return p;
else{printf("没有找到!\n");return NULL;}
}
//2.按值查找:找到和你输入的值相等的节点并返回该节点的地址
struct student *Find2(struct student *head)
{
int data;printf("请输入要找的值:");
scanf("%d",&data);
struct student *p=head->next;
while(p->data!=data && p->next!=NULL)
{p=p->next;}
if(p->data==data) return p;
else{printf("没有找到!\n");return NULL;}
}
void main()
{
struct student *head=Create(); Print(head);
head=T_insert(head);Print(head);
head=W_insert(head); Print(head);
head=Z_insert(head); Print(head);
struct student *p;
if(p=Find1(head)) printf("%d\n",p->data);
if(p=Find2(head)) printf("%d\n",p->data);
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,475评论 4 372
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,744评论 2 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,101评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,732评论 0 221
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,141评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,049评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,188评论 2 320
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,965评论 0 213
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,716评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,867评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,341评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,663评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,376评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,200评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,990评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,179评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,979评论 2 279

推荐阅读更多精彩内容