栈一般有两种实现的方式,链表和数组,链表的实现比较常见,这里主要是用动态数组去实现,可以让用户在堆上自由分配内存指定栈的容量。
为了便于用户的操作,需要对数组有两个附加信息,一个是数组最后一个元素的下标,记作TopOfStack,去模拟栈顶;另一个是数组的容量capacity,这两个附加元素和数组一起组成一个结构体,如下所示:
struct StackRecord
{
int capacity; //栈的容量
int TopOfStack; //栈顶下标
int *Array; //指针指向分配的动态数组
};
{
int capacity; //栈的容量
int TopOfStack; //栈顶下标
int *Array; //指针指向分配的动态数组
};
具体的代码如下:
#include <iostream>
#include <stdlib.h>
using namespace std;
struct StackRecord; //结构体存储动态数组信息
typedef struct StackRecord *Stack; //Stack为结构体指针
Stack CreateStack(int MaxSize);
void DisposeStack(Stack S); //释放栈函数
void MakeEmpty(Stack S); //清空栈
int IsEmpty(Stack S); //判断空
int IsFull(Stack S); //判断满
void Push(int target, Stack S);
int Top(Stack S);
void Pop(Stack S);
void PrintStack(Stack S); //打印栈
struct StackRecord
{
int capacity; //栈的容量
int TopOfStack; //栈顶
int *Array; //指针指向分配的动态数组
};
/*创建栈*/
Stack CreateStack(int MaxSize)
{
Stack S;
S = (Stack)malloc(sizeof( struct StackRecord));
if(S == NULL)
cout<<"Out Of Space!"<<endl;
S->Array = (int *)malloc(sizeof(int)*MaxSize); //动态分配数组
if(S->Array == NULL)
cout<<"Out Of Space!"<<endl;
S->capacity = MaxSize;
MakeEmpty(S); //让TopOfStack置-1,代表栈为空
return S;
}
/*释放栈*/
void DisposeStack(Stack S)
{
if(S!=NULL){
free(S->Array); //先释放数组
free(S); //再释放结构体
}
}
/*清空栈*/
void MakeEmpty(Stack S)
{
S->TopOfStack = -1; //为-1表示栈空
}
/*判断空栈*/
int IsEmpty(Stack S)
{
return S->TopOfStack == -1;
}
/*判断满栈*/
int IsFull(Stack S)
{
return S->TopOfStack == S->capacity-1;
}
/*入栈*/
void Push(int target, Stack S)
{
if(IsFull(S))
cout <<"Full Stack!"<<endl;
else
S->Array[++S->TopOfStack] = target;
}
/*取栈顶元素*/
int Top(Stack S)
{
if (IsEmpty(S) )
cout<<"Empty Stack!"<<endl;
else
return S->Array[S->TopOfStack];
return 0;
}
/*出栈*/
void Pop(Stack S)
{
if(IsEmpty(S))
cout<<"Empty Stack!"<<endl;
else
S->TopOfStack--;
}
/*打印元素*/
void PrintStack(Stack S)
{
if(IsEmpty(S))
cout<<"Empty Stack!"<<endl;
else
for(int i=0; i<=S->TopOfStack; i++){
cout<<S->Array[i]<<' ';
}
cout<<endl;
}
int main()
{
Stack S;
S = CreateStack(5);
for(int i=0; i<5; i++)
Push(i, S);
PrintStack(S);
int top = Top(S);
cout << "the top is:" <<top<<endl;
Pop(S);
PrintStack(S);
Push(4,S);
Push(5,S);
PrintStack(S);
Pop(S);
Pop(S);
Pop(S);
Pop(S);
Pop(S);
Pop(S);
return 0;
}
如果要封装成类,则代码如下:
#include <iostream>
#include <stdlib.h>
using namespace std;
template <typename T>
class Stack{
public:
Stack(int MaxSize = 50);
void MakeEmpty();
int IsEmpty();
int IsFull();
void Push(T target);
int Top();
void Pop();
void PrintStack();
private:
int capacity; //栈的容量
int TopOfStack; //栈顶
T *Array; //指针指向分配的动态数组
};
/*创建栈的构造函数*/
template <typename T>
Stack<T>::Stack(int MaxSize)
{
TopOfStack = -1;
capacity = MaxSize;
Array = new T[MaxSize];
}
/*清空栈*/
template <typename T>
void Stack<T>::MakeEmpty()
{
TopOfStack = -1;
}
/*判断空栈*/
template <typename T>
int Stack<T>::IsEmpty()
{
return TopOfStack == -1;
}
/*判断满栈*/
template <typename T>
int Stack<T>::IsFull()
{
return TopOfStack == capacity - 1;
}
/*入栈*/
template <typename T>
void Stack<T>::Push(T target)
{
if(IsFull())
cout <<"Full Stack!"<<endl;
else
Array[++TopOfStack] = target;
}
/*取栈顶元素*/
template <typename T>
int Stack<T>::Top()
{
if (IsEmpty())
cout<<"Empty Stack!"<<endl;
else
return Array[TopOfStack];
return 0;
}
/*出栈*/
template <typename T>
void Stack<T>::Pop()
{
if (IsEmpty())
cout<<"Empty Stack!"<<endl;
else
TopOfStack --;
}
/*打印元素*/
template <typename T>
void Stack<T>::PrintStack()
{
if(IsEmpty())
cout<<"Empty Stack!"<<endl;
else
for(int i=0; i<=TopOfStack; i++){
cout<<Array[i]<<' ';
}
cout<<endl;
}
int main()
{
char s1,s2;
s1 = 'A';
s2 = 'Z';
Stack<char> S;
for(int i=s2; i>=s1; i--)
S.Push(i);
char x;
while(!S.IsEmpty()){
x = S.Top();
cout <<x<<' ';
S.Pop();
}
return 0;
}