[置顶] ※数据结构※→☆线性表结构(stack)☆============栈 序列表结构(stack sequence)(六)
栈(stack)在计算机科学中是限定仅在表尾进行插入或删除操作的线性表。栈是一种数据结构,它按照后进先出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据。栈是只能在某一端插入和删除的特殊线性表。用桶堆积物品,先堆进来的压在底下,随后一件一件往堆。取走时,只能从上面一件一件取。堆和取都在顶部进行,底部一般是不动的。栈就是一种类似桶堆积物品的数据结构,进行删除和插入的一端称栈顶,另一堆称栈底。插入一般称为进栈,删除则称为退栈。 栈也称为后进先出表。
基本概念
首先系统或者数据结构栈中数据内容的读取与(压入push和 弹出pop)是两回事!插入是增加数据弹出是删除数据 ,这些操作只能从栈顶即最低地址作为约束的接口界面入手操作 ,但读取栈中的数据 是随便的 没有接口约束之说。很多人都误解这个理念从而对栈产生困惑。[1]而系统栈在计算机体系结构中 又起到一个跨部件交互的媒介区域的作用 即 cpu 与内存的交流通道 ,cpu只从系统给我们自己编写的应用程序所规定的栈入口线性地读取执行指令, 用一个形象的词来形容它就是pipeline(管道线、流水线)。cpu内部交互具体参见 EU与BIU的概念介绍。
栈特性
栈作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表。它按照后进先出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读出来)。栈具有记忆作用,对栈的插入与删除操作中,不需要改变栈底指针。
栈是允许在同一端进行插入和删除操作的特殊线性表。允许进行插入和删除操作的一端称为栈顶(top),另一端为栈底(bottom);栈底固定,而栈顶浮动;栈中元素个数为零时称为空栈。插入一般称为进栈(PUSH),删除则称为退栈(POP)。栈也称为后进先出表。
栈可以用来在函数调用的时候存储断点,做递归时要用到栈!
栈与计算机
在计算机系统中,栈则是一个具有以上属性的动态内存区域。程序可以将数据压入栈中,也可以将数据从栈顶弹出。在i386机器中,栈顶由称为esp的寄存器进行定位。压栈的操作使得栈顶的地址减小,弹出的操作使得栈顶的地址增大。
栈在程序的运行中有着举足轻重的作用。最重要的是栈保存了一个函数调用时所需要的维护信息,这常常称之为堆栈帧或者活动记录。堆栈帧一般包含如下几方面的信息:
1.函数的返回地址和参数
2. 临时变量:包括函数的非静态局部变量以及编译器自动生成的其他临时变量。
栈算法
进栈算法
①若TOP≥n时,则给出溢出信息,作出错处理(进栈前首先检查栈是否已满,满则溢出;不满则作②);
②置TOP=TOP+1(栈指针加1,指向进栈地址);
③S(TOP)=X,结束(X为新进栈的元素);
退栈算法
①若TOP≤0,则给出下溢信息,作出错处理(退栈前先检查是否已为空栈, 空则下溢;不空则作②);
②X=S(TOP),(退栈后的元素赋给X);
③TOP=TOP-1,结束(栈指针减1,指向栈顶)。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
以后的笔记潇汀会尽量详细讲解一些相关知识的,希望大家继续关注、支持、顶起我的博客。
你们的关注就是我的动力,本节笔记到这里就结束了。
潇汀一有时间就会把自己的学习心得,觉得比较好的知识点写出来和大家一起分享。
编程开发的路很长很长,非常希望能和大家一起交流,共同学习,共同进步。
如果文章中有什么疏漏的地方,也请大家留言指正。也希望大家可以多留言来和我探讨编程相关的问题。
最后,谢谢你们一直的支持~~~
------------------------------------------
Country: China
Tel: +86-152******41
QQ: 451292510
E-mail: xiaoting_chen2010@163.com
------------------------------------------
C++完整个代码示例(代码在VS2005下测试可运行)
AL_StackSeq.h
/**
@(#)$Id: AL_StackSeq.h 31 2013-09-05 09:02:18Z xiaoting $
@brief Stack (stack) in computer science is limited only in the end of the table to insert or delete operations linear form.
A stack is a data structure that in accordance with the principle of LIFO data storage, data is first pushed into the end of the
last data in the stack, you need to read the data when the data from the topmost pop. The stack is only one end of the inserted
and deleted special linear form. Buckets stacked items, first came under pressure in the heap, followed by a one to the heap.
Removed, only a one taken from above. Take the top of the heap and are carried at the bottom of the general is not moving. Stack
is a similar data structure barrels stacked items, delete and insert one end of said stack, said another pile bottom of the stack.
Insert commonly known as the push to delete is called the stack back. Also known as LIFO stack table. @Author $Author: xiaoting $
@Date $Date: 2013-09-05 17:02:18 +0800 (周四, 05 九月 2013) $
@Revision $Revision: 31 $
@URL $URL: https://svn.code.sf.net/p/xiaoting/game/trunk/MyProject/AL_DataStructure/groupinc/AL_StackSeq.h $
@Header $Header: https://svn.code.sf.net/p/xiaoting/game/trunk/MyProject/AL_DataStructure/groupinc/AL_StackSeq.h 31 2013-09-05 09:02:18Z xiaoting $
*/ #ifndef CXX_AL_STACKSEQ_H
#define CXX_AL_STACKSEQ_H ///////////////////////////////////////////////////////////////////////////
// AL_StackSeq
/////////////////////////////////////////////////////////////////////////// template<typename T>
class AL_StackSeq
{
public:
static const DWORD STACKSEQ_MAXSIZE = 0xffffffff;
static const DWORD STACKSEQ_DEFAULTSIZE = 100;
/**
* Construction
*
* @param DWORD dwSize (default value: STACKSEQ_DEFAULTSIZE)
* @return
* @note
* @attention
*/
AL_StackSeq(DWORD dwSize = STACKSEQ_DEFAULTSIZE); /**
* Destruction
*
* @param
* @return
* @note
* @attention
*/
~AL_StackSeq(); /**
* Empty
*
* @param VOID
* @return BOOL
* @note Returns true stack is empty
* @attention
*/
BOOL Empty() const; /**
* Pop
*
* @param VOID
* @return T
* @note Remove the top element and return data top element
* @attention if empty does not return a value... (please judge the stack is empty before use it)
*/
T Pop(); /**
* Push
*
* @param const T& tTemplate
* @return VOID
* @note Add elements in the stack
* @attention
*/
VOID Push(const T& tTemplate); /**
* Size
*
* @param VOID
* @return DWORD
* @note Returns the number of elements in the stack
* @attention
*/
DWORD Size() const; /**
* Top
*
* @param VOID
* @return DWORD
* @note Back to the top element...
* @attention if empty does not return a value... (please judge the stack is empty before use it)
*/
T Top() const; protected:
private:
/**
* GetBuffer
*
* @param VOID
* @return VOID
* @note get the work buffer
* @attention when the buffer is not enough, it will become to double
*/
VOID GetBuffer(); /**
* IsFull
*
* @param VOID
* @return BOOL
* @note the list is full?
* @attention
*/
BOOL IsFull() const; public:
protected:
private:
T* m_pElements;
DWORD m_dwMaxSize;
DWORD m_dwSize;
}; /**
* Construction
*
* @param DWORD dwSize (default value: STACKSEQ_DEFAULTSIZE)
* @return
* @note
* @attention
*/
template<typename T>
AL_StackSeq<T>::AL_StackSeq(DWORD dwSize):
m_pElements(NULL),
m_dwMaxSize(dwSize),
m_dwSize(0x00)
{
if (0x00 == m_dwMaxSize) {
//for memory deal
m_dwMaxSize = 1;
}
GetBuffer();
} /**
* Destruction
*
* @param
* @return
* @note
* @attention
*/
template<typename T>
AL_StackSeq<T>::~AL_StackSeq()
{
if (NULL != m_pElements) {
delete[] m_pElements;
m_pElements = NULL;
}
} /**
* Empty
*
* @param VOID
* @return BOOL
* @note Returns true stack is empty
* @attention
*/
template<typename T> BOOL
AL_StackSeq<T>::Empty() const
{
return (0x00 == m_dwSize) ? TRUE:FALSE;
} /**
* Pop
*
* @param VOID
* @return T
* @note Remove the top element and return data top element
* @attention if empty does not return a value... (please judge the stack is empty before use it)
*/
template<typename T> T
AL_StackSeq<T>::Pop()
{
T tTypeTemp;
memset(&tTypeTemp, 0x00, sizeof(T));
if (TRUE == Empty()) {
//Empty
return tTypeTemp;
} tTypeTemp = m_pElements[Size()-1];
memset(&m_pElements[Size()-1], 0x00, sizeof(T));
m_dwSize--; return tTypeTemp;
} /**
* Push
*
* @param const T& tTemplate
* @return VOID
* @note Add elements in the stack
* @attention
*/
template<typename T> VOID
AL_StackSeq<T>::Push(const T& tTemplate)
{
if (TRUE == IsFull()) {
// full, need to get more work buffer
GetBuffer();
} m_dwSize++;
m_pElements[Size()-1] = tTemplate;
} /**
* Size
*
* @param VOID
* @return DWORD
* @note Returns the number of elements in the stack
* @attention
*/
template<typename T> DWORD
AL_StackSeq<T>::Size() const
{
return m_dwSize;
} /**
* Top
*
* @param VOID
* @return DWORD
* @note Back to the top element...
* @attention if empty does not return a value... (please judge the stack is empty before use it)
*/
template<typename T> T
AL_StackSeq<T>::Top() const
{
T tTypeTemp;
memset(&tTypeTemp, 0x00, sizeof(T));
if (TRUE == Empty()) {
//Empty
return tTypeTemp;
} return m_pElements[Size()-1];
} /**
* GetBuffer
*
* @param VOID
* @return VOID
* @note get the work buffer
* @attention when the buffer is not enough, it will become to double
*/
template<typename T> VOID
AL_StackSeq<T>::GetBuffer()
{ if ( (Size() < m_dwMaxSize) &&(NULL != m_pElements) ) {
//we do not need to get more buffer
return;
} if (NULL == m_pElements) {
if(0 < m_dwMaxSize){
//get the new work buffer
m_pElements = new T[m_dwMaxSize];
memset(m_pElements, 0x00, sizeof(T)*m_dwMaxSize);
}
return;
} //we need to get more buffer, store the previous pointer
T* pLastTpye = NULL;
DWORD pLastSize = 0x00; // it will become to double
pLastSize = m_dwMaxSize;
pLastTpye = m_pElements;
if (STACKSEQ_MAXSIZE/2 < m_dwMaxSize) {
m_dwMaxSize = STACKSEQ_MAXSIZE;
}
else {
m_dwMaxSize *= 2;
}
if(0 < m_dwMaxSize){
//get the new work buffer
m_pElements = new T[m_dwMaxSize];
memset(m_pElements, 0x00, sizeof(T)*m_dwMaxSize);
}
//need to copy the last to the current
memcpy(m_pElements, pLastTpye, sizeof(T)*pLastSize);
//free the last work buffer
delete[] pLastTpye;
pLastTpye = NULL;
} /**
* IsFull
*
* @param
* @return BOOL
* @note the list is full?
* @attention
*/
template<typename T> BOOL
AL_StackSeq<T>::IsFull() const
{
return (m_dwMaxSize == Size()) ? TRUE:FALSE;
}
#endif // CXX_AL_STACKSEQ_H
/* EOF */
测试代码
#ifdef TEST_AL_STACKSEQ
AL_StackSeq<DWORD> cStackSeq(1);
BOOL bEmpty = cStackSeq.Empty();
std::cout<<bEmpty<<std::endl;
DWORD dwPop = cStackSeq.Pop();
std::cout<<dwPop<<std::endl;
DWORD dwSize = cStackSeq.Size();
std::cout<<dwSize<<std::endl;
DWORD dwTop = cStackSeq.Top();
std::cout<<dwTop<<std::endl; for (WORD wCnt=1; wCnt<16; wCnt++) {
//push 15 14 13 12.....
cStackSeq.Push(16 - wCnt);
dwTop = cStackSeq.Top();
std::cout<<dwTop<<std::endl;
} bEmpty = cStackSeq.Empty();
std::cout<<bEmpty<<std::endl;
dwSize = cStackSeq.Size();
std::cout<<dwSize<<std::endl; while (0x00 != cStackSeq.Size()) {
dwPop = cStackSeq.Pop();
std::cout<<dwPop<<std::endl;
}
#endif
[置顶] ※数据结构※→☆线性表结构(stack)☆============栈 序列表结构(stack sequence)(六)的更多相关文章
- [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)
优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============循环队列 顺序存储结构(queue circular sequence)(十)
循环队列 为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量.存储在其中的队列称为循环队列(Circular Queue). ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============队列 顺序存储结构(queue sequence)(八)
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表.进行插入操作的端称为队尾,进行删除操作的 ...
- [置顶] ※数据结构※→☆线性表结构(list)☆============单向链表结构(list single)(二)
单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始:链表是使用指针进行构造的列表:又称为结点列表,因为链表是由一个个结点组装起来的:其中每个结点都有指 ...
- [置顶] ※数据结构※→☆线性表结构(list)☆============双向链表结构(list double)(三)
双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点. ~~~~~~~~~~~~ ...
- 数据结构复习笔记(ADT栈/LIFO表)
栈是一种特殊的表,只在表首进行插入和删除操作,表首称之为栈顶,表尾称为栈底:栈的核心原则是先进后出,简称Last In First Out(LIFO表):常用的运算有:1.是否为空栈判断:2.栈是否满 ...
- [置顶] ※数据结构※→☆非线性结构(tree)☆============树结点 链式存储结构(tree node list)(十四)
结点: 包括一个数据元素及若干个指向其它子树的分支:例如,A,B,C,D等. 在数据结构的图形表示中,对于数据集合中的每一个数据元素用中间标有元素值的方框表示,一般称之为数据结点,简称结点. 在C语言 ...
- 数据结构图文解析之:栈的简介及C++模板实现
0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...
- STL --> stack栈
stack栈 c++stack(堆栈)是一个容器的改编,它实现了一个先进后出的数据结构(FILO),使用该容器时需要包含#include<stack>头文件: 定义stack对象示例: s ...
随机推荐
- Sting和StringBuffer的区别
java.lang.String代表不可变序列: s1 = "hello"; s2 = "world"; s1 = s1 + s2; 内存分配情况是s1有块内存 ...
- [linux]linux命令学习-netstat
linux非常多服务都与网络相关.当服务调不通或者是启动port被占用,或者是又是被防火墙挡住的时候,就须要查询网络相关的问题,netstat命令之前仅仅会用一两个參数这里.好好学习一番. 经常使用的 ...
- Qt递归拷贝和删除目录
最近在翻看项目代码时,看到了这两个函数,想到这个功能十分常用,因此拿出来与大家分享,希望对大家有用.几点说明: 1.记得当初写代码那会,是参考了网上的帖子写的,做了一点小修改.因此代码源于网络. 2. ...
- c# winform 让Form去掉系统自带的关闭
在桌面系统时我们有时候想把winform 自带的关闭按钮和最大化最小化都去掉,我遇到了类似的情况,在网上一查也有很多答案,但是最后找到了一个最简单的答案,一句话的事,今天记录一下,就是让大家都简单的实 ...
- VC socket Connect 超时时间设置
设置connect超时很简单,CSDN上也有人提到过使用select,但却没有一个令人满意与完整的答案.偶所讲的也正是select函数,此函数集成在winsock1.1中,简单点讲,"作用使 ...
- 弹出框weeboxs 基本属性总结
使用前需包含以下jquery.js.bgiframe.js.weebox.js文件 boxid: null, //设定了此值只后,以后在打开同样boxid的弹窗时,前一个将被自 动关闭 boxclas ...
- 在webx.ml中 配置struts2 后 welcome-file-list 失效的解决办法
struts2 <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.a ...
- delphi实现图象灰度处理的3种方法
灰度处理的方法主要有如下3种: 1.最大值法:使R.G.B的值等于3值中最大的一个,即: R=G=B=max(R,G,B) 最大值法会使形成高亮度很高的灰度图象 var bitmap:tbitma ...
- 一些窗口API函数,比如SetForegroundWindow,SwitchToThisWindow
SetForegroundWindowSwitchToThisWindow procedure TApplication.BringToFront;varTopWindow: HWnd;beginif ...
- JQuery+AJAX实现搜索文本框的输入提示功能
平时使用谷歌搜索的时候发现只要在文本框里输入部分单词或字母,下面马上会弹出一个相关信息的内容框可供选择.感觉这个功能有较好的用户体验,所以也想在自己的网站上加上这种输入提示框. 实现的原理其实很简单, ...