Apply data structure in Visual Basic programming
Chapter Xinsu
(Jiangsu Water Conservancy Research Institute Materials Institute)
Summary: This article discusses a method of using data structures such as array and custom data types such as arrays and custom data types in VB programming.
Keywords: Visual Basic, data structure
1. introduction
The Basic language has a high penetration rate, and Visual Basic has won the intersection of the majority of Basic programmers in the Visual Basic, the Visual Basic, which is very functional, small, easy to get started, and what you see. However, how to build data structures such as numerical calculations, structural calculations, and project management systems, and design the corresponding algorithms, often plagued them. In fact, the structure such as linked list, stack, queue, and binary tree can be described in VB, and to achieve sorting, search, etc., and very flexible mechanism. .
2. Decree of the chain table, stack and queue data structure in VB programming
2.1 The role of array and custom data types
In order to facilitate the understanding of the role of the array, we introduce the concept of the data field and the pointer field, store the value of each element in the data field, and store the position in the array in the pointer field, both correspondence. The upper limit of the pointer points to the position of the first element of the array, the lower limit pointing to the position of the last element. The elements in the array are continuous linear node sequences in memory. This linear data structure is the most widely used and simplest data structure.
Custom Data Types (Type Statement) can include elements of multiple inter-associated data types, VB definition declaration a custom data type must be done at the module layer. Declare a custom data type and define one type of variable.
Example 1 A custom data type named queue declares a fixed size array:
Type Queue
Data as integer 'is used as a data field
Next as integer 'is used as a pointer
End Type
Const max = 10
DIM A (10) As Queue
Set A (i) an element in an array, the pointer of the element points to the array A (10) No. I 1 element, which is subscript of I, the value of the pointer is i. It should be noted that the data structure is different from the data type, which is also different from the object (variable) of the data type declaration. The data structure not only describes the data objects of the data type, but also describes the various operations between the data objects. In order to clarify the role of the custom data type, we specify the value of the variable DATA storage element (as a data field), the variable next stores the pointer to the elements of the element. By using custom data type Queue declares array a (10) and makes special provisions for variable next, we can find that we can store a continuous linear distribution of data in memory in memory in non-linear discontinuous address space, but not Impact us for linear operations.
As such a structure that utilizes a pointer links the structure of each element is referred to as a linked list, the arrays defined by similar example 1 can be used as a linked list.
Example 2 Use Queue to initial A (10) to a one-way link table:
For i = 0 TO 9
A (i) .next = i 1 'i 1 is a pointer for the next element
A (i) .DATA = 10 * RND
Next I
If coupled with statement a (10) .Next = 0 constitutes a one-way circular list. The chain list can be inserted and deleted by changing the pointer.
Top1 Bottom
Figure 1. Circular link queue show
2.2 Stack and Queue
Stack is a data structure that can only be taken into a magazine by one end. In the Visual Basic programming, the stack can be used to achieve recursive effects; or resource recycling due to deletion of array and linked lists, avoiding an embarrassing situation between resource idle, array or linked table length. Stacks can be stored in one-dimensional array or linked list. Using an array to achieve both easy and convenient, at this time, point to the array node with a pointer variable TOP1, each time there is an element into the stack top pointer TOP1 = TOP1 1, A (TOP1) .DATA = 10 * RND, each time there is an element Find TOP1 = TOP1-1, B = A (TOP1) .DATA. When top1 = 0 stack empty, TOP1 is equal to the upper limit of the array. Unlike the stack, the queue is the advanced data structure, and the queue can also be stored in one-dimensional array or linked list. In the team, two pointer variables TOP1, BOTTOM, and finally enter the team's pointer to the head pointer TOP1, the pointer of the most advanced team elements in the team is equal to the tail pointer bottom, when top1 = bottom When the team is empty, the initial condition is top1 = bottom = 0, when top1 1 = bottom (array) or A (TOP1) .Next = Bottom is full. TOP1 = TOP1 1 (array) or top1 = a (TOP1) .Next (Link) when there is an element to come out when the element is out of the team, or bottom = a (bottom) .next ).
Using fixed-size arrays will always encounter a stack full or team full, we can use dynamic arrays to avoid that the dynamic array is an important feature of Visual Basic flexibility and convenience, which can effectively manage memory. In Example 3, the operation of inserting a node in the linked list is inserted by introducing variables LINSHI. The operation of deleting a node in the linked list is similar.
Example 3 The team of the queue and the queue operation, using the circulating chain list defined in Example 1 and assumes that it has been initialized in Example 2.
DIM TOP1 AS INTEGER 'Defines the pointer variable to the header
DIM bottom as integer 'Defines a pointer variable that points to the tail
DIM linshi 'variable
Public Function RemoveQueue (A1 AS Integer) 'Export Function
IF bottom = Top1 the 'bottom = top1 team
Debug.print "Team"
TOP1 = 0: bottom = 0
Else
Bottom = a (bottom) .Next 'Bottom pointer is moved to prepare for the element
J = a (bottom) .DATA 'Element A1
Debug.print "Export, B, J", Bottom, J
END IF
END FUNCTION
Public Function INSERTQUE (Byval A1 AS Integer) 'Enter the team function
IF a (top1) .next = bottom Then 'A (TOP1) .next = BOTTOM team full
MAX = max 1
Redim Preserve A (MAX) As Queue
Linshi = a (top1) .next 'team is full, ready to insert a new node
A (TOP1) .next = max 'Pointer TOP1 = MAX' TOP1 pointer points to the new location to prepare for the new element A1
a (top1) .next = linshi 'new node insert end
A (TOP1) .DATA = A1 'New Element A1 Entering
Else
Top1 = a (TOP1) .next 'Team is dissatisfied, TOP1 pointer is moved, the new element A1 is ready to enter the team
A (TOP1) .DATA = A1 'New Element A1 Entering
Debug.print "Entering, T, I", TOP1, A (TOP1) .DATA
END IF
END FUNCTION
[references]
[1] Zuo Xiaoling Edited. Computer Software Basic Tutorial. Beijing: Electronic Industry Press, 1992
[2] Charles Parly.visual Basic 3.0 Program Design Daquan. Beijing: Electronic Industry Press, 1994