栈的顺序存储和基本实现

#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;
}
//取栈顶元素
int GetTop(SqStack *s,int &e)
{
    if (s->top == -1)
        return 0;
    e = s-> data[s->top];
    return 1;
}
//显示栈中元素
void  DispStack(SqStack *s)
{
    int i;
    for (i = s->top; i >= 0; i--)
        cout << s->data[i] << " ";
    cout << "\n";
}

 
















 

posted @ 2014-10-08 17:54  tong1487  阅读(166)  评论(0)    收藏  举报