在VB6中实现队列

ric*_*ick 0 vb6 queue

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
Run Code Online (Sandbox Code Playgroud)

这是我在VB6中的代码.我在insert函数中遇到错误Return x,它表示"编译器错误预期:语句结束"

还有一个错误是每当我尝试删除队列中的元素时,它会显示"0 DELETED"

Jay*_*ggs 6

您试图通过使用Return语句从函数返回值,该语句在VB6中无效.在VB6中,通过将返回值赋给函数名称来返回Function的值.

所以对于你的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
Run Code Online (Sandbox Code Playgroud)

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

查看此内容可能会有所帮助,它概述了Subs和Functions在VB6中的工作方式.