Skip to content

Instantly share code, notes, and snippets.

@Pr3d4dor
Last active August 29, 2015 14:24
Show Gist options
  • Save Pr3d4dor/875c9024ca9bb85a36eb to your computer and use it in GitHub Desktop.
Save Pr3d4dor/875c9024ca9bb85a36eb to your computer and use it in GitHub Desktop.
#define MAX 10
#define ERRORSTACKEMPTY -2
#define ERRORSTACKFULL -1
typedef struct{
int item[MAX];
int top;
}Stack;
int push(Stack *p,int x);
int pop(Stack *p);
void iniStack(Stack *p);
int stackIsEmpty(Stack *p);
int stackIsFull(Stack *p);
void listStack(Stack *p);
void listStack(Stack *p){
int i;
if (stackIsEmpty(p)==ERRORstackIsEmpty)
printf ("The stack cannot be displayed because is empty!\n");
else{
printf ("Stack elements in order:\n");
for (i=p->top-1;i>-1;i--){
printf ("%d\n",p->item[i]);
}
}
}
int push(Stack *p,int x){
if ((stackIsFull(p))==1)
return (ERRORSTACKFULL); // Erro
else{
p->item[p->top]=x;
p->top++;
return (p->top);
}
}
int pop(Stack *p){
if ((stackIsEmpty(p))==1)
return (ERRORstackIsEmpty); // Erro
else{
p->top--;
return(p->item[p->top]);
}
}
void iniStack(Stack *p){
p->top=0;
}
int stackIsEmpty(Stack *p){
if (p->top==0)
return ERRORstackIsEmpty;
else
return 0;
}
int stackIsFull(Stack *p){
if (p->top==MAX)
return ERRORstackIsFull;
else
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment