#include "stdio.h"
#include "stdlib.h"
#define MAXSIZE 30
typedef int ElemType;
typedef struct
{
ElemType data[MAXSIZE];
int top;
}SequenceStack;
void InitStack(SequenceStack &S)
{
S.top = -1;
}
void DestroyStack(SequenceStack* S)
{
free(S);
}
bool IsEmpty(SequenceStack &S)
{
return (S.top == -1);
}
int getLength(SequenceStack S)
{
return S.top + 1;
}
bool Push(SequenceStack &S, ElemType e)
{
if (S.top == MAXSIZE - 1)
return false;
S.top++;
S.data[S.top] = e;
return true;
}
bool Pop(SequenceStack &S, ElemType &e)
{
if (S.top == -1)
return false;
e = S.data[S.top];
S.top--;
return true;
}
bool GetTop(SequenceStack &S, ElemType &e)
{
if (S.top == -1)
return false;
e = S.data[S.top];
return true;
}
void PrintStack(SequenceStack S)
{
while (S.top != -1)
{
printf("%d\n", S.data[S.top]);
S.top--;
}
}
int main()
{
SequenceStack S;
InitStack(S);
Push(S, 1);
Push(S, 2);
Push(S, 3);
int Length = getLength(S);
printf("栈长:%d\n", Length);
PrintStack(S);
int e;
Pop(S, e);
printf("删除栈顶元素:%d\n", e);
PrintStack(S);
GetTop(S, e);
printf("此时栈顶元素:%d\n", e);
if (IsEmpty(S)) {
DestroyStack(&S);
printf("栈空销毁");
}
system("pause");
return 0;
}
顺序栈(C语言)
最新推荐文章于 2024-07-25 23:20:14 发布