-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_test.c
70 lines (58 loc) · 1.32 KB
/
stack_test.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
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
typedef struct {
int x;
int y;
} Point;
Point *Point_new(const int x, const int y) {
Point *p = (Point *)malloc(sizeof(Point));
p->x = x;
p->y = y;
return p;
}
void Point_delete(Point *p) {
if (p)
free(p);
}
void Point_print(Point *p) {
printf("(%d, %d)", p->x, p->y);
}
int main(int argc, char const* argv[])
{
Stack *st = Stack_new();
StackIterator *iter;
Stack_push(st, Point_new(1, 3));
Stack_push(st, Point_new(1, 6));
Stack_push(st, Point_new(4, 4));
Stack_push(st, Point_new(5, 8));
Stack_push(st, Point_new(1, 5));
Stack_push(st, Point_new(4, 3));
if (Stack_isEmpty(st)) {
printf("stack is empty!\n");
} else {
printf("stack is not empty!\n");
}
iter = Stack_iterator(st);
while (StackIter_moveNext(iter)) {
Point *p = (Point *)StackIter_current(iter);
printf("iter: (%d,%d)\n", p->x, p->y);
}
StackIter_delete(iter);
printf("pop: ");
Point_print((Point *)Stack_pop(st));
printf("\n");
printf("pop: ");
Point_print((Point *)Stack_pop(st));
printf("\n");
while (!Stack_isEmpty(st)) {
Point *p = (Point *)Stack_pop(st);
printf("lp_pop: (%d,%d)\n", p->x, p->y);
}
if (Stack_isEmpty(st)) {
printf("stack is empty!\n");
} else {
printf("stack is not empty!\n");
}
return 0;
}