-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
65 lines (53 loc) · 935 Bytes
/
stack.c
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
int test(){
Stack *stack = new_stack();
push(10, stack);
push(30, stack);
push(-52, stack);
print_stack(stack);
int a = pop(stack);
printf("\npoped : %d", a);
print_stack(stack);
int b = pop(stack);
printf("\npoped : %d", b);
push(666, stack);
print_stack(stack);
pop(stack);
pop(stack);
pop(stack);
pop(stack);
pop(stack);
pop(stack);
pop(stack);
print_stack(stack);
}
/*
int main(void){
puts("Lancement test");
test();
return 0 ;
}
*/
void print_stack(Stack *s){
int i = 0 ;
printf("\n");
for(putchar('|');i<(top(s));i++) printf("%d ", s->tab[i]);
printf("|\n");
}
Stack* new_stack(){
Stack *n = (Stack*)malloc(sizeof(Stack));
n->cursor = 0 ;
return n;
}
void push(int nb, Stack *s){
s->tab[s->cursor++] = nb ;
}
int top(Stack *s){
return s->cursor ;
}
int pop(Stack *s){
if(!s->cursor) return -1;
return s->tab[--s->cursor];
}