blob: c07c7fc1bfeb4844a0fbe196201cdbdc75870c83 (
plain)
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
|
#include <stdlib.h>
typedef struct stack stack;
typedef struct stack {
char *tape;
int len;
char *ptr;
} stack;
stack *push(stack *s, char c){
if (s == NULL) {
stack *outstack = malloc(sizeof(stack));
outstack->tape = malloc(1);
outstack->tape[0] = c;
outstack->ptr = outstack->tape;
outstack->len = 1;
return outstack;
}
s->len++;
s->ptr++;
s->tape = realloc(s->tape, s->len);
*s->ptr = c;
return s;
}
char pop(stack *s){
char c = *s->ptr;
*s->ptr = 0;
s->len--;
s->ptr--;
s->tape = realloc(s->tape, s->len);
return c;
}
char peek(stack *s){
return *s->ptr;
}
void cleanstack(stack *s){
free(s->tape);
free(s);
}
|