使用 Push、Pop 等在 C++ 中创建堆栈

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20386858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:06:03  来源:igfitidea点击:

Create Stack in C++ with Push, Pop etc

c++liststack

提问by chrissik

We are trying to create a C++ program that works like a stack. The following instructions are given: There should be several methods:

我们正在尝试创建一个像堆栈一样工作的 C++ 程序。给出了以下说明: 应该有几种方法:

We have to use this definition of the StackElement:

我们必须使用 的这个定义StackElement

struct StackElement {
int digit;
StackElement* predecessor;
};

The problem now is how to create the push-function without an next parameter as there was with ordinary lists. The same for the pop function. We accomplished to get push()create a new StackElement and pop()delete the new Element, but with 2 new Elements, those methods aren't working properly.

现在的问题是如何创建push没有 next 参数的- 函数,就像普通列表一样。pop 函数也是如此。我们完成了push()创建新 StackElement 并pop()删除新 Element 的工作,但是对于 2 个新元素,这些方法无法正常工作。

Global parameter stack0:

全局参数stack0

StackElement *stack0 = new StackElement;

This is the code for the push-function:

这是push-函数的代码:

StackElement push(int z){
    StackElement *stack1 = new StackElement;
    stack1->digit = z;
    stack1->predecessor = NULL;
    stack0->predecessor = stack1;
    stack1 = stack0;

This is the pop()method:

这是pop()方法:

void pop(){
    StackElement *stack1 = new StackElement;
    if (stack0!=NULL){
    stack1->digit = 0;
    stack0->predecessor = NULL; //predecessor = NULL;
    }
}

And finally the mainmethod:

最后是main方法:

int main()
{
    int z;
    create();

    cout << "Which number to the stack?" << endl;
    cin >> z;

    push(z);
    pop();
    print();

    return 0;
}

We thought about creating a new StackElement, that would work as a 'pseudo'-StackElement and would always stay on top so that the 'real' element on the top would always be the predecessor of the pseudo-Element - but we think this would be against how the stack should work.

我们考虑过创建一个新的 StackElement,它将作为“伪”-StackElement 工作并且始终保持在顶部,因此顶部的“真实”元素将始终是伪元素的前身——但我们认为这会反对堆栈应该如何工作。

So do you guys have any clues on how to proceed? Is there something we are just missing?

那么你们有没有关于如何继续的线索?有什么是我们缺少的吗?

回答by user1810087

1st) Why not using std::stack?

1) 为什么不使用std::stack

2nd) Stack should be LIFO. That means your stack0 should always be the newest one... This leads to:

2) 堆栈应该是LIFO。这意味着您的 stack0 应该始终是最新的……这导致:

StackElement* stack0 = NULL;       // until C++11
// StackElement* stack0 = nullptr; // since C++11

void push(int z) {
  StackElement *stack1 = new StackElement;
  stack1->digit = z;
  stack1->predecessor = stack0;
  stack0 = stack1;
}

void pop() {
  if(stack0) {
    StackElement *tmp = stack0;
    stack0 = stack0->predecessor;
    delete tmp;
  }
}

You don't need the allocate a new one in the pop function. This will end in a huge memory leak.

你不需要在 pop 函数中分配一个新的。这将导致巨大的内存泄漏。

What you meant by pseudo is called root element and is sometimes used in sequences. But not necessary here, since stack0 is the root or in this case the end (the first element and the end).

你所说的伪是指根元素,有时在序列中使用。但在这里不是必需的,因为 stack0 是根或在这种情况下是结尾(第一个元素和结尾)。

A better approach would be to encapsulate it in a class as ryrich mentioned in his answer. And the best approach would be using the C++-given std::stack.

更好的方法是将它封装在一个类中,就像 ryrich 在他的回答中提到的那样。最好的方法是使用 C++ 给定的std::stack

回答by ryrich

You're on the right track here. To keep track of your elements, you need a class. Something like:

你在这里走在正确的轨道上。为了跟踪你的元素,你需要一个类。就像是:

class Stack{
private:
  StackElement *last_data, *first_data;

public:
  Stack():last_data(NULL), first_data(NULL){}  
  void push(int digit);
  void pop();
};

Have the push/pop functions part of the Stackclass. For example, push:

具有类的推送/弹出功能部分Stack。例如,推:

void Stack::push(int digit)
{
  StackElement *p=new StackElement();
  p->digit = digit;
  if(last_data)
    p->predecessor=last_data;
  else // empty stack
  {
    p->predecessor=NULL;
    first_data = p;
  }
  last_data=p;
}

Hope this helps.

希望这可以帮助。

Edit: I'll add pop function for completeness:

编辑:为了完整起见,我将添加 pop 功能:

void Stack::pop()
{
  if (last_data)
  {
    StackElement *tp = last_data;
    last_data = last_data->predecessor;
    delete tp;
  }
}

回答by farrukh masud

if the stack is full and we try to put something in it then it will give us the error of stack overflow

如果堆栈已满并且我们尝试在其中放入一些东西,那么它会给我们堆栈溢出的错误

void IntStack::push()
{
   clrscr();
   int num;
   if(top>=stackSize)
        cout<<"stack Overflow"<<endl;
   else
   {
    cout<<"Enter Number=";
    cin>>num;
    top++;
      stackArray[top]=num;
   }
}