利用栈对数据进行逆置操作

#include "stdafx.h"
#include<iostream>
using namespace std;
class stack                     //利用栈将元素逆置  
{
private:
int msize;    //栈中可存放的最多的元素个数
int top;      //栈顶位置
int *st;      //存放栈元素的数组
public:
stack(int i)      //创建一个长度为i的栈。
{
msize = i;
top = -1;        //此时top=-1,说明为空栈
st = new int[i];   //存放栈元素的数组
}
~stack()   //析构函数
{
delete[]st;
}
bool isempty()    //判断是否为空栈
{
if (top == -1)    //通过判断栈顶元素确定
{
cout << "该栈为空栈" << endl;
return true;
}
return false;
}
bool push()      //入栈操作
{
while(top < msize-1)    //利用while循环结构,将元素输入数据。
{
top++;
   cin >> st[top];
}
return true;
}
bool isfull()    //判断栈满
{
if (top == msize-1)    通过对栈顶元素的定位实现
{
cout << "栈满" << endl;
return true;
}
return false;
}
bool pop()    //出栈操作 
{
while (top > -1)            //利用入栈的思想,出栈也可以通过while循环实现
{
cout << st[top] << " ";
top--;
}
return true;
}
};
int main()
{
stack B(7);
cout << "判断栈是否为空;" << endl;
B.isempty();
cout << "请输入队列中元素;" << endl;
B.push();
cout << "判断是否栈满:" << endl;
B.isfull();
cout << "逆置输出元素:" << endl;
B.pop();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/CAUC_yangxiao/article/details/78309947