数据结构堆栈 内存堆栈

In this article, we’ll be understanding the working and the need for the Stack Data Structure.

在本文中,我们将了解Stack Data Structure的工作原理和需求。

When programming, we often deal with a huge amount of uneven and raw data. This calls for the need of data structures to store the data and enable the user to operate on the data efficiently.

编程时,我们经常处理大量不均匀的原始数据。 这就需要数据结构来存储数据并使用户能够有效地对数据进行操作。



堆栈数据结构入门 (Getting started with the Stack Data Structure)

A stack is a linear data structure that follows a particular order for the insertion and manipulation of data. Stack uses Last-In-First-Out (LIFO) method for input and output of data.

堆栈是一种线性数据结构 ,遵循特定的顺序进行数据的插入和操作。 堆栈使用Last-In-First-Out (LIFO)方法输入和输出数据。

A stack occupies elements of a particular data type in it in a pre-defined order i.e. LIFO.

堆栈以预定义的顺序(即LIFO)在其中占据特定数据类型的元素。

Stack叠放

As seen in the above pictorial representation of stack, the last element is the first to be popped out from the stack.

如上图所示,堆栈中的最后一个元素是第一个从堆栈中弹出的元素。

The insertion and deletion of data items take place from the same end of the stack.

数据项的插入和删除从堆栈的同一端进行

Let’s try to relate stacks with real-life scenarios.

让我们尝试将堆栈与实际场景联系起来。

Consider a pile of books. If you observe, the last book placed would be the first one to be accessed by us. Moreover, a new book can be placed only at the top of the pile. Thus, depicting the working of Stacks.

考虑一堆书。 如果您观察的话,最后放置的书将是我们所阅读的第一本书。 而且,一本新书只能放在书堆的顶部。 因此,描述了堆栈的工作。



堆栈上执行的操作 (Operations Performed on a Stack)

The Stack data structure primarily deals with the following operations on the data:

堆栈数据结构主要处理对数据的以下操作:

  • push(): This function adds data elements to the stack.push() :此函数将数据元素添加到堆栈中。
  • pop(): It removes the top element from the stack.pop() :从堆栈中删除顶部元素。
  • top(): Returns the topmost element i.e. element at the top position of the stack.top() :返回最顶部的元素,即堆栈顶部的元素。
  • isEmpty(): Checks whether the stack is empty or not i.e. Underflow condition.isEmpty() :检查堆栈是否为空,即下溢情况。
  • isFull(): Checks whether the stack is full or not i.e. Overflow condition.isFull() :检查堆栈是否已满,即溢出条件。


堆栈数据结构的工作 (Working of the Stack Data Structure)

Stack data structure follows the LIFO pattern for the insertion and manipulation of elements into it.

堆栈数据结构遵循LIFO模式,用于在其中插入和操作元素。

Note: We have set a pointer element called ‘top‘ to keep the account of the topmost element in the stack. This would help the insertion and deletion operation to be performed in an efficient manner.

注意:我们设置了一个名为“ top ”的指针元素,以保留堆栈中最顶层的元素。 这将有助于以有效的方式执行插入和删除操作。

PUSH (insertion) operation:

PUSH(插入)操作:

  • Initially, it checks whether the stack is full or not i.e. checks for the Overflow condition.最初,它检查堆栈是否已满,即检查溢出条件
  • In case the stack is found to be full, it will exit with an Overflow message.如果发现堆栈已满 ,它将退出并显示一条溢出消息
  • If the stack is found to have space for occupying elements, then the top counter is incremented by 1 and then the data item is added to the stack.如果发现堆栈中有可容纳元素的空间,则将顶部计数器 1 ,然后将数据项添加到堆栈中。

POP (deletion) operation:

POP(删除)操作:

  • Initially, it checks whether the stack is empty or not, i.e. checks for the Underflow condition.最初,它检查堆栈是否为空,即检查下溢条件
  • In case the stack is found to be empty, it will exist with an Underflow message.如果发现堆栈为 ,它将存在一条下溢消息
  • If the stack is not empty, display the element pointed by the top pointer element and then decrement the top pointer by 1.如果堆栈不为空,则显示顶部指针元素指向的元素,然后将顶部指针减1。


堆栈数据结构的实现 (Implementation of the Stack Data Structure)

