c9500堆叠配置_用C ++堆叠

c9500堆叠配置

介绍 (Introduction)

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(后进先出)的方式。 因此,最后插入的最后一个数据元素将被删除或弹出。 因此,现在让我们更深入地研究该主题,以清楚地了解该主题。

C ++中的堆栈工作 (Working of Stack in C++)

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,

现在让我们看一下可以在堆栈上执行的基本操作,

  • push()

    ()
  • pop()

    弹出 ()
  • peek()

    偷看 ()

We are going to discuss each operation one-by-one below.

我们将在下面逐一讨论每个操作。

1.在堆栈上推() (1. push() on Stack)

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]=VALUE
END of Procedure

2.从堆栈弹出() (2. pop() from Stack)

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

3. Stack中的peek() (3. peek() from Stack)

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

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

Noteworthy, in the worst-case scenario, the time complexity of any stack operation is constant and is given by O(1).

值得注意的是,在最坏的情况下,任何堆栈操作的时间复杂度都是恒定的,由O(1)给出

在C ++中实现堆栈 (Implementing Stack in C++)


#include<iostream>
using namespace std;
#define max 10                

int stack[max],top=-1;           //stack declaration

void 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"<<stack[i];
			i++;
		}
		cout<<endl;
	}
}
int main() 
{ 
    int ch,val;
    cout<<"   :::MENU:::";       //Menu for Stack operations
    cout<<"\n1.Push into the Stack\n2.Pop from Stack\n";
    cout<<"3.Peek from Stack\n4.Show the Stack\n5.Exit";
    while(1)    
    {
        printf("\nEnter the choice:");
        scanf("%d",&ch);
         
        switch(ch)
        {
            case 1: cout<<"Enter the value to be pushed: ";
            		cin>>val;
            		push(val);
                    break;
            case 2: cout<<"The popped element is : "<<pop()<<endl;
                    break;
            case 3: cout<<"The top-most element is : "<<peek()<<endl;
            		break;
            case 4: cout<<"Stack : ";
					show();
                    break;
            case 5: exit(0);
             
            default: printf("\nError! Invalid choice!...");
        }
    }
    return 0;
}

Output:

输出:

Stack In C Output
Stack In C++ Output
堆叠在C ++输出中

Understanding the code:

了解代码:

  • At first, we initialize 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”错误来处理该错误。

应用领域 (Applications)

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:

在这种情况下:

  • as soon as f1 is called it is pushed into the stack

    一旦f1被调用,它将被推入堆栈
  • Similarly, consecutive calls of the functions f2 and f3 also push both one-by-one into the stack

    同样,对函数f2f3的连续调用也将它们一对一地推送到堆栈中
  • When the function f3 returns a value, it gets popped out of the stack and triggers the f2 function

    当函数f3返回一个值时,它将弹出堆栈并触发f2函数
  • And hence, the consecutive popping of functions f2 and f1 take place.

    因此,功能f2f1连续弹出。

Further Stack finds its application in different programming methods and techniques, some of them are:

进一步的堆栈可以在不同的编程方法和技术中找到其应用,其中一些是:

  • Tower of Hanoi

    河内塔
  • And Evaluation of prefix, infix and postfix expressions

    并计算前缀,中缀和后缀表达式
  • As well as String Reversal, etc

    以及字符串反转等

结论 (Conclusion)

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 ++中堆栈的概念。 对于任何问题,请随时在评论中提问。

参考资料 (References)

翻译自: https://www.journaldev.com/36000/stack-in-c-plus-plus

c9500堆叠配置

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值