#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100//Initial capacity
#define Stackincrement 10//per additional capacity
typedef struct
{
int *base;//Stack Bottom pointer
int *top;//Stack Top pointer
int stacksize;//Stack capacity
}sqstack;
int Initstack (Sqstack *l)//Create empty stack
{
l->base= (int *) malloc (stack_init_size * sizeof (int));//Assign Base Address
if (! L->base)
Exit (0);//Storage allocation failed
l->top=l->base;//Empty Stack
l->stacksize=stack_init_size;//Initial capacity
return 1;
}
void Getstack (Sqstack *l)//return stack top element
{
int e;
if (l->top==l->base)//Stack Empty
{
printf ("Empty stack, unable to return. \ n");
Exit (0);
}
Else
{
e=* (l->top-1);//The top position of the top pointer of the stack is the upper element of the stack
printf ("Stack top data:%d\n", e);
}
}
void Push (Sqstack *l)//insert stack top element
{
printf ("Please enter the inserted data shu:");
int e;
scanf ("%d", &e);
if (l->top-l->base>=l->stacksize)//Stack full
{
l->base= (int *) realloc (l->base, (l->stacksize+stackincrement) *sizeof (int));//Increase capacity
if (! L->base)
Exit (0);//Storage allocation failed
l->top=l->base+l->stacksize;//Update stack top pointer
l->stacksize+=stackincrement;//Update Stack capacity
}
*l->top++=e;//stack top data into E
}
void Pop (Sqstack *l)//delete stack top element
{
int e;
if (l->top==l->base)//Stack Empty
{
printf ("Delete error. \ n");
Exit (0);
}
Else
{
e=*--l->top;
printf ("Delete data:%d\n", e);
}
}
void Showstack (Sqstack *l)//display stack
{
printf ("stack:");
int *p;
p=l->base;
while (P!=l->top)
{
printf ("%d", *p++);
}
printf ("\ n");
}
int main ()
{
printf ("First construct an empty stack. \ n");
Sqstack L;
Initstack (&L);
printf ("%s", Initstack (&l)? " Creation succeeded. \ n ":" Creation failed. \ n ");
while (1)
{
printf ("Action you can do: \ n");
printf ("1: Return stack top element 2: Insert stack top element 3: Delete stack top element 0: Exit \ n");
printf ("Please enter your choice:");
int Xuan;
scanf ("%d", &xuan);
if (xuan==0)
Break
if (xuan==1)
{
Getstack (&L);
Showstack (&L);
}
if (xuan==2)
{
Push (&L);
Showstack (&L);
}
if (xuan==3)
{
Pop (&L);
Showstack (&L);
}
}
return 0;
}