Stack can be implemented using either of the following ways:

可以使用以下两种方式之一来实现堆栈:

  • Linked List链表
  • Array数组

We’ve already written a comprehensive article on Stack in C++. For the demonstration, I’ll create a simple implementation of a stack using an Array in C++ language.

我们已经在C ++中撰写了一篇有关Stack的综合文章。 为了演示,我将使用C ++语言的Array创建一个简单的堆栈实现。

Example:

例:


# include<iostream>
using namespace std;class stack_data
{public:int top;int data[5];  stack_data(){top = -1;}void push_element(int a);int pop_element();void isEmpty();void isFull();void display();
};void stack_data::push_element(int a)
{if(top >= 5){cout << "Overflow\n";}else{data[++top] = a;}
}int stack_data::pop_element()
{if(top < 0){cout << "Underflow\n";return 0;}else{int pop = data[top--];return pop;}
}void stack_data::isEmpty()
{if(top < 0){cout << "Stack Underflow\n";}else{cout << "Stack can occupy elements.\n";}
}void stack_data::isFull()
{if(top >= 5){cout << "Overflow\n";}else{cout << "Stack is not full.\n";}
}void stack_data::display()
{for(int i=0;i<5;i++){cout<<data[i]<<endl;}
}int main() {stack_data obj;obj.isFull();obj.push_element(40);obj.push_element(99);obj.push_element(66);obj.push_element(40);obj.push_element(11);cout<<"Stack after insertion of elements:\n";obj.display();cout<<"Element popped from the stack:\n";cout<<obj.pop_element()<<endl;}

Output:

输出:


Stack is not full.
Stack after insertion of elements:
40
99
66
40
11
Element popped from the stack:
11


堆栈功能 (Features of Stack)

  • Stack follows Last-In-First-Out fashion to deal with the insertion and deletion of elements.堆栈遵循Last-In-First-Out方式处理元素的插入和删除。
  • The insertion and deletion of data items happen only from a single end.数据项的插入和删除仅从single end
  • Stack is considered to be dynamic in nature i.e. it is a dynamic data structure.堆栈本质上被认为是dynamic ,即它是dynamic data structure
  • Stacks do not occupy a fixed size of data items.堆栈不占用fixed size的数据项。
  • The size of the stack keeps changing with every push() and pop() operation.堆栈的大小随每个push()pop()操作而不断变化


堆栈的应用 (Applications of Stack)

  • In programming, Stack can be used to reverse the elements of an array or string.在编程中,堆栈可用于反转数组或字符串的元素。
  • A stack can be useful in situations where Backtracking is necessary such as N-queens problem, etc.堆栈在需要回溯的情况下很有用,例如N皇后问题等。
  • In editors, the undo and redo functions can be performed efficiently by Stack.在编辑器中,堆栈可以有效地执行撤消重做功能。
  • Infix/Prefix/Postfix conversion of Binary Trees.二叉树的中 / 前缀 / 后缀转换。


堆栈操作的时间复杂度 (Time Complexity of Stack Operations)

  • Push operation i.e. push(): O(1)推送操作,即push(): O(1)
  • Pop operation i.e. pop(): O(1)弹出操作,例如pop(): O(1)

The time complexities of push() and pop() operations stay O(1) because the insertion and deletion of the data items happen only from one end of the stack which is a single step process to be performed.

push()和pop()操作的时间复杂度保持为O(1),因为数据项的插入和删除仅从堆栈的一端开始 ,这是要执行的单步过程。



结论 (Conclusion)

Thus, in this article, we have understood the need for and implementation of a Stack data structure in various programming applications.

因此,在本文中,我们已经了解了各种编程应用程序中对Stack数据结构的需求和实现。

翻译自: https://www.journaldev.com/36920/stack-data-structure

数据结构堆栈 内存堆栈

