Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
102 lines (85 sloc) 1.88 KB
/**
* @ProjectName:StaticStack 静态栈的算法实现(数组栈)
* @Author: MagicDevil.Top (MagicDevil.Zhang@gmail.com)
* @Description: 实现功能:初始化栈、入栈、出栈、清空栈、取栈的大小、打印栈的内容
*/
#include <stdio.h>
#include <windows.h>
#define SUCCESS 1
#define ERROR -1
#define ElemType int
#define StatusType int
#define MAX 100
typedef struct StackNode
{
ElemType data[MAX];
int top;
} StackNode, *Stack;
//1.初始化栈空间
StatusType InitStack(Stack &S)
{
S = (Stack)malloc(sizeof(StackNode));
S->top = -1;
return SUCCESS;
}
//2.入栈
StatusType PushStack(Stack &S, ElemType content)
{
if (S->top < MAX - 1 && content != ERROR)
{ //当栈未满
S->data[++S->top] = content;
return SUCCESS;
}
return ERROR;
}
//3.出栈
ElemType PopStack(Stack &S)
{
if (S->top != -1)
{
return (S->data[S->top--]);
}
return ERROR;
}
//4.清空栈
StatusType ClearStack(Stack &S)
{
S->top = -1;
}
//5.取栈的大小
int getStackNum(Stack &S)
{
return S->top + 1;
}
//6.打印栈的内容
StatusType PrintStack(Stack &S)
{
int getStackNum(Stack & S);
printf("Stack Information\n");
for (int i = 0; i <= S->top; i++)
{
printf(" |- Stack %d : %d\n", i, S->data[i]);
}
printf("Num : %d\n", getStackNum(S));
return SUCCESS;
}
int main(int argc, char const *argv[])
{
Stack stack, q;
InitStack(stack);
InitStack(q);
PushStack(stack, 1);
PushStack(stack, 3);
PushStack(stack, 5);
PushStack(stack, 7);
PrintStack(stack);
PushStack(q, PopStack(stack));
PushStack(q, PopStack(stack));
PushStack(q, PopStack(stack));
PushStack(q, PopStack(stack));
PushStack(q, PopStack(stack));
PrintStack(q);
printf("%d\n", getStackNum(stack));
system("pause");
return 0;
}