# Include <stdio. h>
# Include <malloc. h>
# Define error 0
# Define OK 1
# Define stack_int_size 10/* initial storage space allocation */
# Define stackincrement 5/* increment storage space allocation */
Typedef int elemtype;/* define the element type */
Typedef struct {
Elemtype * base;
Elemtype * top;
Int stacksize;/* currently allocated storage space */
} Sqstack;
Int initstack (sqstack * s);/* construct an empty stack */
Int push (sqstack * s, elemtype E);/* inbound stack */
Int POP (sqstack * s, elemtype * E);/* output stack */
Int createstack (sqstack * s);/* Create stack */
Void printstack (sqstack * s);/* outputs the stack and outputs the elements in the stack */
Int initstack (sqstack * s ){
S-> base = (elemtype *) malloc (stack_int_size + stackincrement) * sizeof (elemtype ));
If (! S-> base) return error;
S-> Top = s-> base;
S-> stacksize = stack_int_size;
Return OK;
}/* Initstack */
Int push (sqstack * s, elemtype E)
{
If (S-> top-S-> base> = s-> stacksize)
{
S-> base = (elemtype *) realloc (S-> base, (S-> stacksize + stack_int_size) * sizeof (elemtype ));
If (! S-> Base)
Return Error;
S-> Top = s-> base + S-> stacksize;
S-> stacksize = s-> stacksize + stackincrement;
}
* (S-> top) = E;
S-> top ++;
Return OK;
}/* Push */
Int POP (sqstack * s, elemtype * E)
{
If (S-> top <= s-> base)
Return Error;
* E = * (-- (S-> top ));
// S-> top --;
Return OK;
}/* Pop */
Int createstack (sqstack * s ){
Int E;
If (initstack (s ))
Printf ("init success! \ N ");
Else {
Printf ("init fail! \ N ");
Return Error;
}
Printf ("input data :( terminated by inputing a character) \ n ");
While (scanf ("% d", & E ))
Push (S, e );
Return OK;
}/* Createstack */
Void printstack (sqstack * s)
{
Int E;
While (POP (S, & E ))
Printf ("% 3d", e );
}/* Pop_and_print */
Int main (){
Sqstack SS;
Printf ("\ n1-createStack \ n ");
Createstack (& SS );
Printf ("n2-Pop & Print \ n ");
Printstack (& SS );
Return 0;
}
Basic operations on dynamic Stack