簡體   English   中英

使用3個堆棧實現排序。 (分段故障-核心轉儲)

[英]Implementing A Sort Using 3 Stacks. (Segmentation Fault - Core Dump)

我在運行時遇到分段錯誤-核心轉儲。 我試圖弄清楚如何擺脫細分錯誤。 這是課堂作業。 編輯:下面添加了輸入和輸出。

//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--;
}

當堆棧為空時,該減量才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,則if條件永遠不會為真stack1不能同時為空和不為空。

旁注:在將堆棧實現為鏈表時具有大小限制有點奇怪。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM