首页 > 其他 > 详细

顺序栈的实现

时间:2019-10-04 19:26:44      阅读:72      评论:0      收藏:0      [点我收藏+]
#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);//指针位置没有移动
}

 

顺序栈的实现

原文:https://www.cnblogs.com/h694879357/p/11622861.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!