1.基本操作
stack createstack(int maxsize)
{
stack s = (stack)malloc(sizeof struct node);
s->data = (int *)malloc(maxsize*sizeof(int));
s->top = -1;
s->maxsize = maxsize;
return s;
}
bool full(stack s)
{
return (s->top == s->maxsize - 1);
}
bool push(stack s,int x)
{
if(full(s))
return 0;
s->data [++(s->top)] = x;
return 1;
}
bool isempty(stack s)
{
return (s->top == -1);
}
int pop(stack s)
{
if(empty(s))
return 0;
return (s->data [--(s->top)]);
}
2.链式存储
stack createstack()
{
stack s;
s = (stack)malloc(sizeof(struct node));
s->next = NULL;
return s;
}
bool isempty(stack s)
{
if(s->next == NULL)
return 1;
return 0;
}
bool push(stack s,int x)
{
stack temp;
temp = (stack)malloc(sizeof(struct node));
temp->data = x;
temp->next = s->next;
s->next = temp;
return 1;
}
int pop(stack s)
{
stack temp;
int top;
if(isempty)
return -1;
temp = s->next;
top = temp->data;
s->next = temp->next;
return top;
}