数据结构堆栈 内存堆栈_了解堆栈数据结构相关推荐

  1. 数据结构堆栈 内存堆栈_零堆栈数据科学家第二部分秋天

    数据结构堆栈 内存堆栈 In Hollywood, it is known that the sequels are rarely better than the original movie/par ...

  2. 数据结构 python堆_Python中的堆栈数据结构是什么?

    成为专业认证的数据结构是数据值的集合,它们之间的关系,以及可以应用于数据的函数或操作.现在有很多可用的数据结构.但今天我们的重点将放在堆栈数据结构上.我将讨论以下主题:为什么是数据结构?数据结构类型什 ...

  3. js堆和栈的区别_几个例子理解不同数据类型的堆栈内存处理

    如有错误烦请指正 js代码的运行环境 浏览器 内核(引擎) node webview(hybrid,嵌入到手机app里面,在app里面运行) ... 下面通过几个例子理解不同数据类型的堆栈内存处理 j ...

  4. JS函数简单的底层原理 -变量重复声明无效,隐式申明,变量提升,函数提升,以及堆栈内存的变化

    JS函数简单的底层原理 (个人理解): 1. 已经使用var申明且赋值,若再次申明,则第二次申明(不赋值)无效. 2.在同一个作用域下,只要是发生了同名,且变量完成赋值,后者会覆盖前者.存在两个相同的 ...

  5. javascript基础系列:堆栈内存(stackamp;heap)(二)

    忙了一段时间,很容易忘记更新博客文章,拖延症很严重,今天晚上趁着有时间,继续系统温习,希望对大家有所帮助.本文章仅代表前端岚枫的个人观点,有不正确还望指出. 浏览器运行机制及基本类型与引用类型的区别 ...

  6. 夯实基础,彻底掌握js的核心技术(三):堆栈内存及闭包详解

    数据渲染机制及堆栈内存 1. 数据值操作机制 /* * 1. 先声明一个变量a,没有赋值(默认值谁undefined) * 2. 在当前作用域中开辟一个位置存储12这个值 * 3. 让变量a和12关联 ...

  7. java中堆栈内存_Java堆空间与堆栈– Java中的内存分配

    java中堆栈内存 Sometime back I wrote a couple of posts about Java Garbage Collection and Java is Pass by ...

  8. 数据类型与堆栈内存练习数据类型检测

    目录 数据类型相关练习 堆栈内存相关练习 数据类型检测 数据类型相关练习 1. let a = {},b = '0',c = 0; a[b] = '你好': a[c] = '再见': console. ...

  9. 【金三银四】 一文弄懂 js 数据类型、堆栈内存、作用域(链)、闭包知识拓展 (一)

    引言 对答如流系列篇,关于基本数据类型.堆栈内存.作用域作用域链.闭包 大家好,这里是lionLoveVue,基础知识决定了编程思维,学如逆水行舟,不进则退.金三银四,为了面试也还在慢慢积累知识,Gi ...

最新文章

  1. deepspeech实时语音识别
  2. archlinux安装TensorFlow带GPU
  3. 1053 Path of Equal Weight
  4. BSCI—9:配置OSPF认证
  5. 最近项目重构的一些感想
  6. C#开发微信门户及应用(25)-微信企业号的客户端管理功能
  7. Auto-Publishing and Monitoring APIs With Spring Boot--转
  8. 使用vs2005进行(wince)DLL源码调试
  9. SAS笔记(6) PROC MEANS和PROC FREQ
  10. Windows7休眠状态下载技巧攻略
  11. 云+X案例展 | 金融类:七牛云Pandora 助阵某银行实现日志智能管理
  12. 快速搭建Python+Selenium+Sublime 自动化测试环境方法
  13. 武汉街头出现手机无线充电路灯,极速快充,但需注意这一点!
  14. 小程序navigator点击有时候会闪一下
  15. nfine框架 上传文件_NFine快速开发框架
  16. 最好用的WIN7WIN10激活工具
  17. 客户至上 | 国产BI领跑者,思迈特软件完成C轮融资
  18. 机器人设计之软件设计
  19. Java之List系列--ArrayList扩容的原理
  20. 录音时分离左右声道的数据

热门文章

  1. Android - 文字中显示图片
  2. 第一个ExtJS练习(添加用户面板)
  3. 百度搜索URL参数的含义
  4. 探寻 JavaScript 逻辑运算符(与、或)的真谛
  5. javascript 布尔类型
  6. maven更新总结与tomcat发布方法总结
  7. SDL_BlitSurface的参数是两个PNG时,如何保护其透明度
  8. [转载] python中svm的使用_Python中支持向量机SVM的使用方法详解
  9. UDP协议和socketserver以及文件上传
  10. 关于vue的npm run dev和npm run build