关于堆栈的一个简单问题......... [英] A simple question about stack.........

查看:50
本文介绍了关于堆栈的一个简单问题.........的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我目前正在研究C中的数据结构。所以我有一个关于堆栈的简单问题。我得到的这本书讲的是堆栈:


请注意,堆栈中最常访问的元素是最顶层的元素,而最不可访问的元素是堆栈的底部。



对我来说,这句话(来自书中)有点令人困惑。我认为堆栈中的每个元素都必须(并且同样)可用于执行代码的引用/修改。但引用说最顶层元素是最容易获得的元素。

所以我需要你的帮助.....



我应该首先阅读一些关于数据结构或C中的数据结构的书?

另外,请你发一些好的名字吗? C中的数据结构书? :)



提前致谢........

AmbrNewlearner

解决方案

您只能访问堆栈的顶部元素。其他的被完全埋没,直到你从堆栈的顶部弹出一些项目。 有关堆栈的信息。


< blockquote>从理论上讲,这是真的:你当时只能在堆栈上推送一个元素,而你只能弹出顶部堆栈元素(如果有的话)。


实际上,堆栈通常由数组或列表或向量实现,或者

无论如何。这个底层类经常通过堆栈类暴露其额外的方法,所以你可以随意摆弄其他堆栈元素。

这样做你自担风险...


亲切的问候,


Jos


当使用像C这样的高级(ish)语言时(或C ++)堆栈条目是在堆栈帧中生成的,每次进行函数调用时,堆栈帧都放在堆栈上。


堆栈帧包含处理器返回函数所需的所有数据(各种堆栈指针和返回地址)以及声明的所有堆栈(自动)变量的空间在函数中。


因此,您只能访问堆栈中最顶层的条目,但这是最顶层的堆栈框架,因此您可以访问函数中的所有变量,因为它们包含在单个堆栈条目中。您无法从调用当前函数的函数中访问变量,也无法在调用树中进一步运行变量,因为它位于堆栈的较高条目中。



我以为你可能不清楚什么是一个条目在堆栈上,对于高级语言,它不是单个变量。


Hello,

I am presently studying Data structures in C. And so I have a simple question about stack. The book which I got my hands on says about stack:

Note that the most frequently accessible element in the stack is the top most element, whereas the least accessible element is the bottom of the stack.

For me this quote (from the book) is a bit confusing. I think that every element on the stack must be potentially (and equally) available for reference/modification by the executing code. But the quote says that top most element is most accessible element.
And so I need your help.....


Should I first read some book on ''data structures'' or ''data strucrtures in C''?
Also, can you please post the name of some "good" book for data structures in C? :)


Thanks in advance........
AmbrNewlearner

解决方案

You can only access the top element of a stack. The other ones are buried completely until you pop some items off the top of the stack. Info about stacks.


In theory that''s true: you can only push one element on the stack at the time and
you can only pop the top stack element (if there is one).

In practice however a stack is often implemented by an array or list or vector or
whatever. That underlying class often exposes its additional methods through
the stack class so you can fiddle diddle with other stack elements at will.
Do that at your own risk though ...

kind regards,

Jos


When using a high(ish) level language like C (or C++) the stack entries are made in stack frames, every time a function call is made a stack frame is placed on the stack.

A stack frame contains all the data required by the processor to return form the function (various stack pointers and the return address) as well as space for all the stack (automatic) variables declared in the function.

As such you can only access the top-most entry in the stack but that is the top-most stack frame so you can access all the variables in the function because they are contained in a single stack entry. You can not access the variables from the function that call the current function or functions further up the call tree because that is in a higher entry on the stack.


I thought you may not be clear on what "an entry" on the stack was, for a high level language it is not a single variable.


这篇关于关于堆栈的一个简单问题.........的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