Stack implemented by Dynamic Array

Source: Internet
Author: User
/*_##################################### #######################################
_##
### 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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.