cpp
复制代码
// 栈FILO-C++.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
typedef int32_t ElemType;
#define STACK_DEPTH 8
typedef struct stStack_Typedef {
ElemType data[STACK_DEPTH];
int top;
} stStack_Typedef;
void Init_Stack(stStack_Typedef* S)
{
int i = 0;
while (i != STACK_DEPTH) {
S->data[i++] = 0;
}
S->top = -1;
}
bool IsStack_Empty(stStack_Typedef* S)
{
if (S->top == -1) {
return true;
}
return false;
}
bool IsStack_Full(stStack_Typedef* S)
{
if (S->top == STACK_DEPTH - 1) {
return true;
}
return false;
}
bool PushElem_ToStack(stStack_Typedef* S, ElemType x)
{
if (IsStack_Full(S)) {
return false;
}
S->data[S->top++] = x;
return true;
}
bool PopElem_FromStack(stStack_Typedef* S, ElemType* x)
{
if (S->top == -1) {
return false;
}
*x = S->data[--S->top];
if (S->top < 0) {
S->top = -1;
return false;
}
return true;
}
bool GetTopElem(stStack_Typedef* S, ElemType* x)
{
if (S->top == -1) {
return false;
}
*x = S->data[S->top];
return true;
}
int main()
{
stStack_Typedef S = { 0 };
ElemType x = 0;
bool isStackFull = false;
bool isStackEmpty = false;
if (!isStackFull) {
if (PushElem_ToStack(&S, 11)) {
printf("入栈元素:%d\r\n", 11);
}
else {
printf("Stack is full!\r\n");
isStackFull = true;
}
}
if (!isStackFull) {
if (PushElem_ToStack(&S, 22)) {
printf("入栈元素:%d\r\n", 22);
}
else {
printf("Stack is full!\r\n");
isStackFull = true;
}
}
if (!isStackFull) {
if (PushElem_ToStack(&S, 33)) {
printf("入栈元素:%d\r\n", 33);
}
else {
printf("Stack is full!\r\n");
isStackFull = true;
}
}
if (!isStackFull) {
if (PushElem_ToStack(&S, 44)) {
printf("入栈元素:%d\r\n", 44);
}
else {
printf("Stack is full!\r\n");
isStackFull = true;
}
}
if (!isStackFull) {
if (PushElem_ToStack(&S, 55)) {
printf("入栈元素:%d\r\n", 55);
}
else {
printf("Stack is full!\r\n");
isStackFull = true;
}
}
if (!isStackFull) {
if (PushElem_ToStack(&S, 66)) {
printf("入栈元素:%d\r\n", 66);
}
else {
printf("Stack is full!\r\n");
isStackFull = true;
}
}
if (!isStackFull) {
if (PushElem_ToStack(&S, 77)) {
printf("入栈元素:%d\r\n", 77);
}
else {
printf("Stack is full!\r\n");
isStackFull = true;
}
}
if (!isStackFull) {
if (PushElem_ToStack(&S, 88)) {
printf("入栈元素:%d\r\n", 88);
}
else {
printf("Stack is full!\r\n");
isStackFull = true;
}
}
if (!isStackFull) {
if (PushElem_ToStack(&S, 99)) {
printf("入栈元素:%d\r\n", 99);
}
else {
printf("Stack is full!\r\n");
isStackFull = true;
}
}
if (!isStackFull) {
if (PushElem_ToStack(&S, 123)) {
printf("入栈元素:%d\r\n", 123);
}
else {
printf("Stack is full!\r\n");
isStackFull = true;
}
}
if (!isStackEmpty) {
if (PopElem_FromStack(&S, &x)) {
printf("出栈元素:%d\r\n", x);
}
else {
printf("Stack is empty!\r\n");
isStackEmpty = true;
}
}
if (!isStackEmpty) {
if (PopElem_FromStack(&S, &x)) {
printf("出栈元素:%d\r\n", x);
}
else {
printf("Stack is empty!\r\n");
isStackEmpty = true;
}
}
if (!isStackEmpty) {
if (PopElem_FromStack(&S, &x)) {
printf("出栈元素:%d\r\n", x);
}
else {
printf("Stack is empty!\r\n");
isStackEmpty = true;
}
}
if (!isStackEmpty) {
if (PopElem_FromStack(&S, &x)) {
printf("出栈元素:%d\r\n", x);
}
else {
printf("Stack is empty!\r\n");
isStackEmpty = true;
}
}
if (!isStackEmpty) {
if (PopElem_FromStack(&S, &x)) {
printf("出栈元素:%d\r\n", x);
}
else {
printf("Stack is empty!\r\n");
isStackEmpty = true;
}
}
if (!isStackEmpty) {
if (PopElem_FromStack(&S, &x)) {
printf("出栈元素:%d\r\n", x);
}
else {
printf("Stack is empty!\r\n");
isStackEmpty = true;
}
}
if (!isStackEmpty) {
if (PopElem_FromStack(&S, &x)) {
printf("出栈元素:%d\r\n", x);
}
else {
printf("Stack is empty!\r\n");
isStackEmpty = true;
}
}
if (!isStackEmpty) {
if (PopElem_FromStack(&S, &x)) {
printf("出栈元素:%d\r\n", x);
}
else {
printf("Stack is empty!\r\n");
isStackEmpty = true;
}
}
if (!isStackEmpty) {
if (PopElem_FromStack(&S, &x)) {
printf("出栈元素:%d\r\n", x);
}
else {
printf("Stack is empty!\r\n");
isStackEmpty = true;
}
}
if (!isStackEmpty) {
if (PopElem_FromStack(&S, &x)) {
printf("出栈元素:%d\r\n", x);
}
else {
printf("Stack is empty!\r\n");
isStackEmpty = true;
}
}
return 0;
}