Post by gaviero on Nov 4, 2004 23:20:20 GMT -5
Warning! This example is not for beginners. The only reason for writing this code was for the challenge ;D For those of you who have used a stack in C or C++, here's my Basic4GL implementation. Honestly, I wasn't sure if this would work. If you really think you need a linked list, you probably are using the wrong language!
gaviero.home.comcast.net/cannonbob
dim list_count ' Number of Elements in list
dim list_element_content$ ' For adding text to a new Element
dim list_element_value# ' For adding a real to a new Element
dim index ' Loop iterator
' Each list element uses this structure
' For this demo, I decided to store a string and real number in each element.
struc Element
dim content$ ' string
dim value# ' real
dim Element &ptrnext ' next element in the list
endstruc
dim Element &list_cursor ' always points to the last item in the list
dim Element list_first ' first item (always need one!)
' List initialization start
list_first.content$ = "First (Head)."
list_first.value# = 1.234 ' Dummy data
list_count = 1
&list_cursor = &list_first
' List intialization end
printr "The list with one element:"
gosub list_print
printr "Add two more items:"
list_element_content$ = "Second."
list_element_value# = 2.344
gosub list_push_element
list_element_content$ = "Third."
list_element_value# = 3.123
gosub list_push_element
gosub list_print
list_element_content$ = "Fourth."
list_element_value# = 1.111
gosub list_push_element
gosub list_print
printr "Shorten list by 2:"
gosub list_pop_element
gosub list_pop_element
gosub list_print
printr "Add new element:"
list_element_content$ = "Last."
list_element_value# = 1.211
gosub list_push_element
gosub list_print
printr "Add new element:"
list_element_content$ = "Last again."
list_element_value# = 1.211
gosub list_push_element
gosub list_print
printr "Add new element:"
list_element_content$ = "The End."
list_element_value# = 2.11
gosub list_push_element
gosub list_print
end
' Print all items in the list
list_print:
&list_cursor = &list_first
for index = 1 to list_count
printr " " + list_cursor.content$ + ", " + list_cursor.value#
if index <> list_count then
&list_cursor = &list_cursor.ptrnext
endif
next
return
' Add an item to the end of the list. Initializes the new item using
' two global variables
list_push_element:
if &list_cursor.ptrnext = null then ' there is no tail to be reused
alloc list_cursor.ptrnext
else
printr "(Element reused)"
endif
list_cursor.ptrnext.content$ = list_element_content$
list_cursor.ptrnext.value# = list_element_value#
&list_cursor = &list_cursor.ptrnext
list_count = list_count + 1
return
' Remove the last item in the list
' Probably not a wise idea to do this since memory stays allocated.
' On the other hand, if the 'tail' pointed to by the list cursor is not null
' (a tail was previously removed), then a new alloc does not occur.
' In other words, the list size in memory is as large as the list when at it longest.
list_pop_element:
if list_count > 1 then
list_count = list_count - 1
' reset cursor to tail
&list_cursor = &list_first
for index = 1 to list_count
if index <> list_count then
&list_cursor = &list_cursor.ptrnext
endif
next
endif
return
gaviero.home.comcast.net/cannonbob