C++ 使用递归对堆栈进行排序
因此,我试图对基于数组的堆栈进行排序,但我一直遇到以下问题:a.out(11849,0x115ce05c0)malloc:*对象0x7fc181402a80的错误:未分配被释放的指针 a、 输出(11849,0x115ce05c0)malloc:在malloc\u error\u break中设置断点以进行调试。 这是我的课程说明书C++ 使用递归对堆栈进行排序,c++,sorting,recursion,stack,C++,Sorting,Recursion,Stack,因此,我试图对基于数组的堆栈进行排序,但我一直遇到以下问题:a.out(11849,0x115ce05c0)malloc:*对象0x7fc181402a80的错误:未分配被释放的指针 a、 输出(11849,0x115ce05c0)malloc:在malloc\u error\u break中设置断点以进行调试。 这是我的课程说明书 StackType::StackType() { maxLength = 10; Top = -1;
StackType::StackType()
{
maxLength = 10;
Top = -1;
numArray = new int[maxLength];
}
//checks if stack is full
bool StackType::isFull()
{
if(Top == maxLength -1)
return true;
else
return false;
}
//checks if stack is empty
bool StackType::isEmpty()
{
if(Top == -1)
return true;
else
return false;
}
//insert numbers into stack
void StackType::push(int num)
{
if(isFull())
throw FullStack();
Top++;
numArray[Top] = num;
}
//deletes numbers in stack
void StackType::pop()
{
if(isEmpty())
throw EmptyStack();
Top--;
}
//returns number at the top pf the stack
int StackType::top()
{
if(isEmpty())
throw EmptyStack();
return numArray[Top];
}
//prints stack
void StackType::printStack()
{
if(isEmpty())
cout << "Stack is empty\n";
else
{
int tempIndex = 0;
//top is the last position of array
cout << "Printing stack:\n";
while(tempIndex <= Top)
{
cout << numArray[tempIndex] << endl;
tempIndex++;
}
}
}
//deletes array
StackType::~StackType()
{
delete [] numArray;
}
StackType::StackType()
{
最大长度=10;
Top=-1;
numArray=新整数[maxLength];
}
//检查堆栈是否已满
bool StackType::isFull()
{
if(Top==maxLength-1)
返回true;
其他的
返回false;
}
//检查堆栈是否为空
bool StackType::isEmpty()
{
如果(顶部==-1)
返回true;
其他的
返回false;
}
//将数字插入堆栈
void StackType::push(int num)
{
如果(isFull())
抛出FullStack();
Top++;
numArray[Top]=num;
}
//删除堆栈中的数字
void StackType::pop()
{
if(isEmpty())
抛出空包();
顶部--;
}
//返回堆栈顶部的数字
int StackType::top()
{
if(isEmpty())
抛出空包();
返回numArray[顶部];
}
//打印堆栈
void StackType::printStack()
{
if(isEmpty())
cout您通过值传递StackType
,这会导致复制它,但您没有提供复制构造函数。因此,您最终得到两个StackType
实例,它们都指向同一数组,并且都试图删除它。第一个成功,另一个触发未定义行为
换句话说,你的类违反了即使我试图通过引用传递它,它也会给我同样的错误:/所以我有点困惑,然后在你的程序中还有其他地方你可以复制堆栈类型
。我忍不住注意到你没有显示
#include "StackType.h"
#include <iostream>
using namespace std;
StackType sortStack(StackType stack, StackType &tempStack);
int main ()
{
//read 10 ints into stack
//output them first
StackType currentStack, orderedStack;
currentStack.push(21);
currentStack.push(49);
currentStack.push(7);
currentStack.push(81);
currentStack.push(5);
currentStack.push(17);
currentStack.push(2);
currentStack.push(26);
currentStack.push(42);
currentStack.push(58);
currentStack.printStack();
cout << "The following is the sorted stack\n";
sortStack(currentStack, orderedStack);
//implement recursion here
//output stack again
return 0;
}
StackType sortStack(StackType stack, StackType &tempStack)
{
int current;
if(stack.isEmpty() && tempStack.isFull()) {
cout << "did it \n";
return tempStack;
}
else
{
current = stack.top();
stack.pop();
if(tempStack.isEmpty())
{
cout << "here1 \n";
tempStack.push(current);
return sortStack(stack, tempStack);
}
else
{
if(current < tempStack.top())
{
stack.push(tempStack.top());
tempStack.pop();
tempStack.push(current);
cout << "here2 \n";
return sortStack(stack, tempStack);
}
else
{
//when current is greater than temp.top
tempStack.push(current);
cout << "here3 \n";
return sortStack(stack, tempStack);
}
}
}
}