Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Vince's Corner Takeout
#59
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
Reply


Messages In This Thread
Vince's Corner Takeout - by bplus - 04-29-2022, 02:12 PM
RE: Vince's Corner Takeout - by vince - 04-29-2022, 09:34 PM
RE: Vince's Corner Takeout - by vince - 05-02-2022, 03:10 AM
RE: Vince's Corner Takeout - by bplus - 05-02-2022, 04:25 AM
RE: Vince's Corner Takeout - by vince - 05-02-2022, 11:16 PM
RE: Vince's Corner Takeout - by vince - 05-03-2022, 01:10 AM
RE: Vince's Corner Takeout - by bplus - 05-03-2022, 01:15 AM
RE: Vince's Corner Takeout - by vince - 05-03-2022, 04:26 AM
RE: Vince's Corner Takeout - by bplus - 05-03-2022, 03:32 PM
RE: Vince's Corner Takeout - by vince - 05-10-2022, 03:41 AM
RE: Vince's Corner Takeout - by vince - 05-10-2022, 03:57 AM
RE: Vince's Corner Takeout - by dcromley - 05-10-2022, 02:57 PM
RE: Vince's Corner Takeout - by vince - 05-10-2022, 08:14 PM
RE: Vince's Corner Takeout - by SMcNeill - 05-10-2022, 02:59 PM
RE: Vince's Corner Takeout - by vince - 05-11-2022, 01:13 AM
RE: Vince's Corner Takeout - by dcromley - 05-11-2022, 01:58 AM
RE: Vince's Corner Takeout - by vince - 06-01-2022, 09:05 AM
RE: Vince's Corner Takeout - by vince - 08-11-2022, 02:51 AM
RE: Vince's Corner Takeout - by bplus - 06-03-2022, 02:47 PM
RE: Vince's Corner Takeout - by triggered - 06-04-2022, 02:00 AM
RE: Vince's Corner Takeout - by vince - 06-07-2022, 02:02 AM
RE: Vince's Corner Takeout - by bplus - 06-07-2022, 02:15 AM
RE: Vince's Corner Takeout - by vince - 07-13-2022, 05:23 AM
RE: Vince's Corner Takeout - by BSpinoza - 07-14-2022, 04:54 AM
RE: Vince's Corner Takeout - by bplus - 07-14-2022, 04:35 PM
RE: Vince's Corner Takeout - by aurel - 08-11-2022, 01:02 PM
RE: Vince's Corner Takeout - by bplus - 08-11-2022, 04:22 PM
RE: Vince's Corner Takeout - by aurel - 08-11-2022, 05:33 PM
RE: Vince's Corner Takeout - by BSpinoza - 08-12-2022, 03:44 AM
RE: Vince's Corner Takeout - by vince - 08-11-2022, 08:42 PM
RE: Vince's Corner Takeout - by vince - 08-19-2022, 05:00 AM
RE: Vince's Corner Takeout - by bplus - 08-19-2022, 06:33 PM
RE: Vince's Corner Takeout - by vince - 08-23-2022, 10:04 PM
RE: Vince's Corner Takeout - by vince - 11-04-2022, 01:48 AM
RE: Vince's Corner Takeout - by vince - 03-31-2023, 11:07 PM
RE: Vince's Corner Takeout - by vince - 09-18-2023, 11:45 PM
RE: Vince's Corner Takeout - by Dav - 09-19-2023, 12:54 AM
RE: Vince's Corner Takeout - by bplus - 09-19-2023, 01:37 AM
RE: Vince's Corner Takeout - by GareBear - 09-19-2023, 03:56 PM
RE: Vince's Corner Takeout - by bplus - 09-19-2023, 04:47 PM
RE: Vince's Corner Takeout - by vince - 09-19-2023, 06:54 PM
RE: Vince's Corner Takeout - by bplus - 09-19-2023, 09:02 PM
RE: Vince's Corner Takeout - by vince - 01-13-2024, 07:15 PM
RE: Vince's Corner Takeout - by bplus - 01-13-2024, 07:59 PM
RE: Vince's Corner Takeout - by GareBear - 01-13-2024, 10:54 PM
RE: Vince's Corner Takeout - by vince - 02-16-2024, 04:01 AM
RE: Vince's Corner Takeout - by bplus - 02-16-2024, 02:27 PM
RE: Vince's Corner Takeout - by vince - 02-16-2024, 07:16 PM
RE: Vince's Corner Takeout - by bplus - 02-17-2024, 02:44 PM
RE: Vince's Corner Takeout - by vince - 10-07-2024, 08:11 AM
RE: Vince's Corner Takeout - by bplus - 10-07-2024, 01:32 PM
RE: Vince's Corner Takeout - by vince - 10-11-2024, 12:05 AM
RE: Vince's Corner Takeout - by vince - 10-11-2024, 12:16 AM
RE: Vince's Corner Takeout - by PhilOfPerth - 10-11-2024, 03:51 AM
RE: Vince's Corner Takeout - by vince - 10-11-2024, 07:55 PM
RE: Vince's Corner Takeout - by PhilOfPerth - 10-11-2024, 11:30 PM
RE: Vince's Corner Takeout - by vince - 10-15-2024, 10:54 AM
RE: Vince's Corner Takeout - by bplus - 10-15-2024, 01:40 PM
RE: Vince's Corner Takeout - by vince - 03-14-2025, 04:52 PM
RE: Vince's Corner Takeout - by bplus - 03-14-2025, 05:12 PM



Users browsing this thread: 10 Guest(s)