Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''''''''''''''''''''''''''''''''''''''''''''''''
- '''''''''''''''''''''''''''''''''''''''''''''''''
- '' BEGINNING OF HEADER INFO FOR LIST FUNCTIONS ''
- '''''''''''''''''''''''''''''''''''''''''''''''''
- '''''''''''''''''''''''''''''''''''''''''''''''''
- Const NULL As Any Ptr = 0
- Type ListNode
- pdata As Any Ptr
- index As Integer
- prev_node As ListNode Ptr = NULL
- next_node As ListNode Ptr = NULL
- End Type
- Type List
- start_node As ListNode Ptr
- current_node As ListNode Ptr
- current_index As Integer
- End Type
- Declare Function ListCreate() As ListNode Ptr
- Declare Function ListGetNode(pln As ListNode Ptr, index As Integer) As ListNode Ptr
- Declare Function ListUBound(pln As ListNode Ptr) As Integer
- Declare Function ListAddNodeToEnd(pln As ListNode Ptr) As ListNode Ptr
- Declare Function ListGetLastNode(pln As ListNode Ptr) As ListNode Ptr
- Declare Function ListRemoveNodeFromEnd(pln As ListNode Ptr) As ListNode Ptr
- Declare Function ListRemoveNode(pln As ListNode Ptr, index As Integer) As ListNode Ptr
- Declare Function ListInsertNode(pln As ListNode Ptr, index As Integer) As ListNode Ptr
- Declare Function ListGetData(pln As ListNode Ptr, index As Integer) As Any Ptr
- Declare Sub ListDestroy(pln As ListNode Ptr)
- '''''''''''''''''''''''''''''''''''''''''''''''''
- '''''''''''''''''''''''''''''''''''''''''''''''''
- '' END OF HEADER INFO FOR LIST FUNCTIONS ''
- '''''''''''''''''''''''''''''''''''''''''''''''''
- '''''''''''''''''''''''''''''''''''''''''''''''''
- 'Test code here
- Dim my_list As List
- Dim i As Integer
- 'Create a list with 10 nodes with random integer data
- Print "Create list..."
- my_list.start_node = ListCreate()
- Print "List created"
- my_list.current_node = my_list.start_node
- Print "Allocate Data to list..."
- my_list.current_node->pdata = Allocate(SizeOf(Integer))
- For i = 0 To 10
- my_list.current_node = ListAddNodeToEnd(my_list.start_node)
- my_list.current_node->pdata = Allocate(SizeOf(Integer))
- *CPtr(Integer Ptr, my_list.current_node->pdata) = i
- Next
- Print "Data allocated."
- Sleep
- For i = 0 To 10
- Print "Data in node: ", *CPtr(Integer Ptr, ListGetData(my_list.start_node, i))
- Next
- ListDestroy(my_list.start_node)
- Sleep
- 'Function ListCreate()
- Function ListCreate() As ListNode Ptr
- Dim t_pln As ListNode Ptr
- t_pln = Callocate(1, SizeOf(ListNode))
- Return t_pln
- End Function
- 'Function ListGetNode
- Function ListGetNode(pln As ListNode Ptr, index As Integer) As ListNode Ptr
- Dim t_pln As ListNode Ptr
- t_pln = pln
- While (t_pln->next_node <> NULL)
- If (t_pln->index = index) Then
- Return t_pln
- Else
- t_pln = t_pln->next_node
- EndIf
- Wend
- Return NULL
- End Function
- 'Function ListUBound
- Function ListUBound(pln As ListNode Ptr) As Integer
- Dim i As Integer
- Dim t_pln As ListNode Ptr
- t_pln = pln
- While (t_pln->next_node <> NULL)
- t_pln = t_pln->next_node
- i += 1
- Wend
- Return i
- End Function
- 'Function ListAddNodeToEnd
- Function ListAddNodeToEnd(pln As ListNode Ptr) As ListNode Ptr
- Dim last_pln As ListNode Ptr
- Dim new_pln As ListNode Ptr
- Print "test a"
- last_pln = ListGetLastNode(pln)
- Print "test b"
- new_pln = Callocate(1, SizeOf(ListNode))
- last_pln->next_node = new_pln
- new_pln->prev_node = last_pln
- new_pln->index = last_pln->index + 1
- Return new_pln
- End Function
- 'Function ListGetLastNode
- Function ListGetLastNode(pln As ListNode Ptr) As ListNode Ptr
- Dim last_node_i As Integer
- Dim t_pln As ListNode Ptr
- Print "test c"
- last_node_i = ListUBound(pln)
- Print "test d"
- t_pln = ListGetNode(pln, last_node_i)
- Print "test e"
- Return t_pln
- End Function
- 'Function ListRemoveNodeFromEnd
- Function ListRemoveNodeFromEnd(pln As ListNode Ptr) As ListNode Ptr
- Dim newlast_pln As ListNode Ptr
- Dim oldlast_pln As ListNode Ptr
- oldlast_pln = ListGetLastNode(pln)
- newlast_pln = oldlast_pln->prev_node
- DeAllocate(oldlast_pln->pdata)
- DeAllocate(oldlast_pln)
- newlast_pln->next_node = NULL
- Return newlast_pln
- End Function
- 'Function ListRemoveNode
- Function ListRemoveNode(pln As ListNode Ptr, index As Integer) As ListNode Ptr
- Dim t_pln As ListNode Ptr
- Dim prev_pln As ListNode Ptr
- Dim next_pln As ListNode Ptr
- 'Get node at index
- t_pln = ListGetNode(pln, index)
- 'Get the previous and next nodes in the structure
- prev_pln = t_pln->prev_node
- next_pln = t_pln->next_node
- 'Delete this node's data
- DeAllocate(t_pln->pdata)
- 'Delete this node
- DeAllocate(t_pln)
- 'Set the next_node of the previous node to the next node
- prev_pln->next_node = next_pln
- 'Set the prev_node of the next node to the previous node
- next_pln->prev_node = prev_pln
- 'Set the current node to the next_pln
- t_pln = next_pln
- 'Subtract the indices from every node following prev_pln by 1
- Do
- next_pln->index -= 1
- next_pln = next_pln->next_node
- Loop While (next_pln->next_node <> NULL)
- 'Return t_pln, or current node at specified index
- Return t_pln
- End Function
- 'Function ListInsertNode
- Function ListInsertNode(pln As ListNode Ptr, index As Integer) As ListNode Ptr
- Dim new_pln As ListNode Ptr
- Dim t_pln As ListNode Ptr
- Dim prev_pln As ListNode Ptr
- Dim next_pln As ListNode Ptr
- 'Get node at index
- t_pln = ListGetNode(pln, index)
- 'Get previous and next nodes from it
- prev_pln = t_pln->prev_node
- next_pln = t_pln->next_node
- 'Create new node to insert at index
- new_pln = Callocate(1, SizeOf(ListNode))
- 'Fill in new node with node info
- new_pln->index = index
- new_pln->prev_node = prev_pln
- new_pln->next_node = next_pln
- 'Fill in surrounding nodes with info on new node
- prev_pln->next_node = new_pln
- next_pln->prev_node = new_pln
- 'Add one to all subsequent indices after the new node.
- Do
- next_pln->index += 1
- next_pln = next_pln->next_node
- Loop While(next_pln->next_node <> NULL)
- Return new_pln
- End Function
- 'Function ListGetData
- Function ListGetData(pln As ListNode Ptr, index As Integer) As Any Ptr
- Dim t_pln As ListNode Ptr
- t_pln = ListGetNode(pln, index)
- Return t_pln->pdata
- End Function
- 'Sub ListDestroy
- Sub ListDestroy(pln As ListNode Ptr)
- Dim i As Integer
- Dim t_pln As ListNode Ptr
- Dim t_pln2 As ListNode Ptr
- t_pln = pln
- Do
- DeAllocate(t_pln->pdata)
- If t_pln->next_node = NULL Then
- i = 1
- Else
- t_pln2 = t_pln
- EndIf
- DeAllocate(t_pln)
- t_pln = t_pln2
- Loop Until i = 1
- End Sub
Add Comment
Please, Sign In to add comment