1、初始化栈
为栈分配空间,并且将top初始化为-1(表示栈中没有元素)。
bool InitStack(stack& s)// 1、初始化栈。
{
s.data = new ElemType[Maxsize];
s.top = -1; // 初始化栈顶指针
if(!s.data)
return false;
else
return true;
}
如果成功分配空间返回true,否则返回false。
2、清空栈
清空栈,将Top指针指向-1的初始化位置。
bool ClearStack(stack& s) // 清空栈,将Top指针指向-1的初始化位置。
{
s.top = -1; //将栈顶指针指向-1,遍历的时候到top就结束
if(s.top == -1)
return true;
else
return false;
}
3、栈判空
如果top等于-1,则表示栈为空,否则表示栈不为空。
bool StackEmpty(stack& s)// 判断是否为空
{
if(s.top==-1)
return true;
else
return false;
}
4、求栈长度
栈内元素个数为top+1;
int lengthstack(stack s)
{
return s.top + 1;
}
5、获取栈顶元素
读取栈顶元素,若栈未空,将栈顶元素赋值给x,并将x返回。
ElemType Top(stack s)// 读取栈顶元素,若栈未空,将栈顶元素赋值给x
{
if(StackEmpty(s)) return false;
int x = s.data[s.top];
return x;
}
6、插入元素
进栈,若栈未满,则将新元素插入到S.top+1的位置。
bool Push(stack& s,ElemType x)// 进栈,若栈未满,则将新元素插入到S.top+1的位置。
{
if(s.top==Maxsize-1)// 栈顶指针指向最后一个,栈满,报错,因为数组下标从0开始,数组下标最大值为Max-1
return false;
s.data[++s.top] = x;// 应熟练掌握++i和i++的区别,这里因为top指针指向的是栈目前最后一个元素,需要将指针移到下一个再装填新元素,否则会覆盖,所以使用++S.top。
return true;
}
7、删除元素
读取栈顶元素,若栈未空,将栈顶元素赋值给x,并将x返回。
ElemType Top(stack s)// 读取栈顶元素,若栈未空,将栈顶元素赋值给x
{
if(StackEmpty(s)) return false;
int x = s.data[s.top];
return x;
}
8、输出所有元素
void show(stack s)
{
for(int i = 0; i <= s.top; i ++) cout << s.data[i] << " ";
cout << endl;
}
9、销毁栈
销毁栈,并释放栈所用空间。
bool DestroyStack(stack& s)
{
free(s.data);
s.data = nullptr;
s.top = 0;
if(s.data == nullptr)
return true;
else
return false;
}
完整代码
#include<iostream>
#include<cstdlib>
using namespace std;
typedef int ElemType;
const int Maxsize = 10;
typedef struct stack{
ElemType* data;
int top;
}stack;
void e()
{
cout << "1.初始化栈" << endl;
cout << "2.销毁栈" << endl;
cout << "3.清空栈" << endl;
cout << "4.栈判空" << endl;
cout << "5.求栈长度" << endl;
cout << "6.获取栈顶元素" << endl;
cout << "7.插入一个 元素" << endl;
cout << "8.删除一个元素" << endl;
cout << "9.输出所有元素" << endl;
cout << "10.进制转换" << endl;
}
bool InitStack(stack& s)// 1、初始化栈。
{
s.data = new ElemType[Maxsize];
s.top = -1; // 初始化栈顶指针
if(!s.data)
return false;
else
return true;
}
bool StackEmpty(stack& s)// 判断是否为空
{
if(s.top==-1)
return true;
else
return false;
}
bool Push(stack& s,ElemType x)// 进栈,若栈未满,则将新元素插入到S.top+1的位置。
{
if(s.top==Maxsize-1)// 栈顶指针指向最后一个,栈满,报错,因为数组下标从0开始,数组下标最大值为Max-1
return false;
s.data[++s.top] = x;// 应熟练掌握++i和i++的区别,这里因为top指针指向的是栈目前最后一个元素,需要将指针移到下一个再装填新元素,否则会覆盖,所以使用++S.top。
return true;
}
bool Pop(stack& s)// 出栈,若栈未空,则将栈顶S.top元素赋值给x,并将Top指针-1。
{
if(StackEmpty(s)) return false;
s.top--;
return true;
}
ElemType Top(stack s)// 读取栈顶元素,若栈未空,将栈顶元素赋值给x
{
if(StackEmpty(s)) return false;
int x = s.data[s.top];
return x;
}
bool DestroyStack(stack& s) // 销毁栈,并释放栈所用空间。
{
free(s.data);
s.data = nullptr;
s.top = 0;
if(s.data == nullptr)
return true;
else
return false;
}
bool ClearStack(stack& s) // 清空栈,将Top指针指向-1的初始化位置。
{
s.top = -1; //将栈顶指针指向-1,遍历的时候到top就结束
if(s.top == -1)
return true;
else
return false;
}
void show(stack s)
{
for(int i = 0; i <= s.top; i ++) cout << s.data[i] << " ";
cout << endl;
}
int main()
{
stack s;
int op;
bool flag = true;
while(true)
{
cin >> op;
if(op == 1){
if(InitStack(s))
{
flag = false;
cout << "初始化成功" << endl;
}
else cout << "初始化失败" << endl;
}else if(op == 2){
if(flag)
{
cout << "栈还未初始化" << endl;
continue;
}
if(DestroyStack(s))
{
flag= true;
cout << "销毁成功" << endl;
}
else cout << "销毁失败" << endl;
}else if(op == 3){
if(flag)
{
cout << "栈已经被销毁" << endl;
continue;
}
if(ClearStack(s)) cout << "清空成功" << endl;
else cout << "清空失败" << endl;
}else if(op == 4){
if(flag)
{
cout << "栈已经被销毁" << endl;
continue;
}
if(StackEmpty(s)) cout << "栈为空" << endl;
else cout << "栈不为空" << endl;
}else if(op == 5){
if(flag)
{
cout << "栈已经被销毁" << endl;
continue;
}
cout << "栈的长度为" << s.top + 1 << endl;
}else if(op == 6){
if(flag)
{
cout << "栈已经被销毁" << endl;
continue;
}
if(Top(s)) cout << "栈顶元素为:" << Top(s) << endl;
else cout << "栈为空" << endl;
}else if(op == 7){
if(flag)
{
cout << "栈已经被销毁" << endl;
continue;
}
ElemType x;
cin >> x;
if(Push(s,x)) cout << "插入成功" << endl;
else cout << "插入失败" << endl;
}else if(op == 8){
if(flag)
{
cout << "栈已经被销毁" << endl;
continue;
}
if(Pop(s)) cout << "删除元素成功" << endl;
else cout << "删除元素失败" << endl;
}else if(op == 9){
if(flag)
{
cout << "栈已经被销毁" << endl;
continue;
}
show(s);
}else if(op == -1)
break;
}
cout << "程序已退出" << endl;
return 0;
}