用动态数组实现栈的操作

          栈一般有两种实现的方式,链表和数组,链表的实现比较常见,这里主要是用动态数组去实现,可以让用户在堆上自由分配内存指定栈的容量。

  为了便于用户的操作,需要对数组有两个附加信息,一个是数组最后一个元素的下标,记作TopOfStack,去模拟栈顶;另一个是数组的容量capacity,这两个附加元素和数组一起组成一个结构体,如下所示:

struct StackRecord
{
    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;
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值