Here is some minimalist code to create and walk linked lists.
There are 3 sections of code:
1 Forward linked lists
2 Backward linked lists
3 Bidirectional linked lists
Anthony Jordan
; ---------------------------------------------------------
; Forward Linked List AKJ 30-Jul-03
; Create a forward-linked list of 4 items, then cycle through all the items.
Type item
Field id
Field nextitem.item ; This field is being recursively defined as custom type 'item'
End Type
; Create the list
address.item = Null ; Variable to point to items in the list
For i=1 To 4
mylist.item=New item
mylist\id=i
If address<>Null Then address\nextitem=mylist ; Forward link of the previous item
address = mylist ; Address of the current item
Next ; i
; Fill in the forward link of the last item
address\nextitem=First item
; Now walk the links and display the list
mylist = First item ; Arbitrary starting point
For i=1 To 10 ; Walk the list repeatedly
Print " "+mylist\id+" "+Str$(mylist\nextitem)
mylist = mylist\nextitem ; Use the link to go to the next item in the list
Next ; i
WaitKey()
; ---------------------------------------------------------
; Backward Linked List AKJ 30-Jul-03
; Create a backward-linked list of 4 items, then cycle through all the items.
Type item
Field id
Field previtem.item ; This field is being recursively defined as custom type 'item'
End Type
; Create the list
address.item = Null ; Variable to point to items in the list
For i=1 To 4
mylist.item=New item
mylist\id=i
mylist\previtem=address ; Address of the previous item
address = mylist ; Address of the current item
Next ; i
; Fill in the backward link of the first item
mylist = First item
mylist\previtem=address
; Now walk the links and display the list
mylist = First item ; Arbitrary starting point
For i=1 To 10 ; Walk the list repeatedly
Print " "+mylist\id+" "+Str$(mylist\previtem)
mylist = mylist\previtem ; Use the link to go to the previous item in the list
Next ; i
WaitKey()
; ---------------------------------------------------------
; Bidirectional Linked List AKJ 30-Jul-03
; Create a bidirectional-linked list of 4 items, then cycle through all the items.
Type item
Field id
Field previtem.item ; This field is being recursively defined as custom type 'item'
Field nextitem.item ; Also recursively defined
End Type
; Create the list
address.item = Null ; Variable to point to items in the list
For i=1 To 4
mylist.item=New item
mylist\id=i
If address<>Null
mylist\previtem=address ; Backward link of the current item
address\nextitem=mylist ; Forward link of the previous item
EndIf
address = mylist ; Address of the current item
Next ; i
; Fill in the backward link of the first item
mylist = First item
mylist\previtem=address
; Fill in the forward link of the last item
address\nextitem=mylist
; Now walk the links and display the list
mylist = First item ; Arbitrary starting point
For i=1 To 10 ; Walk the forward links
Print " "+mylist\id+" "+Str$(mylist\previtem)+" "+Str$(mylist\nextitem)
mylist = mylist\nextitem ; Use the link to go to the next item in the list
Next ; i
Print ""
For i=1 To 10 ; Walk the backward links
Print " "+mylist\id
mylist = mylist\previtem ; Use the link to go to the previous item in the list
Next ; i
WaitKey()
There are 3 sections of code:
1 Forward linked lists
2 Backward linked lists
3 Bidirectional linked lists
Anthony Jordan
; ---------------------------------------------------------
; Forward Linked List AKJ 30-Jul-03
; Create a forward-linked list of 4 items, then cycle through all the items.
Type item
Field id
Field nextitem.item ; This field is being recursively defined as custom type 'item'
End Type
; Create the list
address.item = Null ; Variable to point to items in the list
For i=1 To 4
mylist.item=New item
mylist\id=i
If address<>Null Then address\nextitem=mylist ; Forward link of the previous item
address = mylist ; Address of the current item
Next ; i
; Fill in the forward link of the last item
address\nextitem=First item
; Now walk the links and display the list
mylist = First item ; Arbitrary starting point
For i=1 To 10 ; Walk the list repeatedly
Print " "+mylist\id+" "+Str$(mylist\nextitem)
mylist = mylist\nextitem ; Use the link to go to the next item in the list
Next ; i
WaitKey()
; ---------------------------------------------------------
; Backward Linked List AKJ 30-Jul-03
; Create a backward-linked list of 4 items, then cycle through all the items.
Type item
Field id
Field previtem.item ; This field is being recursively defined as custom type 'item'
End Type
; Create the list
address.item = Null ; Variable to point to items in the list
For i=1 To 4
mylist.item=New item
mylist\id=i
mylist\previtem=address ; Address of the previous item
address = mylist ; Address of the current item
Next ; i
; Fill in the backward link of the first item
mylist = First item
mylist\previtem=address
; Now walk the links and display the list
mylist = First item ; Arbitrary starting point
For i=1 To 10 ; Walk the list repeatedly
Print " "+mylist\id+" "+Str$(mylist\previtem)
mylist = mylist\previtem ; Use the link to go to the previous item in the list
Next ; i
WaitKey()
; ---------------------------------------------------------
; Bidirectional Linked List AKJ 30-Jul-03
; Create a bidirectional-linked list of 4 items, then cycle through all the items.
Type item
Field id
Field previtem.item ; This field is being recursively defined as custom type 'item'
Field nextitem.item ; Also recursively defined
End Type
; Create the list
address.item = Null ; Variable to point to items in the list
For i=1 To 4
mylist.item=New item
mylist\id=i
If address<>Null
mylist\previtem=address ; Backward link of the current item
address\nextitem=mylist ; Forward link of the previous item
EndIf
address = mylist ; Address of the current item
Next ; i
; Fill in the backward link of the first item
mylist = First item
mylist\previtem=address
; Fill in the forward link of the last item
address\nextitem=mylist
; Now walk the links and display the list
mylist = First item ; Arbitrary starting point
For i=1 To 10 ; Walk the forward links
Print " "+mylist\id+" "+Str$(mylist\previtem)+" "+Str$(mylist\nextitem)
mylist = mylist\nextitem ; Use the link to go to the next item in the list
Next ; i
Print ""
For i=1 To 10 ; Walk the backward links
Print " "+mylist\id
mylist = mylist\previtem ; Use the link to go to the previous item in the list
Next ; i
WaitKey()