#include <iostream>
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef int ElemType;
typedef struct {
ElemType* top;
ElemType* base;
int stacksize;
}SequenceStack;
//定义结构体
int InitStack(SequenceStack& S) {
S.base = new ElemType[MAXSIZE];
if (!S.base) return ERROR;
S.top = S.base;
S.stacksize = MAXSIZE;
return OK;
}
//初始化栈
int Push(SequenceStack& S, ElemType e) {
if (S.top - S.base == S.stacksize) return ERROR;
*S.top++ = e;
return OK;
}
//压栈
int GetHead(SequenceStack& S) {
if (S.top != S.base) return *(S.top - 1);
else return ERROR;
}
//取栈顶元素
int Pop(SequenceStack& S, ElemType& e) {
if (S.top == S.base) return ERROR;
e = *--S.top;
return OK;
}
//出栈
int StackEpty(SequenceStack& S) {
if (S.base == S.top) {
return OK;
}
else{
return ERROR;
}
}//判断栈是否为空
int StackLength(SequenceStack S) {
int i = 1;
if (S.base == S.top) {
return 0;
}
else {
while (--S.top != S.base) {
i++;
}
return i;
}
}//求栈的长度
int Pop_Each_Elem(SequenceStack& S) {
int t=0;
if (S.base == S.top) return ERROR;
while (--S.top != S.base || S.base==S.top) {
//由于底指针也存储元素,所以加上头和底相同的状况
t = *S.top;
std::cout << t << std::endl;
if (S.base == S.top) return OK;
//前面已经输出了头和底指针相同的情况,所以退出
}
return OK;
}//将栈中元素依次出栈并输出
int print_TTE(SequenceStack S, ElemType i) {
while (i >= 0) {
std::cout << *--S.top;
i--;
}
return OK;
}//此函数在转化进制时调用
int Ten_to_eight(SequenceStack& S,ElemType n) {
int t,i=1;
//这里使用直接除8法
t = n % 8;
*S.top++ = t;
n = n / 8;
while (n > 8 ) {
if (n > 8) {
t = n % 8;
*S.top++ = t;
i++;
n = n / 8;
}
else {
break;
}
}
*S.top++ = n;
//由于在写函数形参时传入的是参数的地址,所以如果直接输出结果会导致头指针倒退
//所以这里重新调用一个没有传入地址的函数来输出结果
print_TTE(S, i);
std::cout << std::endl;
return OK;
}
int main() {
int status;
SequenceStack ss;
status = InitStack(ss);
if (!status) {
std::cout << "初始化错误。" << std::endl;
std::exit(0);
}//判断栈是否初始化成功,不成功直接退出程序
while(true){
std::cout << "--------------------------------" << std::endl;
std::cout << "0、退出程序。" << std::endl;
std::cout << "1、判断栈是否为空。" << std::endl;
std::cout << "2、获取栈顶元素。" << std::endl;
std::cout << "3、获取栈的长度。" << std::endl;
std::cout << "4、将栈中元素依次出栈并输出。" << std::endl;
std::cout << "5、出栈。" << std::endl;
std::cout << "6、压栈。" << std::endl;
std::cout << "7、实现十进制转为八进制。" << std::endl;
std::cout << "--------------------------------" << std::endl;
int status,num,e,len,temp,n;
std::cout << "你的选择:";
std::cin >> num;
switch (num)
{
case 0://退出程序
break;
case 1://判断栈是否为空
status = StackEpty(ss);
if (!status) {
std::cout << "当前栈不为空!" << std::endl;
continue;
}
else {
std::cout << "当前栈为空。" << std::endl;
continue;
}
case 2://获取栈顶元素
std::cout << "当前的头元素" << GetHead(ss) << std::endl;
continue;
case 3://获取栈的长度
len = StackLength(ss);
if (!len) {
std::cout << "当前栈的长度为0。" << std::endl;
continue;
}
else {
std::cout << "当前栈的长度为" << len << "。" << std::endl;
continue;
}
case 4://将栈中元素依次出栈并输出
if (!Pop_Each_Elem(ss)) {
std::cout << "栈空!请先压栈!"<< std::endl;
continue;
}
continue;
case 5://出栈
status = Pop(ss, temp);
if (!status) {
std::cout << "当前栈为空!" << std::endl;
continue;
}
else {
std::cout << "出栈成功," << "元素为" << temp << std::endl;
continue;
}
case 6://出栈
std::cout << "输入你要入栈的数:";
std::cin >> e;
status = Push(ss, e);
if (!status) {
std::cout << "栈满!" << std::endl;
continue;
}
else {
std::cout << "入栈成功!" << std::endl;
continue;
}
case 7://实现十进制转为八进制
std::cout << "所要转化的十进制:";
std::cin >> n;
Ten_to_eight(ss, n);
continue;
default:
break;
}
break;
}
}
标签:SequenceStack,功能,顺序,return,实现,top,int,base,ERROR
From: https://www.cnblogs.com/qianyuzz/p/17059917.html