资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言有堆栈数函数吗 c语言有堆栈数函数吗怎么写

谁能帮我说下C语言中的堆栈

个人认为楼上的不懂C语言堆栈到底是怎么回事,按楼上说法,只是大概讲了下栈,没有讲堆.

创新互联公司2013年成立,是专业互联网技术服务公司,拥有项目网站建设、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元合江做网站,已为上家服务,为合江各地企业和个人服务,联系电话:13518219792

要讲C语言的堆栈,要从计算机的数据内存分配讲起.

____________________

| Stack区(数组,指针,结构体,局部变量)

____________________

| Static变量(静态变量,全局变量)

____________________

| Heep区(堆区)

____________________

| 代码段

____________________

从上面示意图中可看出整个内存分配,堆分配是在内存中按块划分,也就是相对与函数malloc,realloc,calloc.这3个函数为内存分配函数.而且需要手动调用free函数释放资源,否则会造成大量的内存碎片.

如果楼主不相信可以自己写一个死循环,内部调用malloc函数,创建N个内存块,运行一段时间后,绝对会造成系统瘫痪,资源被耗尽.

栈区划分为计算机自身划分,即在函数或局部变量被调用时,系统自动为其分配栈,以后进先出为原则实现变量的保存,在函数调用完毕时,系统会自动释放栈内资源,所以,栈可以说是短命的(生存周期只在调用过程中).

这里只是粗略说了下堆和栈,另外再说下static--静态区,全局变量或静态变量存放于静态区,只要代码中存在静态变量或全局变量,自动放于静态区,静态区存放的变量生存周期是整个程序结束时才释放.

代码段区,顾名思义存放的是程序代码(暂时先这么理解).

PS:本人原创,最近发现一些人盗用本人回答的问题.特此声明.嘿嘿.

____________________ _________

补充:

我对于C#不是很熟悉,而且我也是从事C开发的,对于面向对象语言应用不是很熟.在这只能给出C++的代码.代码有点长,不知道你能不能看的懂,才写的.

#include iostream.h

#include stdlib.h

#include malloc.h

#include string.h

#include time.h

#include stdio.h

#include assert.h

/*

//基于数组的栈的实现

#define N 50

typedef struct Stack{

int top;

int A[N];

}*pStack;

//Pop出栈

int Pop(pStack pst)

{

int e;

if(pst-top == -1)

{

cout"Stack is empty!"endl;

return -1;

}

else

{

e = pst-A[pst-top];

pst-top--;

// cout"The element "e" is pop"endl;

return e;

}

}

//Push入栈

void Push(pStack pst)

{

int e;

if(pst-top == N-1)

{

cout"Stack is full!"endl;

}

else

{

cout"Input the push number:";

cine;

pst-top++;

pst-A[pst-top] = e;

}

}

//清空栈

void empty(pStack pst)

{

pst-top = -1;

}

//判断栈是否为空

int IsEmpty(pStack pst)

{

if(pst-top == -1)

{

return 0;

// cout"The Stack is empty!"endl;

}

else

{

return 1;

// cout"The Stack is not empty!"endl;

}

}

//判断栈是否为满

int IsFull(pStack pst)

{

if(pst-top == N-1)

{

return 0;

}

else

{

return 1;

}

}

//初始化栈

void InitStack(pStack pst)

{

pst-top = -1;

}

void main()

{

Stack S;

InitStack(S);

int n;

cout"How many times do you want to Push:";

cinn;

for(int i=0; in; i++)

{

Push(S);

}

cout"How many times do you want to Pop:";

cinn;

for(i=0; in; i++)

{

cout"The element "Pop(S)" is pop"endl;

}

cout"The Stack's stutor:"endl;

if(IsEmpty(S) == 0)

{

cout"The Stack is empty!"endl;

}

else

{

cout"The Stack is not empty!"endl;

}

if(IsFull(S) == 0)

{

cout"The Stack is full!"endl;

}

else

{

cout"The Stack is not full!"endl;

}

empty(S);

cout"The Stack's stutor:"endl;

if(IsEmpty(S) == 0)

{

cout"The Stack is empty!"endl;

}

else

{

cout"The Stack is not empty!"endl;

}

}

*/

typedef struct Stack{

Stack *prior;

Stack *next;

int element;

}*pStack;

//压栈

void Push(pStack *pst)

