Stack 02 I understand

Source: Internet
Author: User

// ------- Use the algorithm of the basic operation of the array current stack to implement ------------------------
Stack InitStack (int MaxElements) // construct an empty Stack
{
Stack S;
If (MaxElements <MinStackSize)
{
Puts ("Stack size is too small! ");
Return NULL;
}
S = (Stack) malloc (sizeof (struct StackRecord ));
If (! S)
{
Printf ("Out of space! ");
Return NULL;
}
S-> Array = (ElemType *) malloc (sizeof (ElemType) * MaxElements );
If (! S-> Array)
{
Printf ("Out of space! ");
Return NULL;
}
S-> StackSize = MaxElements;
Return MakeEmpty (S );
}
Void DestroyStack (Stack * S); // initial condition: Stack S already exists. Operation Result: Destroy stack S.
{
Free (S-> Array );
Free (S );
}
Stack MakeEmpty (Stack S); // initial condition: Stack S is not empty. Operation Result: reset stack S to empty stack.
{
S-> TopOfStack = EmptyTop;
Return S;
}
Int IsEmpty (Stack S); // initial condition: Stack S already exists. Operation Result: judge whether the stack is empty.
{
Return S-> TopOfStack = EmptyTop;
}

Int IsFull (Stack S); // initial condition: Stack S already exists. Operation Result: judge whether the stack is empty.
{
Return S-> TopOfStack = StackSize-1;
}
Int StackLength (Stack S); // initial condition: Stack S already exists. Operation Result: return the number of stack S nodes.
{
Return S-> TopOfStack-EmptyTop;
}

<

Related Article

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.