#include<stdlib.h>
#include<stdio.h>
#include<iostream.h>
#define Maxsize 100
typedef int ElemType;
typedef struct
{
ElemType data[Maxsize];
int top;
}SqStack;
void InitStack(SqStack *&s)
{
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
void DestroyStack(SqStack *&s)
{
free(s);
}
bool StackEmpty(SqStack *s)
{
return (s->top==-1);
}
bool Push(SqStack *&s,ElemType e)
{
if(s->top==Maxsize-1)
return false;
else s->top++;
s->data[s->top]=e;
return true;
}
bool Pop(SqStack *&s,ElemType &e)
{
if(s->top==-1)
return false;
e=s->data[s->top];
s->top--;
return true;
}
bool GetTop(SqStack *s,ElemType &e)
{
if(s->top==-1)
return false;
e=s->data[s->top];
return true;
}
void main()
{
SqStack *s;
int i,e;
InitStack(s);
Push(s,1);
Push(s,2);
Push(s,3);
Push(s,4);
Push(s,5);
GetTop(s,e);
cout<<"栈顶元素为"<<e<<endl;
while(!StackEmpty(s))
{
Pop(s,i);
cout<<i<<endl;
}
}
数据结构教程(P66~67)栈的顺序结构
最新推荐文章于 2024-08-29 10:09:01 发布