栈的自实现
#include "stdafx.h"
//FILO
//first in last out
//LIFO
//last in first out
#define N 100
struct Stack
{
char space[N]; //栈空间
int top; //栈下标
};
struct Stack st = { { 0 }, 0 };
int isFull() //栈满返真
{
if (st.top == 100)
return 1;
else
return 0;
}
int isEmpty() //栈空返真
{
if (st.top == 0)
return 1;
else
return 0;
}
void pushStack(char ch)//入栈
{
st.space[st.top] = ch;
st.top++;
}
int popStack()//出栈
{
st.top--;
return st.space[st.top];
}
//注意入栈出栈的自定义函数,若参数定义为struct Stack st
//则是在"栈"(用完即消)上进行的操作,不能影响到全局变量st!
int _tmain(int argc, _TCHAR* argv[])
{
char ch = 'a';
for (int i = 0; i<26; i++)
{
if (!isFull())
pushStack(ch++);//ch++为了打印26个字母
}
while (!isEmpty())
putchar(popStack());//按栈的方式倒序输出字母
return 0;
}