用顺序栈实现入栈出栈倒序输出,以下是我的程序,总有一个错误,请大师指点迷津,小生感激不尽

#include<iostream>
const int MaxStackSize 10;
class Stack
{
DataType StackList[MaxStackSize];
int top;
public:
stack();
bool IsEmpty();
bool IsFull();
DataType GetTop();
void Push(DataType x);
DataType Pop();
void Clear();
};
Stack::stack()
{
top=-1;
}
bool Stack::IsEmpty()
{
if(top==-1)
return true;
else
return false;
}
bool Stack::IsFull()
{
if(top==MaxStackSize-1)
return true;
else
return false;
}
DataType Stack::GetTop()
{
if(IsEmpty())
{
cout<<"栈空!"<<endl;
exit(0);
}
return StackList[top];
}
void Stack::Pop()
{
if(IsEmpty())
{
cout<<"栈空!"<<endl;
exit(0);
}
return StackList[top--];
}
void Stack::Clear()
{
top=-1;
};
int main()
{
Stack s;
int a[10];
int i;
cout<<"请输入10个数:";
for(i=0;i<10;i++)
{
cin>>a[i];
s.Push(a[i]);
}
for(i=0;i<10;i++)
{
s.Top(a[i]);
cout<<a[i]<<" ";
s.Pop();
}
return 0;
}

你贴上来的程序根本编译不过啊,针对性的修改了下,修改部分在如下的代码中用注释标示了出来,请注意跟你的程序对比下~~:

#include<iostream>
#include<cstdlib>
using namespace std;//此处定义名空间
typedef int DataType;//此处定义DataType为int,你的主程序中使用了栈来存取int型元素
const int MaxStackSize=10;//此处添加'='号
class Stack
{
    DataType StackList[MaxStackSize];
    int top;
public:
    Stack();//此处构造函数名首字母应大写
    bool IsEmpty();
    bool IsFull();
    DataType GetTop();
    void Push(DataType x);
    DataType Pop();
    void Clear();
};
Stack::Stack()//此处构造函数名首字母应大写
{
    top=-1;
}
bool Stack::IsEmpty()
{
    if(top==-1)  return true;
    else  return false;
}
bool Stack::IsFull()
{
    if(top==MaxStackSize-1)  return true;
    else  return false;
}
DataType Stack::GetTop()
{
    if(IsEmpty())
    {
        cout<<"栈空!"<<endl;
        exit(0);
    }
    return StackList[top];
}
//遗漏成员函数Push的实现
void Stack::Push(DataType x)
{
    if(IsFull())
    {
        cout<<"栈满!"<<endl;
        exit(0);
    }
    StackList[++top]=x;
}

DataType Stack::Pop()//此处返回值类型应与声明的一致
{
    if(IsEmpty())
    {
        cout<<"栈空!"<<endl;
        exit(0);
    }
    return StackList[top--];
}
void Stack::Clear()
{
    top=-1;
};
int main()
{
    Stack s;
    int a[10];
    int i;
    cout<<"请输入10个数:";
    for(i=0; i<10; i++)
    {
        cin>>a[i];
        s.Push(a[i]);
    }
    for(i=0; i<10; i++)
    {
        a[i]=s.GetTop();//s.Top(a[i]); //此处函数调用使用了错误的函数名,猜测你要使用GetTop函数
        cout<<a[i]<<" ";
        s.Pop();
    }
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
相似回答