#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<list>
#include<vector>
using namespace std;
//特点:先进后出
//运用:递归 汉诺塔 表达式计算
const int stack_init_size=100;//初始化大小
const int stack_increment=10; //增量大小
#define null NULL
typedef struct
{
int *base;//栈底
int *top;//栈顶
int stacksize; //当前的stack大小
} SqStack;
/**********构造一个空栈**********/
/** 初始化大小为100
*********************************/
bool InitStack(SqStack &st)
{
st.base=(int *)malloc(sizeof(int) * stack_init_size);//分配内存
if(!st.base)return false;//内存分配失败
st.top=st.base;
st.stacksize=stack_init_size;
return true;
}
/**********销毁栈**********/
/** 前提:栈存在
*********************************/
void DestoryStack(SqStack &st)
{
st.top=st.base;
free(st.base);
st.top=null;
st.base=null;
}
/**********清空栈**********/
/** 前提:stack存在
*********************************/
void ClearStack(SqStack &st)
{
st.top=st.base;
st.stacksize=0;
}
/**********判断栈是否为空**********/
/** 前提:栈存在
** 返回值:true表示栈为空,反之为false
** top==base?true:false
*********************************/
bool EmptyStack(SqStack st)
{
return st.base==st.top?true:false;
}
/**********获取栈的长度**********/
/** 前提:栈存在
** 返回值:栈长度
** top-base
*********************************/
int GetStackLength(SqStack st)
{
return st.top-st.base;
}
/**********获取栈的栈顶元素**********/
/** 前提:栈存在
** 返回值:用e返回栈顶元素
** 如果为空栈,那就返回-1 不为空栈且成功返回1
*********************************/
int GetTop(SqStack st,int &data)
{
if(st.top==st.base)return -1;
data=*(st.top-1);
return 1;
}
int GetTopStack(SqStack st)
{
if(st.top==st.base)return -1;
return *(st.top-1);
}
/**********出栈**********/
/** 前提:栈存在
** 判断是否为空栈
** 用data返回栈顶元素
** 如果为空栈就返回-1 反之返回1 表示成功
*********************************/
int Pop(SqStack &st,int &data)
{
if(st.base==st.top)return -1;//空栈 返回-1 表示错误
data=*(--st.top);//先改指针 再赋值
return 1;
}
/**********入栈**********/
/** 前提:栈存在
** 判断栈满,如果满了就增加容量
*********************************/
int Push(SqStack &st,int &data)
{
if(st.top-st.base>=st.stacksize)//栈满
{
st.base=(int *)realloc(st.base,(stack_increment+st.stacksize)*sizeof(int));
if(!st.base)return -1;
st.top=st.base+st.stacksize;//重定位
st.stacksize+=stack_increment;
}
*st.top++=data;//先赋值 在改指针
return 1;
}
/**********遍历栈**********/
/** 前提:栈存在
*********************************/
void PrintStack(SqStack st)
{
if(st.base==st.top)
{
exit(-1);
}
while(st.base!=st.top)
{
cout<<*(--st.top)<<" ";
}
cout<<endl;
}
int main()
{
SqStack st;
int n;
int data;
InitStack(st);
while(cin>>n,n)
{
Push(st,n);
}
PrintStack(st);
if(GetTopStack(st)==-1)cout<<"empty GetTopStack"<<endl;
else cout<<"GetTopStack = "<<GetTopStack(st)<<endl;
if(GetTop(st,data)==-1)cout<<"empty GetTop"<<endl;
else cout<<"GetTop = "<<data<<endl;
int length=GetStackLength(st);
while(length--)
{
int tmp=Pop(st,data);
if(tmp==-1)cout<<"empty Pop"<<endl;
else cout<<data<<" ";
}
cout<<endl;
cout<<"stack length = "<<GetStackLength(st)<<endl;
if(EmptyStack(st)==true)cout<<"empty stack"<<endl;
else cout<<"stack has item"<<endl;
ClearStack(st);
cout<<"stack length = "<<GetStackLength(st)<<endl;
DestoryStack(st);
return 0;
}
数据结构之队列基本实现
最新推荐文章于 2022-03-27 16:11:51 发布