#include <stdio.h>
#include <malloc.h>
typedef struct NODE
{
int *pbase;
int len;//array Maximum hold length
int top;//Array Effective length
}stack,*stack;
void Initstack (Stack,int);
void push (Stack,int);
void Pop (Stack,int *);
BOOL Stackempty (Stack);
BOOL Stackfull (Stack);
void Main ()
{
Stack stack;
Initstack (&stack,6);
printf ("Start stack \ n");
Press Stack
Push (&stack,2);
Push (&stack,3);
Push (&stack,4);
Push (&stack,5);
Out of the stack
printf ("Start out stack \ n");
int Val;
Pop (&stack,&val);
Pop (&stack,&val);
Pop (&stack,&val);
Pop (&stack,&val);
}
void Initstack (Stack stack,int len)
{
stack->pbase= (int *) malloc (sizeof (int) *len);//Note This place initializes the memory to be allocated.
stack->len=len;
stack->top=-1;//Note that the local coordinates should be indicated as-1
}
void push (Stack stack,int val)
{
if (Stackfull (stack))
{
printf ("Stack is full, can't stack");
Return
}
stack->top++;
stack->pbase[stack->top]=val;
}
BOOL Stackfull (Stack stack)
{
if (stack->len-1==stack->top)
return true;
Else
return false;
}
void Pop (Stack stack,int *val)
{
if (Stackempty (stack))
{
printf ("The current stack is empty, unable to stack");
Return
}
*val=stack->pbase[stack->top];
printf ("%d", *val);
stack->top--;
}
BOOL Stackempty (Stack stack)
{
if (stack->top==-1)
return true;
Else
return false;
}
This article from the "Jane Answers Life" blog, reproduced please contact the author!
An array implementation of the stack of linear tables