数据结构堆栈 内存堆栈_了解堆栈数据结构

数据结构堆栈 内存堆栈

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
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

数据结构堆栈 内存堆栈

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值