annotate final_main/src/SingleLinkedStack.cbc @ 0:83f997abf3b5

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