80
|
1 #include "../context.h"
|
|
2 #interface "Stack.h"
|
|
3
|
82
|
4 Stack* createSingleLinkedStack(struct Context* cbc_context) {
|
80
|
5 struct Stack* stack = new Stack();
|
|
6 struct SingleLinkedStack* singleLinkedStack = new SingleLinkedStack();
|
|
7 stack->stack = (union Data*)singleLinkedStack;
|
|
8 singleLinkedStack->top = NULL;
|
|
9 stack->push = C_pushSingleLinkedStack;
|
|
10 stack->pop = C_popSingleLinkedStack;
|
|
11 stack->pop2 = C_pop2SingleLinkedStack;
|
|
12 stack->get = C_getSingleLinkedStack;
|
|
13 stack->get2 = C_get2SingleLinkedStack;
|
|
14 stack->isEmpty = C_isEmptySingleLinkedStack;
|
|
15 stack->clear = C_clearSingleLinkedStack;
|
|
16 return stack;
|
|
17 }
|
|
18
|
|
19 void printStack1(union Data* data) {
|
|
20 struct Node* node = &data->Element.data->Node;
|
|
21 // if (node == NULL) {
|
|
22 // printf("NULL");
|
|
23 // } else {
|
|
24 // printf("key = %d ,", node->key);
|
|
25 // printStack1((union Data*)data->Element.next);
|
|
26 // }
|
|
27 }
|
|
28
|
|
29 void printStack(union Data* data) {
|
|
30 printStack1(data);
|
|
31 // printf("\n");
|
|
32 }
|
|
33
|
|
34 __code clearSingleLinkedStack(struct SingleLinkedStack* stack,__code next(...)) {
|
|
35 stack->top = NULL;
|
|
36 goto next(...);
|
|
37 }
|
|
38
|
|
39 __code pushSingleLinkedStack(struct SingleLinkedStack* stack, union Data* data, __code next(...)) {
|
|
40 Element* element = new Element();
|
|
41 element->next = stack->top;
|
|
42 element->data = data;
|
|
43 stack->top = element;
|
|
44 goto next(...);
|
|
45 }
|
|
46
|
|
47 __code popSingleLinkedStack(struct SingleLinkedStack* stack, __code next(union Data* data, ...)) {
|
|
48 if (stack->top) {
|
|
49 data = stack->top->data;
|
|
50 stack->top = stack->top->next;
|
|
51 } else {
|
|
52 data = NULL;
|
|
53 }
|
|
54 goto next(data, ...);
|
|
55 }
|
|
56
|
|
57 __code pop2SingleLinkedStack(struct SingleLinkedStack* stack, __code next(union Data* data, union Data* data1, ...)) {
|
|
58 if (stack->top) {
|
|
59 data = stack->top->data;
|
|
60 stack->top = stack->top->next;
|
|
61 } else {
|
|
62 data = NULL;
|
|
63 }
|
|
64 if (stack->top) {
|
|
65 data1 = stack->top->data;
|
|
66 stack->top = stack->top->next;
|
|
67 } else {
|
|
68 data1 = NULL;
|
|
69 }
|
|
70 goto next(data, data1, ...);
|
|
71 }
|
|
72
|
|
73
|
|
74 __code getSingleLinkedStack(struct SingleLinkedStack* stack, __code next(union Data* data, ...)) {
|
|
75 if (stack->top)
|
|
76 data = stack->top->data;
|
|
77 else
|
|
78 data = NULL;
|
|
79 goto next(data, ...);
|
|
80 }
|
|
81
|
|
82 __code get2SingleLinkedStack(struct SingleLinkedStack* stack, __code next(union Data* data, union Data* data1, ...)) {
|
|
83 if (stack->top) {
|
|
84 data = stack->top->data;
|
|
85 if (stack->top->next) {
|
|
86 data1 = stack->top->next->data;
|
|
87 } else {
|
|
88 data1 = NULL;
|
|
89 }
|
|
90 } else {
|
|
91 data = NULL;
|
|
92 data1 = NULL;
|
|
93 }
|
|
94 goto next(data, data1, ...);
|
|
95 }
|
|
96
|
|
97 __code isEmptySingleLinkedStack(struct SingleLinkedStack* stack, __code next(...), __code whenEmpty(...)) {
|
|
98 if (stack->top) {
|
|
99 goto next(...);
|
|
100 } else {
|
|
101 goto whenEmpty(...);
|
|
102 }
|
|
103 }
|
|
104
|
|
105
|