#include <stdlib.h>
#define MIN_STACK_SIZE 1
struct stack_t{
int length;
int top;
char *array;
};
typedef struct stack_t * stack;
/*
* create a empty stack according to the given len
* return :the pointer to the stack
*/
stack Create_Stack(size_t len)
{
if(len<MIN_STACK_SIZE)
{
return NULL;
}
stack s=(stack)malloc(sizeof(struct stack_t));
if(s==NULL)
{
return NULL;
}
char *array=(char *)malloc(sizeof(char)*len);
if(array==NULL)
{
return NULL;
}
s->top=-1;
s->array=array;
s->length=len;
return s;
}
/*
* delete the given stack
*/
int Delete_Stack(stack s)
{
if(s==NULL)
{
return -1;
}
free(s->array);
free(s);
return 1;
}
/*
* return -1:empyt
*/
int IsEmptyStack(stack s)
{
return s->top;
}
/*
* push a char to stack s
*/
int Push_Stack(stack s,char c)
{
if(s==NULL||s->top==(s->length-1))
{
return -1;
}
s->top++;
s->array[s->top]=c;
return 1;
}
/*
* pop a char from stack s
*/
char Pop_Stack(stack s)
{
if(s==NULL||s->top==-1)
{
return -1;
}
s->top--;
return s->array[s->top+1];
}
/*
* get a char from stack s
*/
char GetStackTop(stack s)
{
if(s==NULL||s->top==-1)
{
return -1;
}
return s->array[s->top];
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter