0
Dim n, front, rear As Integer
Dim x As Integer
Dim arr() As Integer

Public Function init()
  n = InputBox("Enter size :")
  ReDim arr(n) As Integer
  front = 0
  rear = -1
End Function

Public Function insert(x As Integer)
  If rear = n-1 Then
    MsgBox "queue FULL !!!", vbOKOnly, "QUEUE"
  Else
    rear = rear + 1
    arr(rear) = x
    MsgBox x, vbOKOnly, "INSERTED"
  End If
End Function

Public Function delete() As Integer
  If rear + 1 = front Then
    MsgBox "queue Empty !!!", vbOKOnly, "QUEUE"
  Else
    x = arr(front)
    front = front + 1
    Return x
  End If
End Function

Private Sub inser_Click()
  If rear < n Then
    x = InputBox("Enter element :")
    Call insert(x)
  Else
    MsgBox "queue FULL !!!", vbOKOnly, "QUEUE"
  End If
End Sub

Private Sub del_Click()
  x = delete()
  MsgBox x, vbOKOnly, "DELETED"
End Sub

Private Sub Exit_Click()
  End
End Sub

Private Sub Form_Load()
  Call init
End Sub

这是我在 VB6 中的代码。我在显示“预期编译器错误:语句结束”insert的行中出现函数错误Return x

另一个错误是,每当我尝试删除队列的元素时,它都会显示“0 DELETED”

4

1 回答 1

6

您正在尝试使用 Return 语句从函数返回值,这在 VB6 中无效。在 VB6 中,您通过将返回值分配给函数的名称来返回函数的值。

因此,对于您的delete功能,您可以这样写:

Public Function delete() As Integer
    If rear + 1 = front Then
        MsgBox "queue Empty !!!", vbOKOnly, "QUEUE"
    Else
        x = arr(front)
        front = front + 1
        delete = x    ' <-- returning x here.
    End If
End Function

看看你的其他函数,它们根本没有明确返回值。

看看这个可能会有所帮助,它概述了 Subs 和 Functions 在 VB6 中的工作方式。

于 2012-05-10T17:18:33.447 回答