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