要求写一个栈类,实现Push,Pop,GetTop 3个函数,下面为测试结果(提示:栈满不要求有追加存储空间的功能)

来源:百度知道 编辑:UC知道 时间:2024/06/04 12:01:18
输出结果为:
输入3个数:
1 2 3
当前栈顶元素为3
请按任意键继续……

一部分代码为:
#include<iostream>
#include<cassert>
using namespace std;
class SqStack
{
public:
SqStack();
SqStack(int size);
~SqStack();
public:
int GetTop();
void Push(int e);
void Pop();
private:
const int STACK_INIT_SIZE;
private:
//基指针
int *m_pBase;
//栈顶指针
int *m_pTop;
//当前分配的存储空间
int m_StackSize;
};

int main()
{
int num1,num2,num3;
SqStack sq(5);

cout << "输入个数:"<<endl;
cin>>num1>> num2>> num3;
sq.Push(num1);
sq.Push(num2);
sq.Push(num3);

cout <<"当前栈顶元素为"
<<sq.GetTop()
<< endl;

return 0;
}
请各位高手帮我把程序写下完整,谢谢

#include<iostream>
#include<cassert>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define ERROR -1;
using namespace std;
class SqStack
{
public:
SqStack();
SqStack(int size);
~SqStack();
public:
int GetTop();
void Push(int e);
void Pop();
//private:
//const int STACK_INIT_SIZE;
private:
//基指针
int *m_pBase;
//栈顶指针
int *m_pTop;
//当前分配的存储空间
int m_StackSize;
};

SqStack::SqStack()
{
m_pBase=new int[STACK_INIT_SIZE];
//m_pBase=(int*)malloc(STACK_INIT_SIZE*sizeof(int));
m_pTop=m_pBase;
m_StackSize=STACK_INIT_SIZE;
}

SqStack::SqStack(int size)
{
m_pBase=new int[size];
//m_pBase=(int*)malloc(size*sizeof(int));
m_pTop=m_pBase;
m_StackSize=size;
}

SqStack::~SqStack()
{
if(m_pTop!=m_pBase)