03-14-2025, 04:52 PM
linked lists throwaway
Code: (Select All)
defint a-z
type nodeType
str as _mem
strLen as integer
'you can add more data to store per node here
n as _mem
p as _mem
end type
type listType
head as _mem
tail as _mem
end type
screen 0
dim new as listType
dim cur as _mem
dim temp as _mem
newList new
'insert garbage
addNodeNext temp, "test1", new.head
addNodeNext temp, "test2", temp
addNodeNext temp, "test3", temp
addNodeNext temp, "test4", temp
addNodePrev temp, "testa", new.tail
addNodePrev temp, "estb", temp
addNodePrev temp, "", temp
addNodePrev temp, "td", temp
printList new
nextNode cur, new.head
cx = 1
cy = 1
do
cls
locate 1,1
printList new
locate cy,cx
color 0,7
print chr$(screen(cy,cx))
color 7,0
do
k$ = inkey$
loop until len(k$)>0
select case k$
case chr$(13) 'enter
if cy = 1 and cx = 1 then
addNodePrev cur, "", cur
elseif -1 then
s$ = readNode$(cur)
ss$ = right$(s$, len(s$)-cx+1)
s$ = left$(s$, cx-1)
writeNode cur, s$
addNodeNext cur, ss$, cur
cy = cy + 1
cx = 1
end if
case chr$(8) 'backspace
if cx = 1 then
if cy > 1 then
s$ = readNode$(cur)
temp = cur
prevNode cur, cur
ss$ = readNode$(cur)
cx = len(ss$) + 1
writeNode cur, ss$ + s$
cy = cy-1
rmNode temp
end if
elseif cx > 1 then
cx = cx - 1
s$ = readNode$(cur)
ss$ = left$(s$, cx-1) + right$(s$, len(s$)-cx)
writeNode cur, ss$
end if
case chr$(0)+"K" 'left
if cx > 1 then cx = cx - 1
case chr$(0)+"M" 'right
s$ = readNode$(cur)
if cx <= len(s$) then cx = cx + 1
case chr$(0)+"H" 'up
s$ = readNode$(cur)
if cy > 1 then
prevNode cur, cur
s$ = readNode$(cur)
if cx > len(s$) then cx = len(s$) + 1
cy = cy - 1
end if
case chr$(0)+"P" 'down
nextNode temp, cur
if temp.offset <> new.tail.offset then
cur = temp
s$ = readNode$(cur)
if cx > len(s$) then cx = len(s$) + 1
cy = cy + 1
end if
case chr$(32) to chr$(128)
s$ = readNode$(cur)
s$ = mid$(s$,1,cx-1) + k$ + right$(s$, len(s$)-cx+1)
writeNode cur, s$
cx = cx + 1
end select
loop until k$ = chr$(27)
system
sub addNodeNext (new as _mem, s$, cur as _mem)
dim node as nodeType
dim temp as _mem
dim n as _mem
temp = _memnew(len(node))
nextNode n, cur
node.strLen = len(s$)
if node.strLen > 0 then
node.str = _memnew(len(s$))
_memput node.str, node.str.offset, s$
end if
node.n = n
node.p = cur
_memput temp, temp.offset, node
node = _memget(cur, cur.offset, nodeType)
node.n = temp
_memput cur, cur.offset, node
node = _memget(n, n.offset, nodeType)
node.p = temp
_memput n, n.offset, node
new = temp
end sub
sub addNodePrev (new as _mem, s$, cur as _mem)
dim node as nodeType
dim temp as _mem
dim p as _mem
temp = _memnew(len(node))
prevNode p, cur
node.strLen = len(s$)
if node.strLen > 0 then
node.str = _memnew(len(s$))
_memput node.str, node.str.offset, s$
end if
node.n = cur
node.p = p
_memput temp, temp.offset, node
node = _memget(cur, cur.offset, nodeType)
node.p = temp
_memput cur, cur.offset, node
node = _memget(p, p.offset, nodeType)
node.n = temp
_memput p, p.offset, node
new = temp
end sub
sub rmNode (cur as _mem)
dim node as nodeType
dim n as _mem
dim p as _mem
'remove the string first
node = _memget(cur, cur.offset, nodeType)
if node.strLen > 0 then
_memfree node.str
end if
nextNode n, cur
prevNode p, cur
node = _memget(p, p.offset, nodeType)
node.n = n
_memput p, p.offset, node
node = _memget(n, n.offset, nodeType)
node.p = p
_memput n, n.offset, node
_memfree cur
end sub
sub nextNode (new as _mem, old as _mem)
dim node as nodeType
node = _memget(old, old.offset, nodeType)
new = node.n
end sub
sub prevNode (new as _mem, old as _mem)
dim node as nodeType
node = _memget(old, old.offset, nodeType)
new = node.p
end sub
function readNode$ (cur as _mem)
dim node as nodeType
node = _memget(cur, cur.offset, nodeType)
if node.strLen = 0 then
readNode$ = ""
exit function
end if
s$ = string$(node.strLen, 0)
_memget node.str, node.str.offset, s$
readNode$ = s$
end sub
sub writeNode (cur as _mem, s$)
dim node as nodeType
'remove old string, freeing memory
node = _memget(cur, cur.offset, nodeType)
if node.strLen > 0 then _memfree node.str
'add new string
node.strLen = len(s$)
if node.strLen > 0 then
node.str = _memnew(len(s$))
_memput node.str, node.str.offset, s$
end if
_memput cur, cur.offset, node
end sub
sub newList (new as listType)
dim node as nodeType
new.head = _memnew(len(node))
new.tail = _memnew(len(node))
s$ = "head"
node.strLen = len(s$)
node.str = _memnew(len(s$))
node.n = new.tail
node.p = new.tail
_memput node.str, node.str.offset, s$
_memput new.head, new.head.offset, node
s$ = "tail"
node.strLen = len(s$)
node.str = _memnew(len(s$))
node.n = new.head
node.p = new.head
_memput node.str, node.str.offset, s$
_memput new.tail, new.tail.offset, node
end sub
sub printList (cur as listType)
dim temp as _mem
nextNode temp, cur.head
do
if temp.offset = cur.tail.offset then exit do
print readNode$ (temp)
nextNode temp, temp
loop
end sub
sub rmList (cur as listType)
dim temp as _mem
dim temp2 as _mem
nextNode temp, cur.head
do
if temp.offset = cur.tail.offset then exit do
temp2 = temp
nextNode temp, temp2
rmNode temp2
loop
rmNode cur.head
rmNode cur.tail
end sub