使用3个堆栈实现排序。 (分段故障 - 核心转储)

时间:2014-11-19 08:19:52

标签: 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 排序堆栈: 分段错误(核心转储)

1 个答案:

答案 0 :(得分:1)

有几个问题。

首先,您的Pop

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

当堆栈为空时递减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())

如果if条件为while条件,则stack1条件永远不会为真 - {{1}}不能为空且不为空。

旁注:当您将堆栈实现为链接列表时,具有大小限制有点奇怪。