顺序栈的实现

#include<iostream>
#define maxsize 100
using namespace std;
typedef struct
{
    int *base;
    int *top;
    int stacksize;
}SqStack;
void InitStack(SqStack &s)
{
    s.base = new int[maxsize];
    if (!s.base)
    {
        cout << "error!" << endl;
        return;
    }
    s.top = s.base;
    s.stacksize = maxsize;
}
bool StackEmpty(SqStack s)
{
    if (s.top == s.base)
        return true;
    else
        return false;
}
int StackLength(SqStack s)
{
    return s.top - s.base;
}
void ClearStack(SqStack &s)
{
    if (s.base)
        s.top = s.base;
}
void DestroyStack(SqStack &s)
{
    if (s.base)
    {
        delete s.base;
        s.stacksize = 0;
        s.base = s.top = NULL;
    }
}
void Push(SqStack &s, int e)
{
    if (s.top - s.base == s.stacksize)
    {
        cout << "栈满了!" << endl;
        return;
    }
    *s.top++ = e;//a++ 值不变 ++a 值已经变化  指针移动
}
void Pop(SqStack &s, int &e)//出栈,指针移动了
{
    if (s.top == s.base)
    {
        cout << "栈空" << endl;
        return;
    }
    e = *--s.top;
}
void GetTop(SqStack &s, int &e)
{
    if (s.top == s.base)
        cout << "栈空" << endl;
    e = *(s.top - 1);//指针位置没有移动
}

 

posted @ 2019-10-04 18:58  Tomorrow1126  阅读(128)  评论(0编辑  收藏  举报