【问题标题】:C++ - Reverse function in a template class stack implementationC++ - 模板类堆栈实现中的反向函数
【发布时间】:2014-09-01 12:07:59
【问题描述】:

这是我使用带有结构类型节点和类类型堆栈的模板的堆栈实现:


堆栈.h
#ifndef STACK_H_
#define STACK_H_

#include <cstdlib>
#include <iostream>
#include <cassert>

using namespace std;

template <class t>
struct node{
    t data;
    node<t>* next;
};

template <class t>
class stack
{
public:
    stack();
    ~stack();
    bool isEmpty(){ return (top_ptr=NULL);};
    void push(const t&);
    void pop();
    t top() const;
    void reverse();
    void clear();
    void print();
private:
    node<t>* top_ptr;
};

template <class t>
stack<t>::stack()
{
    top_ptr=NULL;
}

template <class t>
stack<t>::~stack()
{
    while(top_ptr != NULL) pop();
}

template <class t>
void stack<t>::push(const t& source)
{
    node<t>* new_node = new node<t>;
    new_node->data = source;
    new_node->next = top_ptr;
    top_ptr = new_node;
    cout << "Inserito!" << endl;
}

template <class t>
void stack<t>::pop()
{
    node<t>* remove = top_ptr;
    top_ptr = top_ptr->next;
    delete remove;
    cout << "Rimosso!" << endl;
}

template <class t>
t stack<t>::top() const
{
    assert(top_ptr != NULL);
    return top_ptr->data;
}

template <class t>
void stack<t>::clear()
{
    node<t>* temp;
    while(top_ptr != NULL)
    {
        temp = top_ptr;
        top_ptr = top_ptr->next;
        delete temp;
    }
    cout << "Clear completato!" << endl;
}

template <class t>
void stack<t>::reverse()
{
    stack<t> new_stack;
    while(top_ptr != NULL)
    {
        new_stack.push(top_ptr->data);
        pop();
    }
    cout << "Reverse completato!" << endl;
}

template <class t>
void stack<t>::print()
{
    node<t>* ptr = top_ptr;
    while(ptr!=NULL)
    {
        cout << " " << ptr->data << endl;
        ptr = ptr->next;
    }
}

#endif /* STACK_H_ */


这是 main.cpp:
#include "stack.h"

int main()
{
    stack<int> stackino;
    for(int i = 0; i<10; i++) stackino.push(i);
    stackino.pop();
    cout << "top(): " << stackino.top() << endl;
    stackino.print();
    cout << "Invoco clear()" << endl;
    stackino.clear();
    cout << "Stackino dopo clear():" << endl;
    stackino.print();
    cout << "Invoco reverse()" << endl;
    stackino.reverse();
    cout << "Stackino dopo reverse()" << endl;
    stackino.print();

    cout << "FINE!" << endl;
    return 0;
}


问题是导致程序崩溃的reverse(),我猜“top_ptr = new_stack.top_ptr”是错误的,但它可以编译和执行,但会崩溃。有人可以帮我纠正这个吗?

【问题讨论】:

  • 你的 isEmpty 函数不正确,首先...
  • 要扩展上述评论top_ptr=NULL 将设置top_ptrNULL (BAD) 并返回false,将其更改为==
  • 无法复制。在 Clang、GCC 和 VC++ 下运行良好。必须是未定义的行为或代码中的其他位置。
  • @djanthony93 “崩溃”是什么意思?它实际上是段错误还是类似的东西?

标签: c++ function stack reverse


【解决方案1】:

我想我理解这个问题。让我知道我是否解释错了。 当您执行top_ptr = new_stack.top_ptr 时,您将临时堆栈的顶部作为您的堆栈。 问题是,当该临时堆栈被破坏时,它仍然具有相同的top_ptr,并删除了与其关联的所有内存。这会让你的真实筹码变得糟糕 top_ptr

我建议尝试:

top_ptr = new_stack.top_ptr;
new_stack.top_ptr = NULL;

这样它就不会清除您的堆栈,并给您留下一个错误的指针。 希望这有效吗?

【讨论】:

  • 它可以编译,但是当我尝试 print() 我的堆栈时,它什么也没打印:(
  • 根据您的 Main() 方法,您不是在执行相反操作之前完全清除堆栈吗?
猜你喜欢
  • 1970-01-01
  • 2021-03-18
  • 1970-01-01
  • 2023-03-24
  • 2016-09-14
  • 2010-11-26
  • 2010-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多