/*_##################################### #######################################
_##
### Stack implemented by Dynamic Array
_ ## Author: xwlee
_ ## Time: 2006.12.31
### Chang'an University
_ ## Development condition: win2003 SERVER + vc6.0
_##
_ ## Dynamic_array.cpp File
_####################################### ###################################*/
# 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 Function
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 Function
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 function
Void push (stack_type value)
{
If (is_full () // If the stack is full, the condition is true.
{
Printf ("stack already full./N ");
Exit (0 );
}
Top_element + = 1;
Stack [top_element] = value;
}
// Pop Function
Void POP (void)
{
If (is_empty () // If the stack is empty, the condition is true.
{
Printf ("stack already empty./N ");
Exit (0 );
}
Top_element-= 1;
}
// Top Function
Stack_type top (void)
{
If (is_empty () // If the stack is empty, the condition is true.
{
Printf ("stack already empty./N ");
Exit (0 );
}
Return stack [top_element];
}
// Is_empty Function
Int is_empty (void)
{
If (stack_size = 0) // If the stack is empty.
{
Printf ("Stack is empty, Please create stack firstly./N ");
Exit (0 );
}
Return top_element =-1;
}
// Is_full Function
Int is_full (void)
{
If (stack_size = 0) // If the stack is empty.
{
Printf ("Stack is empty, Please create stack firstly./N ");
Exit (0 );
}
Return top_element = (signed INT) (stack_size-1 );
}