#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct SNODE
{
int *data;
int top;
int MAXSIZE;
}*stack, node;
stack initi_stack(int n);
bool is_full(stack plist);
bool push(stack plist, int val);
bool pop(stack plist, int *val);
bool is_empty(stack plist);
int main(void)
{
int lens;
int val;
int i;
printf(“请输入您要生成的栈的大小:\n”);
scanf("%d", &lens);
stack S = initi_stack(lens);
push(S, -1314);
push(S, -25);
push(S, 52);
push(S, 727);
for ( i = 0; i <= S->top; i++)
{
printf("%d “, S->data[i]);
}
printf(”\n");
pop(S, &val);
for ( i = 0; i <= S->top; i++)
{
printf("%d “, S->data[i]);
}
printf(”\n");
free(S->data);
free(S);
S = NULL;
return 0;
}
stack initi_stack(int n)
{
stack tmp = (stack) malloc(sizeof(node));
if(NULLtmp)
{
exit(-1);
}
tmp->MAXSIZE = n;
tmp->top = -1;
tmp->data = (int *)malloc(sizeof(int) * n);
if(NULLtmp->data)
{
exit(-1);
}
return tmp;
}
bool is_full(stack plist)
{
if(plist->top == plist->MAXSIZE-1)
{
return true;
}
else
{
return false;
}
}
bool push(stack plist, int val)
{
if(is_full(plist))
{
printf(“stack is full, failed!\n”);
return false;
}
plist->data[++(plist->top)] = val;
return true;
}
bool is_empty(stack plist)
{
if(plist->top==-1)
{
return true;
}
else
{
return false;
}
}
bool pop(stack plist, int *val)
{
if(is_empty(plist))
{
printf(“no factor in stack,failed!\n”);
return false;
}
*val = plist->data[plist->top];
plist->top–;
return true;
}