本文共 7193 字,大约阅读时间需要 23 分钟。
c9500堆叠配置
A stack is a linear data structure in C++. It serves as a collection of data, arranged serially in a specific order. In a stack in C++, two of the basic stack operations are push and pop. Further, all of the operations on a stack are performed at one end.
堆栈是C ++中的线性数据结构。 它充当数据的集合,按特定顺序依次排列。 在C ++的堆栈中,两个基本的堆栈操作是push和pop。 此外,堆栈上的所有操作都在一端执行。
Also, a stack follows LIFO(Last In, First Out) fashion. And hence the last data element inserted is deleted or popped out first. So now let us dig deeper into the topic to have a clear understanding of the same.
此外,堆栈遵循LIFO(后进先出)的方式。 因此,最后插入的最后一个数据元素将被删除或弹出。 因此,现在让我们更深入地研究该主题,以清楚地了解该主题。
One can imagine a stack as if they’re blocks. You place one block on top of another and can continue to remove the top block which is the one you inserted last. Stack also works in a similar pattern. The latest data is stored at the top of the stack and when popping out, returns the top-most element.
可以想象堆栈就像是块一样。 您可以将一个块放在另一个块的顶部,然后可以继续删除最前面插入的那个块。 堆栈也以类似的方式工作。 最新数据存储在堆栈的顶部,并在弹出时返回最顶层的元素。
Now let us look at the basic operations that can be performed on a stack,
现在让我们看一下可以在堆栈上执行的基本操作,
We are going to discuss each operation one-by-one below.
我们将在下面逐一讨论每个操作。
Considering our previous example of blocks a push is an operation where we add a new block over the previous set.
考虑我们前面的块示例,推入是一种操作,其中我们在先前的集合上添加了一个新块。
In stack, push()
inserts a data element on the top of the previously pushed data. Let us have a look at the algorithm for pushing data.
在堆栈中, push()
在先前推送的数据的顶部插入一个数据元素。 让我们看一下推数据的算法。
Procedure PUSH(VALUE) If TOP==Size_of_Stack Write OVERFLOW else TOP=TOP+1 STACK[TOP]=VALUEEND of Procedure
Similarly, considering our previous example, pop()
is the operation where the top-most block is removed from the stack of blocks. Hence, pop removes and returns the element present at the top of the given Stack.
类似地,考虑我们前面的示例, pop()
是将最上面的块从块堆栈中删除的操作。 因此, pop删除并返回存在于给定Stack顶部的元素。
Algorithm for pop()
function can be given by:
pop()
函数的算法可以通过以下方式给出:
Procedure POP() If TOP==-1 Write UNDERFLOW else TOP=TOP-1 RETURN STACK[TOP+1]END of Procedure
The peek operation is similar to the pop operation in terms that it returns the topmost element from a stack. But the peek() operation does not delete the element, only returns it.
窥视操作类似于弹出操作,因为它从堆栈中返回最顶层的元素。 但是peek()操作不会删除元素,只会返回它。
Hence, the algorithm for peek()
function in Stack is given by:
因此,Stack中的peek()
函数的算法由下式给出:
Procedure PEEK() If TOP==-1 Write UNDERFLOW else Print STACK[TOP]END of Procedure
Noteworthy, in the worst-case scenario, the time complexity of any stack operation is constant and is given by O(1).
值得注意的是,在最坏的情况下,任何堆栈操作的时间复杂度都是恒定的,由O(1)给出 。
#includeusing namespace std;#define max 10 int stack[max],top=-1; //stack declarationvoid push(int num) //push() inserts n element into the Stack{ if(top==max) //checks is Stack if Full { cout<<"OVERFLOW!"; } else { top=top+1; //top is increased stack[top]=num; //element is inserted }} int pop() //pop() pops out the top-most element from the Stack{ if(top==-1) //Checks if Stack is empty { cout<<"UNDERFLOW!"; } else { top=top-1; return stack[top+1]; //top-most element popped and returned to the main function }}int peek(){ if(top==-1) //Checks if Stack is empty { cout<<"UNDERFLOW!"; } else { return stack[top]; //top-most element is returned to main() }}void show(){ int i=0; if(top==-1) //Check if stack is empty { cout<<"UNDERFLOW!"; } else if(top==max) //Check if stack is full { cout<<"OVERFLOW!"; } else { while(i<=top) //printing the current stack elements { cout<<"\t"< >val; push(val); break; case 2: cout<<"The popped element is : "< <
Output:
输出:
Understanding the code:
了解代码:
top
as ‘-1’ to signify that that stack is empty. An integer array, stack
is also declared with a size 20 首先,我们将top
初始化为“ -1”以表示该堆栈为空。 整数数组, stack
的大小也声明为20 push()
– It is the function that is later used to insert or push data into the Stack. A value is passed to this function which needs to be added into the stack at the top position push()
–此函数稍后用于将数据插入或推入堆栈中。 一个值传递给此函数,该值需要添加到栈顶位置 pop()
– It is the function which pops out the latest element from the top and returns it where the function was called pop()
–该函数从顶部弹出最新元素,并将其返回到调用该函数的位置 peek()
– It peeks on the top-most element present inside the stack and returns the same at the place of the function call. Remember, peek does not delete the top element from the stack, it just gives us peek of the value peek()
–窥视堆栈中存在的最顶层元素,并在函数调用的位置返回该元素。 请记住,peek不会从堆栈中删除顶部元素,而只是给我们提供了peek的价值 show()
– This function is created for the sole purpose of displaying the elements of the stack. show()
–创建此函数的唯一目的是显示堆栈的元素。 Did you notice the repetitive checking for the top?
您是否注意到重复检查顶部?
We do that because the operations on the stack cannot be performed if the stack gets overloaded or even if it doesn’t contain any data, this checking is required.
我们这样做是因为,如果堆栈过载或即使其中不包含任何数据,也无法执行堆栈上的操作,因此需要进行此检查。
For example, while applying the pop operation we must check whether the stack has any data stored inside or not to avoid errors. We have handled the error by printing the “UNDERFLOW” error in our code above.
例如,在执行弹出操作时,我们必须检查堆栈中是否存储了任何数据,以避免出错。 我们已经通过在上面的代码中打印“ UNDERFLOW”错误来处理该错误。
Ever thought how recursive function calls work? What happens when a function calls itself and halts until the later returns some value? This halting procedure takes place using stacks.
有没有想过递归函数调用是如何工作的? 当函数调用自身并暂停直到后者返回某个值时会发生什么? 暂停过程使用堆栈进行。
Suppose we have some nested functions given as
假设我们有一些嵌套函数给出为
f1() { return f2(); } f2() { return f3(); } f3() { return value; }
In this case:
在这种情况下:
Further Stack finds its application in different programming methods and techniques, some of them are:
进一步的堆栈可以在不同的编程方法和技术中找到其应用,其中一些是:
A stack is a very useful data structure with a wide range of applications. Hope this tutorial helps in understanding the concept of stacks in C++. For any questions feel free to ask in the comments.
堆栈是非常有用的数据结构,具有广泛的应用程序。 希望本教程有助于理解C ++中堆栈的概念。 对于任何问题,请随时在评论中提问。
翻译自:
c9500堆叠配置
转载地址:http://vgozd.baihongyu.com/