Buchempfehlung
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Umfassend, aber leicht verständlich führt dieses Buch in die Programmierung von ATMEL AVR Mikrocontrollern ein. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

linkedList.bas

Uploader:MitgliedStringEpsilon
Datum/Zeit:30.01.2015 00:24:24

' Doppelt verkettete Liste, geschrieben in FreeBASIC
' (c) 2011 MilkFreeze
' Refactored by StringEpsilon, 2015.

#MACRO DeclareList(datatype )

type ##datatype##ListElement extends object
    previousElement as ##datatype##ListElement ptr
    nextElement as ##datatype##ListElement ptr
    element as datatype ptr

    declare constructor()
    declare destructor()
end type

constructor ##datatype##ListElement()
   this.element = new datatype
end constructor

destructor ##datatype##ListElement()
    delete this.nextElement
    delete this.element

    this.previousElement = 0
end destructor

TYPE ##datatype##List extends object
    PRIVATE:
        _first as ##datatype##ListElement ptr
        _last as ##datatype##ListElement ptr
        _count as uinteger

        declare function GetElement(index as uinteger) as  ##datatype##ListElement ptr
    PUBLIC:
        declare property item(index as uinteger) as datatype
        declare property item(index as uinteger, value as datatype)

        declare operator [](index as uinteger) as datatype


        declare sub append (newItem as datatype)
        declare sub insert (index as uinteger, newItem as datatype)
        declare sub clearList ()
        declare sub remove (index as uinteger)

        declare function count () as uinteger

        declare constructor ()
        declare destructor ()

        declare operator LET (new_list as ##datatype##List)
end TYPE

Constructor ##datatype##List()
    this._count = 0
end constructor

destructor ##datatype##List()
    if (this._count <> 0) then
        delete this._first
        this._first = 0
        this._last  = 0
        this._count = 0
    end if
end destructor

sub ##datatype##List.ClearList()
    this.destructor()
end sub

function ##datatype##List.GetElement(index as uinteger) as ##datatype##ListElement ptr
    if ( index <= 0 ) then return this._first
    dim p_tmp_item as ##datatype##ListElement ptr
    dim i as integer
    if (index < (this._count-1) / 2) then
        i = 0
        p_tmp_item = this._first
        while (i < index)
            p_tmp_item = p_tmp_item->nextElement
            i += 1
        wend
    else
        i = this._count - 1
        p_tmp_item = this._last
        while (i > index)
            p_tmp_item = p_tmp_item->previousElement
            i -= 1
        wend
    end if

    return p_tmp_item
end function

sub ##datatype##List.remove (index as uinteger)
    if (index <= 0 OR index > this._count) then exit sub
    dim element as ##datatype##ListElement ptr

    element = this.GetElement(index)
    'Special case for end of the list:
    if ( element->nextElement = 0 ) then
        this._last = element->previousElement
        this._last->nextElement = 0
    else
        element->nextElement->previousElement = element->previousElement
        if ( element->previousElement <> 0 ) then
            element->previousElement->nextElement = element->nextElement
        end if
    end if

    if ( index = 0 ) then
    _first = element->nextElement
    end if

    element->nextElement = 0
    delete element
    this._count -= 1
end sub

sub ##datatype##List.insert (index as uinteger, newItem as datatype)
    if (index >= this._count ) then
        this.append(newItem)
    elseif (index <= this._count AND index > 0 ) then
        dim nextElement as ##datatype##ListElement ptr  =  this.GetElement(index)
        dim previousElement as ##datatype##ListElement ptr = nextElement->previousElement

        dim newElement as ##datatype##ListElement ptr = NEW ##datatype##ListElement
        *(newElement->element) = newItem

        newElement->nextElement = nextElement
        nextElement->previousElement = newElement

        if ( index = 1 ) then
            this._first = newElement
        else
            previousElement->nextElement = newElement
            newElement->previousElement = previousElement
        end if

        this._count += 1
    end if
end sub

sub ##datatype##List.append(newItem as datatype)
    if (this._first = 0 ) then
        this._first = NEW ##datatype##ListElement
        *this._first->element = newItem
        this._last = this._first
        this._last->previousElement = this._first
    elseif (_last <> 0) then
        this._last->nextElement = NEW ##datatype##ListElement
        this._last->nextElement->previousElement = this._last
        this._last = this._last->nextElement
        *this._last->element = newItem
    end if
    _count += 1
end sub

property ##datatype##List.item(index as uinteger) as datatype
    return (*this.GetElement(index)->element)
end property

property ##datatype##List.item(index as uinteger, value as datatype)
    *this.GetElement(index)->element = value
end property

function ##datatype##List.count() as uinteger
    return this._count
end function

operator ##datatype##List.let(new_list as ##datatype##List)
    if (@this = @new_list OR new_list.count = 0) then exit operator
    dim i as integer
    this.destructor()

    for i as integer = 0 to new_list.count() -1
        this.append( new_list.item(i) )
    next
end operator

#endMACRO