-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pilha.h
49 lines (41 loc) · 928 Bytes
/
Pilha.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char* elementos;
int qtd;
} Pilha;
void iniciarPilha(Pilha *p){
p->qtd = 0;
p->elementos = (char *)malloc(p->qtd * sizeof(char));
}
void push(Pilha *p, char e){
if(e=='#')
return;
p->qtd++;
p->elementos = (char *)realloc(p->elementos, p->qtd * sizeof(char));
p->elementos[p->qtd-1] = e;
}
char pop(Pilha *p){
char r = 0;
if(p->qtd == 0)
printf("pilha vazia.\n");
else{
p->qtd--;
r = p->elementos[p->qtd];
p->elementos = (char *)realloc(p->elementos, p->qtd * sizeof(char));
}
return r;
}
void printPilha(Pilha p){
if(p.qtd == 0)
printf("[ lambda [\n");
else{
printf("[ ");
printf("%c", p.elementos[0]);
for(int i = 1; i < p.qtd; i++){
printf(", %c", p.elementos[i]);
}
printf(" [\n");
}
return;
}