Guest User

Untitled

a guest
Jun 9th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. '''''''''''''''''''''''''''''''''''''''''''''''''
  3. '''''''''''''''''''''''''''''''''''''''''''''''''
  4. '' BEGINNING OF HEADER INFO FOR LIST FUNCTIONS ''
  5. '''''''''''''''''''''''''''''''''''''''''''''''''
  6. '''''''''''''''''''''''''''''''''''''''''''''''''
  7.  
  8. Const NULL As Any Ptr = 0
  9.  
  10. Type ListNode
  11.     pdata As Any Ptr
  12.     index As Integer
  13.     prev_node As ListNode Ptr = NULL
  14.     next_node As ListNode Ptr = NULL
  15. End Type
  16.  
  17. Type List
  18.     start_node As ListNode Ptr
  19.     current_node As ListNode Ptr
  20.     current_index As Integer
  21. End Type
  22.  
  23. Declare Function ListCreate() As ListNode Ptr
  24. Declare Function ListGetNode(pln As ListNode Ptr, index As Integer) As ListNode Ptr
  25. Declare Function ListUBound(pln As ListNode Ptr) As Integer
  26. Declare Function ListAddNodeToEnd(pln As ListNode Ptr) As ListNode Ptr
  27. Declare Function ListGetLastNode(pln As ListNode Ptr) As ListNode Ptr
  28. Declare Function ListRemoveNodeFromEnd(pln As ListNode Ptr) As ListNode Ptr
  29. Declare Function ListRemoveNode(pln As ListNode Ptr, index As Integer) As ListNode Ptr
  30. Declare Function ListInsertNode(pln As ListNode Ptr, index As Integer) As ListNode Ptr
  31. Declare Function ListGetData(pln As ListNode Ptr, index As Integer) As Any Ptr
  32. Declare Sub ListDestroy(pln As ListNode Ptr)
  33.  
  34. '''''''''''''''''''''''''''''''''''''''''''''''''
  35. '''''''''''''''''''''''''''''''''''''''''''''''''
  36. '' END OF HEADER INFO FOR LIST FUNCTIONS       ''
  37. '''''''''''''''''''''''''''''''''''''''''''''''''
  38. '''''''''''''''''''''''''''''''''''''''''''''''''
  39.  
  40. 'Test code here
  41. Dim my_list As List
  42.  
  43. Dim i As Integer
  44.  
  45. 'Create a list with 10 nodes with random integer data
  46. Print "Create list..."
  47. my_list.start_node = ListCreate()
  48. Print "List created"
  49.  
  50. my_list.current_node = my_list.start_node
  51.  
  52. Print "Allocate Data to list..."
  53. my_list.current_node->pdata = Allocate(SizeOf(Integer))
  54. For i = 0 To 10
  55.     my_list.current_node = ListAddNodeToEnd(my_list.start_node)
  56.     my_list.current_node->pdata = Allocate(SizeOf(Integer))
  57.     *CPtr(Integer Ptr, my_list.current_node->pdata) = i
  58. Next
  59.  
  60. Print "Data allocated."
  61. Sleep
  62.  
  63. For i = 0 To 10
  64.     Print "Data in node: ", *CPtr(Integer Ptr, ListGetData(my_list.start_node, i))
  65. Next
  66.  
  67. ListDestroy(my_list.start_node)
  68. Sleep
  69.  
  70. 'Function ListCreate()
  71. Function ListCreate() As ListNode Ptr
  72.     Dim t_pln As ListNode Ptr
  73.    
  74.     t_pln = Callocate(1, SizeOf(ListNode))
  75.     Return t_pln
  76. End Function
  77.  
  78. 'Function ListGetNode
  79. Function ListGetNode(pln As ListNode Ptr, index As Integer) As ListNode Ptr
  80.     Dim t_pln As ListNode Ptr
  81.     t_pln = pln
  82.     While (t_pln->next_node <> NULL)
  83.         If (t_pln->index = index) Then
  84.             Return t_pln
  85.         Else
  86.             t_pln = t_pln->next_node
  87.         EndIf
  88.     Wend
  89.    
  90.     Return NULL
  91. End Function
  92.  
  93. 'Function ListUBound
  94. Function ListUBound(pln As ListNode Ptr) As Integer
  95.     Dim i As Integer
  96.     Dim t_pln As ListNode Ptr
  97.     t_pln = pln
  98.     While (t_pln->next_node <> NULL)
  99.         t_pln = t_pln->next_node
  100.         i += 1
  101.     Wend
  102.     Return i
  103. End Function
  104.  
  105. 'Function ListAddNodeToEnd
  106. Function ListAddNodeToEnd(pln As ListNode Ptr) As ListNode Ptr
  107.     Dim last_pln As ListNode Ptr
  108.     Dim new_pln As ListNode Ptr
  109.     Print "test a"
  110.     last_pln = ListGetLastNode(pln)
  111.     Print "test b"
  112.     new_pln = Callocate(1, SizeOf(ListNode))
  113.     last_pln->next_node = new_pln
  114.     new_pln->prev_node = last_pln
  115.     new_pln->index = last_pln->index + 1
  116.    
  117.     Return new_pln
  118. End Function
  119.  
  120. 'Function ListGetLastNode
  121. Function ListGetLastNode(pln As ListNode Ptr) As ListNode Ptr
  122.     Dim last_node_i As Integer
  123.     Dim t_pln As ListNode Ptr
  124.    
  125.     Print "test c"
  126.     last_node_i = ListUBound(pln)
  127.     Print "test d"
  128.     t_pln = ListGetNode(pln, last_node_i)
  129.     Print "test e"
  130.     Return t_pln
  131. End Function
  132.  
  133. 'Function ListRemoveNodeFromEnd
  134. Function ListRemoveNodeFromEnd(pln As ListNode Ptr) As ListNode Ptr
  135.     Dim newlast_pln As ListNode Ptr
  136.     Dim oldlast_pln As ListNode Ptr
  137.    
  138.     oldlast_pln = ListGetLastNode(pln)
  139.     newlast_pln = oldlast_pln->prev_node
  140.     DeAllocate(oldlast_pln->pdata)
  141.     DeAllocate(oldlast_pln)
  142.     newlast_pln->next_node = NULL
  143.    
  144.     Return newlast_pln
  145. End Function
  146.  
  147. 'Function ListRemoveNode
  148. Function ListRemoveNode(pln As ListNode Ptr, index As Integer) As ListNode Ptr
  149.     Dim t_pln As ListNode Ptr
  150.     Dim prev_pln As ListNode Ptr
  151.     Dim next_pln As ListNode Ptr
  152.    
  153.     'Get node at index
  154.     t_pln = ListGetNode(pln, index)
  155.    
  156.     'Get the previous and next nodes in the structure
  157.     prev_pln = t_pln->prev_node
  158.     next_pln = t_pln->next_node
  159.    
  160.     'Delete this node's data
  161.     DeAllocate(t_pln->pdata)
  162.    
  163.     'Delete this node
  164.     DeAllocate(t_pln)
  165.  
  166.     'Set the next_node of the previous node to the next node
  167.     prev_pln->next_node = next_pln
  168.    
  169.     'Set the prev_node of the next node to the previous node
  170.     next_pln->prev_node = prev_pln
  171.    
  172.     'Set the current node to the next_pln
  173.     t_pln = next_pln
  174.    
  175.     'Subtract the indices from every node following prev_pln by 1
  176.     Do
  177.         next_pln->index -= 1
  178.         next_pln = next_pln->next_node
  179.     Loop While (next_pln->next_node <> NULL)
  180.        
  181.     'Return t_pln, or current node at specified index
  182.     Return t_pln
  183. End Function
  184.  
  185.  
  186. 'Function ListInsertNode
  187. Function ListInsertNode(pln As ListNode Ptr, index As Integer) As ListNode Ptr
  188.     Dim new_pln As ListNode Ptr
  189.     Dim t_pln As ListNode Ptr
  190.     Dim prev_pln As ListNode Ptr
  191.     Dim next_pln As ListNode Ptr
  192.    
  193.     'Get node at index
  194.     t_pln = ListGetNode(pln, index)
  195.    
  196.     'Get previous and next nodes from it
  197.     prev_pln = t_pln->prev_node
  198.     next_pln = t_pln->next_node
  199.    
  200.     'Create new node to insert at index
  201.     new_pln = Callocate(1, SizeOf(ListNode))
  202.    
  203.     'Fill in new node with node info
  204.     new_pln->index = index
  205.     new_pln->prev_node = prev_pln
  206.     new_pln->next_node = next_pln
  207.    
  208.     'Fill in surrounding nodes with info on new node
  209.     prev_pln->next_node = new_pln
  210.     next_pln->prev_node = new_pln
  211.    
  212.     'Add one to all subsequent indices after the new node.
  213.     Do
  214.         next_pln->index += 1
  215.         next_pln = next_pln->next_node
  216.     Loop While(next_pln->next_node <> NULL)
  217.    
  218.     Return new_pln
  219. End Function
  220.  
  221. 'Function ListGetData
  222. Function ListGetData(pln As ListNode Ptr, index As Integer) As Any Ptr
  223.     Dim t_pln As ListNode Ptr
  224.     t_pln = ListGetNode(pln, index)
  225.    
  226.     Return t_pln->pdata
  227. End Function
  228.  
  229. 'Sub ListDestroy
  230. Sub ListDestroy(pln As ListNode Ptr)
  231.     Dim i As Integer
  232.     Dim t_pln As ListNode Ptr
  233.     Dim t_pln2 As ListNode Ptr
  234.    
  235.     t_pln = pln
  236.     Do
  237.         DeAllocate(t_pln->pdata)
  238.         If t_pln->next_node = NULL Then
  239.             i = 1
  240.         Else
  241.             t_pln2 = t_pln
  242.         EndIf
  243.         DeAllocate(t_pln)
  244.         t_pln = t_pln2
  245.     Loop Until i = 1
  246. End Sub
Add Comment
Please, Sign In to add comment