c++ - 使用 3 个堆栈实现排序。 (段错误 - 核心转储)

标签 c++

我在运行时遇到段错误 - 核心转储。 我想弄清楚如何摆脱段错误。 这是类作业。 编辑:在下面添加了输入和输出。

//client.cpp

#include <iostream>
#include "stack.h"

using namespace std;

int main(void){
    stack unsorted;
    stack stack1;
    stack stack2;
    stack stack3;
    int const MAX_VALUES = 5;
    int input;

    cout << "Please input "<< MAX_VALUES << " unique integers.\n";
    cout << "Click the ENTER key after each integer inputted." <<endl;

    for (int i = 0; i < MAX_VALUES; i++)
    {
        cin >> input;
        stack1.Push(input);
        unsorted.Push(input);
    }

    cout << "Unsorted Stack: " <<endl;
    for (int i = 0; i < MAX_VALUES; i++)
    {
        cout<<unsorted.Pop()<<endl;

    }

    cout << "Sorted Stack: "<<endl;


    while((!stack1.IsEmpty())&&(!stack3.IsEmpty())){
        if ((stack1.IsEmpty())&&(!stack3.IsEmpty()))
        {
            stack2.Push(stack3.Pop());
        }
        if (stack2.Top() > stack1.Top())
        {
            stack3.Push(stack2.Pop());
        }
        if (stack3.Top() < stack1.Top())
        {
            stack2.Push(stack3.Pop());
        }
        else
        {
            stack2.Push(stack1.Pop());
        }
    }

    while (!stack2.IsEmpty()){
        cout << stack2.Pop() << endl;
    }

}

//stack.h

#include <cstddef>

struct node;

class stack
{
public:
    stack();   
    ~stack();  

    bool IsFull();
    bool IsEmpty();
    void Push(int input);
    int Pop();
    int Top();

private:
    node* top;
    int const MAX_VALUES = 5;
    int count;
};

//stack.cpp

#include "stack.h"

struct node
{
    int input;
    node* next;
};

stack::stack()
{
    top = NULL;
    count = 0;
}

bool stack::IsFull()
{
    if (MAX_VALUES > count)
    {
        return false;
    }
    return true;
}
bool stack::IsEmpty(){
    if (top == NULL){
        return true;
    }
    else{
        return false;
    }
}

void stack::Push(int num)
{
    if(IsFull() == false)
    {
        node* newNode = new node;
        newNode->input = num;
        newNode->next = top;
        top = newNode;
        count ++;
    }
}

int stack::Top()
{
    int topval;
    topval = top->input;    
    return topval;
}

int stack::Pop()
{
    int Popped;
    if (top != NULL)
    {
        node* temp = top;
        top = top->next;
        Popped = temp->input;
        delete temp;
        return Popped;
    }
    count--;
}


stack::~stack()
{
    node* current = top;

    while( top != NULL)
    {
        node* next = current->next;
        delete current;
        current = next;
    }
    top = NULL;
}

输入: 请输入 5 个唯一的整数。 输入每个整数后单击 ENTER 键。 7 1个 56 67 8

输出:

未排序的堆栈: 8个 67 56 1个 7 排序堆栈: 段错误(核心转储)

最佳答案

有几个问题。

首先,您的Pop:

int stack::Pop()
{
    int Popped;
    if (top != NULL)
    {
        node* temp = top;
        top = top->next;
        Popped = temp->input;
        delete temp;
        return Popped;
    }
    count--;
}

当堆栈为空时递减计数,但如果为空则不返回任何内容。
你需要更多这样的东西:

int stack::Pop()
{
    int Popped = -1; // Make sure the return value is well-defined
    if (!IsEmpty())
    {
        node* temp = top;
        top = top->next;
        Popped = temp->input;
        delete temp;
        count--;   // Only decrement if we actually popped something.
    }
    return Popped;
}

和析构函数:

stack::~stack()
{
    node* current = top;

    while( top != NULL)
    {
        node* next = current->next;
        delete current;
        current = next;
    }
    top = NULL;
}

循环永远不会停止,因为你有错误的终止条件 - 除非堆栈为空,top 永远不会变为 NULL
应该是

stack::~stack()
{
    node* current = top;
    while (current != NULL)
    {
        node* next = current->next;
        delete current;
        current = next;
    }
    top = NULL;
}

main 中的循环是这样开始的(我删除了一些不必要的括号):

while (!stack1.IsEmpty() && !stack3.IsEmpty()) {
    if (stack1.IsEmpty() && !stack3.IsEmpty())

如果 while 条件为 if 条件永远不会为真 - stack1 不能既为空又不为空。

旁注:将堆栈实现为链表时设置大小限制有点奇怪。

关于c++ - 使用 3 个堆栈实现排序。 (段错误 - 核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27011993/

相关文章:

c++ - 命名空间中变量的默认/自定义值

c++ - N 位 x 包含 L 个 1

c++ - C++11 中的定时器,带 chrono : from nanoseconds to milliseconds

c++ - 在 union 中访问结构

c++ - 不支持非平凡的指定初始化器

C++ Boost 如何将 C++ 对象序列化到共享内存并在另一个应用程序中重新创建它

c++ - std::unique_lock::_Owns 数据成员不是原子的?

android - 是否可以通过编译的二进制文件在 Android 中生成本地缓冲区溢出以获得 root 访问权限?

c++ - QT creator如何让一个UI生成的类继承其他类?

c++ - 有什么更好的方法来避免 do-while(0);在 C++ 中破解?