江苏省高校计算机等级考试命题研究院 江苏省高校计算机等级考试辅导
二级VB常用算法(七)数组元素插入删除- -

VB常用算法(七)数组元素插入删除- -

曹苏群  http://caosuqun.bokee.com

Tag插入    删除    算法                                          

1、算法说明

数组中元素的插入和删除一般是在已固定序列的数组中插入或删除一个元素,使得插入或删除操作后的数组还是有序的。

基本思路:首先要找到插入位置或要删除的元素。

1)        插入

代码如下:

Private Sub Command1_Click()

    Dim a(10) As Integer

    Dim i As Integer, k As Integer

    For i = 0 To 9       '生成数组

        a(i) = i * 3 + 1

    Print a(i);

    Next i

    Print

    Print "插入14"

    For k = 0 To 9       '查找插入14在数组中的位置

        If 14 < a(k) Then Exit For

    Next k

    For i = 9 To k Step -1 '从最后元素开始逐个后移,腾出位置

        a(i + 1) = a(i)

    Next i

    a(k) = 14            '插入数14

    For i = 0 To 10

        Print a(i);

    Next i

    Print

End Sub

 

2)       

1       4      7      10     13    16     19     22     25    28

K

删除

代码如下:

Dim a() as integer

….

ReDim a(1 to n)

For i=k+1 to n

a(i-1)=a(i)

Next I

Redim preserve a(1 to n-1)

 

2、实战练习

1)        补充代码(2001秋二(8))

         C盘根目录下文件Data4.txt的内容是:24681013579。下面程序的功能是将文件后半部分的奇数分别按序插入到前半部分的适当位置,得到的新数列是:1 2 3 4 5 6 7 8 9 10。(实现方法:第一次调整后的数列是:1 2 4 6 8 10 3 5 7 9第二次调整后的数列是:1 2 3 4 6 8 10 5 7 9)。

 

                  Option Explict

                  Private Sub Form_Click()

                           Dim A(10) As Integer,i As Integer,J As Integer

                           Open "c:\Data4.txt" For Input As #12

                           Do   1 

                                    J=J+1

                                    Input #12,A(J)

                           Loop

                           Call Insert(A)

                           For i=1 To 10

                                    Print A(i);

                           Next i

                           Print

                           Close #12

                  End Sub

 

                  Private Sub Insert(A() As Integer)

                           Dim i As Integer,Putp As Integer,J As Integer

                           Dim Getp As Integer,N As Integer,Tem As Integer

                           N=UBound(A)/2

                           Putp=1

                           Getp=N+1

                           For i=1 To N

                                    Tem=A(Getp)

                                    For J=Getp To Putp +1 Step -1

                                               2 

                                    Next J

                                    A(Putp)=Tem

                                    Getp=Getp+1

                                    Putp=  3  

                           Next i

                  End Sub

 

2)        补充代码(2001春二(8))

         下面程序得功能是将无序数组中相同得数只保留一个,其余得删除,并输出经过删除后的数组元素,删除相同数是通过将该数组元素后面的元素在数组内依次前移替换前一个元素的值实现的。数组各元素的值从文件data.txt中读取。

 

                  Option Explict

                  Option Base 1

                  Private Sub Form_Click()

                           Dim I As Integer,J As Integer,K As Integer

                           Dim A()As Integer,T As Integer,M As Integer

                           Open "c:\my documents\2000test\data.txt" For Input As #1

                           Do While  1 

                                    I=I+1

                                    Redim Preserve A(I)

                                    Input #1,A(I)

                           Loop

                           M=1:T=  2 

                           Do While M

                                    I=M+1

                                    Do While I<=T

                                             If A(I)=A(M)Then

                                                      For J=1 To   3 

                                                               A(J)=A(J+1)

                                                      Next J

                                                      T=T-1

                                             Else

                                                      I=  4 

                                             End If

                                    Loop

                                    M=M+1

                           Loop

                           Redim Preserve A(T)

                           For I=1 To T

                                    Print A(I);

                           Next I

                           Print

                  End Sub