#include<stdio.h>
#define MaxSize 20
#define ERROR 0
#define OK 1
typedef int ElementType;
typedef struct SNode *Stack;
struct SNode
{
ElementType Data[MaxSize];
int Top;
};
/**
* 入栈
* */
void Push(Stack PtrS, ElementType item)
{
//入栈前检查是否堆栈已满
if(PtrS->Top == MaxSize-1)
printf("堆栈满");
else
{
PtrS->Data[++(PtrS->Top)] = item;
}
}
/**
* 出栈
* */
ElementType Pop(Stack PtrS)
{
//出栈前检查堆栈是否为空
if(PtrS->Top == -1)
{
printf("堆栈空");
return ERROR;
}
else
{
return(PtrS->Data[(PtrS->Top)--]);
}
}
/**
* 输入栈内的元素
* */
void loopStack(Stack PtrS)
{
while(PtrS->Top+1)
{
printf("%d\n",PtrS->Data[PtrS->Top--]);
}
}
/**
* 测试程序
**/
int main(int argc, char const *argv[])
{
struct SNode test;
test.Top = -1;
Push(&test,1);
Push(&test,2);
Push(&test,3);
loopStack(&test);
return 0;
}
c语言数据结构堆栈入栈出栈
最新推荐文章于 2023-05-02 19:01:11 发布