#ifndef _STDIO_H
#include <stdio.h>
#endif
#ifndef _MALLOC_H
#include <malloc.h>
#endif
#define MAX_SIZE 100
static
int* Top = NULL;
static
int* But = NULL;
int XKJCreat( void )
{
But = (int *)malloc( MAX_SIZE );
if( But == NULL )
{
perror( "mem:" );
return 0;
}
Top = But;
return 1;
}
int XKJPop( void )
{
if( Top == But )
{
printf( "Empty stack" );
getchar();
return 0;
}
else return *--Top;
}
int XKJPush( int number )
{
if( (Top - But) >= sizeof(int) * MAX_SIZE)
{
printf( "over stack" );
getchar();
return 0;
}
else *Top++ = number;
return number;
}
数组实现栈
速度很快(比链表快N倍)CPU内部指令就是这样工作的
唯一缺点就是大小是固定的