comparison src/list/list.c @ 13:a765eb7f3b7c

implement delete, traverse
author Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
date Tue, 07 Apr 2015 15:39:14 +0900
parents
children c3314208d7b9
comparison
equal deleted inserted replaced
12:81411d8f9a77 13:a765eb7f3b7c
1 #include <stdlib.h>
2 #include <stdio.h>
3
4 #include "prototype.h"
5
6 #include "context.h"
7 #include "allocate.h"
8 #include "origin_cs.h"
9
10 __code code1(Context* context) {
11 goto meta_code1(context, allocate);
12 }
13
14 __code meta_code1(Context* context, __code (*next)()) {
15 context->next = next;
16 goto context->next(context, (int)sizeof(data1), 1, code2);
17 }
18
19 __code code2(Context* context, data1* ds) {
20 ds->value = 1024;
21 goto meta_code2(context, ds);
22 }
23
24 __code meta_code2(Context* context, data1* ds) {
25 mdata* mds = (mdata*)context->mds-1;
26 context->head = mds;
27 mds->next = 0;
28 goto append(context, code3);
29 }
30
31 __code append(Context* context, __code (*next)()) {
32 goto meta_append(context, (mdata*)context->head, next);
33 }
34
35 __code meta_append(Context* context, mdata* mds, __code (*next)()) {
36 if (mds->next)
37 goto meta_append(context, mds->next, next);
38 mds->next = context->mds;
39 goto allocate(context, (int)sizeof(data1), 1, next);
40 }
41
42 __code code3(Context* context, data1* ds) {
43 ds->value = 10;
44 goto meta_code3(context);
45 }
46
47 __code meta_code3(Context* context) {
48 goto append(context, code4);
49 }
50
51 __code code4(Context* context, data1* ds) {
52 goto meta_code4(context);
53 }
54
55 __code meta_code4(Context* context) {
56 goto traverse(context, code5);
57 }
58
59 __code traverse(Context* context, __code (*next)()) {
60 goto meta_traverse(context, (mdata*)context->head, next);
61 }
62
63 __code meta_traverse(Context* context, mdata* mds, __code (*next)()) {
64 printf("current value in list is %d\n", ((data1*)mds->ds)->value);
65 if (mds->next)
66 goto meta_traverse(context, mds->next, next);
67 goto next(context);
68 }
69
70 __code code5(Context* context) {
71 goto meta_code5(context);
72 }
73
74 __code meta_code5(Context* context) {
75 goto delete(context, code6);
76 }
77
78 __code delete(Context* context, __code (*next)()) {
79 goto meta_delete(context, (mdata*)context->head, next);
80 }
81
82 __code meta_delete(Context* context, mdata* head, __code (*next)()) {
83 context->head = (head->next) ? head->next : 0;
84 goto next(context);
85 }
86
87 __code code6(Context* context) {
88 printf("after delete\n");
89 goto meta_code6(context);
90 }
91
92 __code meta_code6(Context* context) {
93 goto traverse(context, exit_code);
94 }
95
96 int main() {
97 Context* context = (Context*)malloc(sizeof(Context));
98 context->ds_heap = malloc(sizeof(data1)*1024);
99 context->mds_heap = malloc(sizeof(mdata)*1024);
100 context->ds = context->ds_heap;
101 context->mds = context->mds_heap;
102 goto start_code(context, code1);
103 }