{

if((*pst) == NULL)

{

pStack S = (pStack)malloc(sizeof(Stack));

(*pst) = S;

(*pst)-next = NULL;

(*pst)-prior = NULL;

cout"Input the PUSH data:";

cin(*pst)-element;

}

else

{

pStack S = (pStack)malloc(sizeof(Stack));

(*pst)-next = S;

S-prior = (*pst);

S-next = NULL;

(*pst) = S;

cout"Input the PUSH data:";

cin(*pst)-element;

}

}

//判断是否为空

int IsEmpty(pStack pst)

{

if(pst == NULL)

{

cout"The Stack is empty!"endl;

return 1;

}

return 0;

}

//出栈

pStack Pop(pStack *pst)

{

if(IsEmpty((*pst)) == 1)

return (*pst);

pStack S = (*pst);

if((*pst)-prior == NULL)

{

cout"Out:"(*pst)-elementendl;

(*pst) = NULL;

free(S);

return (*pst);

}

else

{

cout"Out:"(*pst)-elementendl;

(*pst) = (*pst)-prior;

(*pst)-next = NULL;

free(S);

return (*pst);

}

}

//初始化栈

void InitStack(pStack pst)

{

pst = NULL;

}

void main()

{

pStack pS = NULL;

// InitStack(pS);

int n;

cout"How many times do you want to Push:";

cinn;

for(int i=0; in; i++)

{

Push(pS);

}

pStack S;

S = Pop(pS);

}

请用C语言编写一个堆栈函数程序

//该程序简单并可正确运行,希望kutpbpb的回答能对你有所帮助!

#includestdio.h

#define N 100

typedef struct

{

int value[N];

int base;

int top;

}Sta;

void print()

{

printf("\n菜单:");

printf("\n1.入栈:");

printf("\n2.出栈:");

printf("\n3.退出:");

}

void printS(Sta S)

{

printf("\n请输出栈中元素:");

for(int i=S.top;i!=S.base;i--)

printf("%d ",S.value[i-1]);

}

void pushS(Sta S,int e)

{

if(S.top==N)

printf("\n栈满");

else

S.value[S.top++]=e;

}

void popS(Sta S,int e)

{

if(S.top==S.base)

printf("\n栈空");

else

{

e=S.value[--S.top];

printf("\n请输出出栈元素: %d",e);

}

}

void main()

{

Sta S;

int e,choose;

S.base=S.top=0;

do{

print();

printf("\n请输入你的选项:");

scanf("%d",choose);

switch(choose)

{

case 1:

printf("\n请输入入栈元素:");

scanf("%d",e);

pushS(S,e);

printS(S);

break;

case 2:

popS(S,e);

printS(S);

break;

case 3:

default:

break ;

}

if(choose==3)

break;

}while(1);

}

C语言中的栈、堆是什么?

C语言中的堆和栈都是一种数据项按序排列的数据结构。

栈就像装数据的桶或箱子

我们先从大家比较熟悉的栈说起吧,它是一种具有后进先出性质的数据结构,也就是说后存放的先取,先存放的后取。

这就如同我们要取出放在箱子里面底下的东西(放入的比较早的物体),我们首先要移开压在它上面的物体(放入的比较晚的物体)。

堆像一棵倒过来的树

而堆就不同了,堆是一种经过排序的树形数据结构,每个结点都有一个值。

通常我们所说的堆的数据结构,是指二叉堆。堆的特点是根结点的值最小(或最大),且根结点的两个子树也是一个堆。

由于堆的这个特性,常用来实现优先队列,堆的存取是随意,这就如同我们在图书馆的书架上取书。

虽然书的摆放是有顺序的,但是我们想取任意一本时不必像栈一样,先取出前面所有的书,书架这种机制不同于箱子,我们可以直接取出我们想要的书。

扩展资料:

关于堆和栈区别的比喻

使用栈就象我们去饭馆里吃饭,只管点菜(发出申请)、付钱、和吃(使用),吃饱了就走,不必理会切菜、洗菜等准备工作和洗碗、刷锅等扫尾工作,他的好处是快捷,但是自由度小。

使用堆就象是自己动手做喜欢吃的菜肴,比较麻烦,但是比较符合自己的口味,而且自由度大。

参考资料来源:百度百科-堆栈


网页题目:c语言有堆栈数函数吗 c语言有堆栈数函数吗怎么写
文章转载:http://cdkjz.cn/article/ddcgdpd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220