// ------- 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;
}
<