读书人

用VB2005写了一个循环队列请帮忙看看

发布时间: 2012-01-01 23:10:55 作者: rapoo

用VB2005写了一个循环队列,请帮忙看看有什么需要改进的
Imports System.Text

Public Class UQueue

Structure myList
Dim sMsg As String
End Structure

Const StringLength As Integer = 250
Public nHead As Integer '头指针
Public nTrail As Integer '尾指针
Public nSize As Integer '队列长
Public mList() As myList
Dim uCount As Integer '队列元素个数
Dim Temp As Integer

' ' ' <summary>
' ' ' 构造函数
' ' ' </summary>
' ' ' <param name= "QueueLength "> 循环队列长度 </param>
' ' ' <remarks> </remarks>
Public Sub New(ByVal QueueLength As Integer)
Dim I As Integer
nSize = QueueLength
ReDim mList(nSize)
For I = 0 To nSize - 2
mList(I).sMsg = " "
Next
mList(nSize - 1).sMsg = " "
nHead = 0
nTrail = -1
End Sub

' ' ' <summary>
' ' ' 获取或编辑队列元素
' ' ' </summary>
' ' ' <param name= "FigureIndex "> 从head开始计数 </param>
' ' ' <value> </value>
' ' ' <returns> 返回队列中的元素 </returns>
' ' ' <remarks> 区别于传统循环队列的新属性 </remarks>
Public Property Items(ByVal FigureIndex As Integer) As String
Get
If FigureIndex > = Count() Then
Return "Break "
Else
If nHead + FigureIndex <= nSize - 1 Then


Return mList(nHead + FigureIndex).sMsg.ToString
Else
Return mList(FigureIndex - (nSize - _ nHead)).sMsg.ToString
End If
End If
End Get
Set(ByVal value As String)
If FigureIndex > = nSize Then
Else
If nHead + FigureIndex <= nSize - 1 Then
mList(nHead + FigureIndex).sMsg = value
Else
mList(FigureIndex - (nSize - nHead)).sMsg = value
End If
End If
End Set
End Property

' ' ' <summary>
' ' ' 弹出队列头元素
' ' ' </summary>
' ' ' <returns> 循环队列头元素,先入先出 </returns>
' ' ' <remarks> 类似堆栈 </remarks>
Public Function Pop() As String
Dim nTemp As Integer
If Count() = 0 Then
MessageBox.Show( "空 ")
Else
nTemp = nHead
nHead += 1
If nHead > nSize - 1 Then


nHead = 0
End If
uCount -= 1
If Count() <= 0 Then
uCount = 0
End If
End If
Return mList(nTemp).sMsg
End Function

' ' ' <summary>
' ' ' 推入队列元素
' ' ' </summary>
' ' ' <param name= "sMsg "> 元素 </param>
' ' ' <remarks> 从队列尾部插入 </remarks>
Public Sub Push(ByVal sMsg As String)
If Count() = 0 Then
nTrail += 1
If nTrail > nSize - 1 Then
nTrail = 0
End If
mList(nTrail).sMsg = sMsg
Else
nTrail += 1
If nTrail > nSize - 1 Then
nTrail = 0
End If
mList(nTrail).sMsg = sMsg
If nTrail = nHead Then
nHead += 1
If nHead > nSize - 1 Then
nHead = 0
End If


End If
End If
uCount += 1
If Count() > = nSize Then
uCount = nSize
End If
End Sub

' ' ' <summary>
' ' ' 返回队列是否为空
' ' ' </summary>
' ' ' <returns> </returns>
' ' ' <remarks> </remarks>
Public Function isEmpty() As Boolean
If Count() = 0 Then
Return True
Else
Return False
End If
End Function

' ' ' <summary>
' ' ' 返回队列有效元素总个数
' ' ' </summary>
' ' ' <returns> </returns>
' ' ' <remarks> </remarks>
Public Function Count()
Return uCount
End Function

End Class



[解决办法]
新手帮顶

读书人网 >VB Dotnet

热点推荐