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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
-
|
!
-
|
|
|
|
|
|
|
!
-
|
!
-
|
|
|
|
|
|
|
|
!
-
|
!
-
|
!
-
|
|
!
-
|
|
|
!
-
|
|
|
!
|
#module "DLinkedListAry"
#deffunc DLink_init int i
mref res,64
if i < 1 : res = -1 : goto *@f
n = i
dim _value , n+2
dim _next , n+2
dim _prev , n+2
repeat n+1
_next(cnt) = cnt+1
_prev(cnt) = -1 + cnt
loop
head = 0
tail = head
new = _next(head)
_next(n+1) = 1
_prev(head) = n
_prev(n+1) = n
res = n
*@
return
#deffunc DLink_add int i
if new = (n+1) : res = -1 : goto *@f
p = _next(head)
repeat
if (p = new)|(i < _value(p)) : break
p = _next(p)
loop
_value(new) = i
if p = new{
tail = p
}
else{
_next(tail) = _next(new)
_prev(_next(tail)) = tail
_next(_prev(p)) = new
_next(new) = p
_prev(new) = _prev(p)
_prev(p) = new
}
new = _next(tail)
res = p
*@
return
#deffunc DLink_del int i
if tail = head : res = -1 : goto *@f
p = _next(head)
repeat
if (p = new)|(i = _value(p)) : break
p = _next(p)
loop
if p = new : res = -1 : goto *@f
if p = tail{
tail = _prev(p)
}
else{
_next(_prev(p)) = _next(p)
_prev(_next(p)) = _prev(p)
_next(p) = _next(tail)
_next(tail) = p
_prev(p) = _prev(new)
_prev(new) = p
}
new = p
res = p
*@
return
#deffunc DLink_disp str cmd
if (cmd ! "+") & (cmd ! "-") : res = -1 : goto *@f
if cmd = "+" : p = _next(head) : else : p = tail
s = "[ "
repeat
if ((cmd = "+") & (p = new)) | (p = head) : res = cnt : break
s += "" + _value(p) + " "
if cmd = "+" : p = _next(p) : else : p = _prev(p)
loop
s += "]:" + res + "/" + n
mes s
*@
return
#global
#define LISTSIZE 5
randomize
font "MS ゴシック",12
cmd = ""
input cmd,,,3
DLink_init LISTSIZE
if stat = -1{
dialog "リストの初期化に失敗しました.",1,"終了確認" : end
}
else{
title "リストサイズ=" + stat
}
onkey gosub *enter
stop
*enter
if iparam ! 13 : goto *@f
getstr num,cmd,1
if num = ""{
cmd = "" : objprm 0,cmd
goto *@f
}
num = int(num)
c = peek(cmd,0)
switch c
case '+' : DLink_add num
if stat ! -1{
title " "+num+" を挿入しました."
DLink_disp "+"
DLink_disp "-"
}
swbreak
case '-' : DLink_del num
if stat ! -1{
title " "+num+" を削除しました."
DLink_disp "+"
DLink_disp "-"
}
swbreak
swend
cmd = "" : objprm 0,cmd
*@
return
|