首页 > 其他 > 详细

栈的顺序存储和基本实现

时间:2014-10-08 23:57:47      阅读:357      评论:0      收藏:0      [点我收藏+]
#include<iostream>
#include<cstring>
#include<cstdlib>
#define maxsize 1000
using namespace std;
//顺序结构
typedef struct 
{
    int data[maxsize];
    int top;
}SqStack;
//初始化栈
void initStack(SqStack *&s)
{
    s = new SqStack;
    s->top = -1;//空栈
}
void Clearstack(SqStack *&s)
{
    delete s;
}
//求栈的长度
int StackLength(SqStack *s)
{
    return (s->top+1);
}
//判断栈是否为空
int StackEmpty(SqStack *s)
{
    return (s->top == -1);
}
//进栈
int push(SqStack *&s,int e)
{
    if (s->top == maxsize - 1)//栈满的情况,栈上溢出
        return 0;
    s->top++;
    s->data[s->top] = e;//将e插进栈顶
    return 1;
}
//出栈
int  pop(SqStack *&s,int e)
{
    if (s->top == -1)//空栈情况,栈下溢出
        return 0;
    e= s->data[s->top];
    s->top--;
    return 1;
}

 

栈的顺序存储和基本实现

原文:http://www.cnblogs.com/tong1487/p/4011759.html

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