/*_############################################################################
_##
_## 动态数组实现的堆栈
_## Author: xwlee
_## Time: 2006.12.31
_## Chang'an University
_## Development condition: win2003 Server+VC6.0
_##
_## dynamic_array.cpp 文件
_##########################################################################*/
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
static STACK_TYPE *stack;
static size_t stack_size;
static int top_element = -1;
// create_stack函数
int create_stack( size_t size )
{
if( stack_size != 0)
{
printf("stack already created.");
abort();
}
stack_size = size;
stack = ( STACK_TYPE *)malloc( stack_size * sizeof( STACK_TYPE ) );
if( stack == NULL)
{
printf("create stack false,please try again(size<max(size_t)). /n");
return 0;
}
return 1;
}
// destroy_stack函数
int destroy_stack( void )
{
if( stack_size == 0 )
{
printf("destroy stack false./n");
return 0;
}
stack_size = 0;
free( stack );
stack = NULL;
return 1;
}
// push函数
void push( STACK_TYPE value )
{
if( is_full() ) // 若堆栈已满,条件成立.
{
printf("stack already full./n");
exit(0);
}
top_element += 1;
stack[ top_element ] = value;
}
// pop函数
void pop( void )
{
if( is_empty() ) // 若堆栈已空,条件成立.
{
printf("stack already empty./n");
exit(0);
}
top_element -= 1;
}
// top函数
STACK_TYPE top( void )
{
if( is_empty() ) // 若堆栈已空,条件成立.
{
printf("stack already empty./n");
exit(0);
}
return stack[ top_element ];
}
// is_empty函数
int is_empty( void )
{
if( stack_size == 0 ) // 若堆栈空时.
{
printf("stack is empty,please create stack firstly./n");
exit(0);
}
return top_element == -1;
}
// is_full函数
int is_full( void )
{
if( stack_size == 0 ) // 若堆栈空时.
{
printf("stack is empty,please create stack firstly./n");
exit(0);
}
return top_element == (signed int)(stack_size - 1);
}
_##
_## 动态数组实现的堆栈
_## Author: xwlee
_## Time: 2006.12.31
_## Chang'an University
_## Development condition: win2003 Server+VC6.0
_##
_## dynamic_array.cpp 文件
_##########################################################################*/
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
static STACK_TYPE *stack;
static size_t stack_size;
static int top_element = -1;
// create_stack函数
int create_stack( size_t size )
{
if( stack_size != 0)
{
printf("stack already created.");
abort();
}
stack_size = size;
stack = ( STACK_TYPE *)malloc( stack_size * sizeof( STACK_TYPE ) );
if( stack == NULL)
{
printf("create stack false,please try again(size<max(size_t)). /n");
return 0;
}
return 1;
}
// destroy_stack函数
int destroy_stack( void )
{
if( stack_size == 0 )
{
printf("destroy stack false./n");
return 0;
}
stack_size = 0;
free( stack );
stack = NULL;
return 1;
}
// push函数
void push( STACK_TYPE value )
{
if( is_full() ) // 若堆栈已满,条件成立.
{
printf("stack already full./n");
exit(0);
}
top_element += 1;
stack[ top_element ] = value;
}
// pop函数
void pop( void )
{
if( is_empty() ) // 若堆栈已空,条件成立.
{
printf("stack already empty./n");
exit(0);
}
top_element -= 1;
}
// top函数
STACK_TYPE top( void )
{
if( is_empty() ) // 若堆栈已空,条件成立.
{
printf("stack already empty./n");
exit(0);
}
return stack[ top_element ];
}
// is_empty函数
int is_empty( void )
{
if( stack_size == 0 ) // 若堆栈空时.
{
printf("stack is empty,please create stack firstly./n");
exit(0);
}
return top_element == -1;
}
// is_full函数
int is_full( void )
{
if( stack_size == 0 ) // 若堆栈空时.
{
printf("stack is empty,please create stack firstly./n");
exit(0);
}
return top_element == (signed int)(stack_size - 1);
}