'Singly linked List, basic functions, no checks

$ filename "SinglyLinkedList.exe"
'uses rtl32
'uses rtl64

uses console

type record
===========
    int num
    bstring name    'not string
    record *nextptr 'pointer to next record
end type

class SinglyLinkedList
======================
record startRecord

sub constructor()
end sub

sub destructor()
    record *List
    @list = @startRecord.nextptr
    while @List
        frees list.name
        sys p=@list
        @List = @List.nextptr
        freememory p         'free prior record
    wend
end sub

function newRecord() as record*
    sys address = getmemory sizeof(record)
    return address
end function

sub createRecords(int n)
    record *List
    int num = 51
    int i
    '
    @List=@StartRecord
    for i = 1 to n
        @List.nextptr = newRecord()
        @List = @List.nextptr
        List.num = num
        List.name = "NewName_" + str(num)
        printl "Created data for node " + i + " = " + List.num + " " + List.name  + ", nextptr = " + @List
        num += 1
    next i
end sub

sub prependRecord()
    static int num=100
    record *prepend, *temp

    @prepend = newRecord()
    num+=1
    prepend.num = num
    prepend.name = "PrependName_" + str(num)

    @temp = @startRecord.nextptr
    @startRecord.nextptr = @prepend
    @prepend.nextptr = @temp

    printl "Prepend data at beginning " + " = " + prepend.num + " " + prepend.name  + ", nextptr = " + @prepend.nextptr
end sub

sub appendRecord()
    static int num=400
    record *append, *temp

    @append = newRecord()
    num+=1
    append.num = num
    append.name = "AppendName_" + str(num)

    @temp = startRecord.nextptr
    while @temp.nextptr
        @temp = @temp.nextptr
    wend
    @temp.nextptr = @append

    printl "Append data at end" + " = " + append.num + " " + append.name  + ", nextptr = " + @append
end sub

sub insertRecord(int pos)
    static int num=1000
    record *insert, *temp

    @insert = newRecord()
    num+=1
    insert.num = num
    insert.name = "InsertName_" +str(num)

    @temp = startRecord.nextptr
    if pos = 1 then
        @startRecord.nextptr = @insert
        @insert.nextptr = @temp
    else
        int i
        'no check if valid pos
        for i=2 to pos-1
            @temp = @temp.nextptr
        next i
        @insert.nextptr = @temp.nextptr
        @temp.nextptr = @insert
    end if
    printl "Insert data at position " + pos " = " + insert.num + " " + insert.name  + ", nextptr = " + @insert.nextptr
end sub

sub deleteRecord(int pos)
    record *delRecord, *previous

    @delRecord = startRecord.nextptr
    @previous = startRecord.nextptr
    int i
    for i=2 to pos
        'no check if valid pos
        @previous = @delRecord
        @delRecord = @delRecord.nextptr
    next i
    if @delRecord = @startRecord.nextptr then
        @startRecord.nextptr = @delRecord.nextptr
    else
        @previous.nextptr = @delRecord.nextptr
        @delRecord.nextptr = 0
    end if

    printl "Delete data at position " + pos " = " + delRecord.num + " " + delRecord.name  + ", nextptr = " + @delRecord.nextptr
    frees delRecord.name
    freememory @delRecord
end sub

sub reverseRecords()
    record *previous, *current, *temp

    @current = startRecord.nextptr
    while @current
        @temp = @current
        @current = @current.nextptr
        @temp.nextptr = @previous
        @previous = @temp
    wend
    @startRecord.nextptr = @previous
    printl : printl "Reverse List"
end sub

sub displayList()
    record *List
    @list = @startRecord.nextptr

    printl : printl "Display List"
    int ix = 0
    while @List
        ix += 1
        printl "Data Node " + ix + " = " + List.num + " " + List.name  + ", nextptr = " + @List
        sys *tmp : @tmp = @List
        @List = @List.nextptr
        if @List = 0 then
            printl "nextptr " + @tmp + ": Empty List, nextptr = " + @List
            exit while
        end if

    wend
end sub

end class 'Singly LinkedList
============================

sub main()
    new SinglyLinkedList li
    int n = 5   'number of created Records

    print  "Create and manage Singly Linked List -- basic functions, no checks"
    printl "------------------------------------------------------------------" + cr

    li.createRecords(n)
    printl
    li.displayList()
    printl "Enter ... " : waitkey

    li.reverseRecords()
    printl
    li.displayList()
    printl "Enter ... " : waitkey

    li.reverseRecords()
    li.prependRecord()
    li.prependRecord()
    li.displayList()
    printl "Enter ... " : waitkey
    printl

    li.appendRecord()
    li.appendRecord()
    li.displayList()
    printl "Enter ... " : waitkey
    printl

    li.insertRecord(1)
    li.insertRecord(4)
    li.displayList()
    printl "Enter ... " : waitkey
    printl

    li.deleteRecord(1)
    li.deleteRecord(5)
    li.displayList()
    printl "Enter ... " : waitkey

    li.reverseRecords()
    printl
    li.displayList()

    printl
    printl "Enter to end ..."
    del li
    waitkey
end sub

main()
