VB.Net数据结构系列 第3章 栈和队列 3.2 队列

1、构造函数之间相互调用

Me.New(别的带参数的构造函数的重载版本)   

Code3-2

Queue.vb:

Public Class Queue
    Private queueArray() As Object
    Private growFactor As Integer
    Private head As Integer
    Private Const MinimumGrow As Integer = 4
    Private Const ShrinkThreshold As Integer = &H20 '初始容量可以存放32个元素
    Private size As Integer
    Private tail As Integer

    Public ReadOnly Property count As Integer
        Get
            Return size
        End Get
    End Property

    Sub New()
        Me.New(ShrinkThreshold, 2.0F)
    End Sub

    Sub New(ByVal capacity As Integer, ByVal growFactor As Single)
        If capacity < 0 Then
            Throw New ArgumentOutOfRangeException("capacity", "初始容量不能小于0")
        End If
        If (growFactor < 1.0 Or growFactor > 10.0) Then
            Throw New ArgumentOutOfRangeException("groFactor", "增长因子必须在1到10之间")
        End If
        queueArray = New Object(capacity) {}
        head = 0
        tail = 0
        size = 0
        Me.growFactor = Integer.Parse(growFactor * 100.0F)
    End Sub

    '出队
    Public Function Dequeue() As Object
        If size = 0 Then
            Throw New InvalidOperationException("队列为空")
        End If
        Dim obj2 As Object = queueArray(head)
        queueArray(head) = Nothing
        head = (head + 1) Mod queueArray.Length
        size -= 1
        Return obj2
    End Function

    '入队
    Public Sub Enqueue(ByVal obj As Object)
        If size = queueArray.Length Then
            Dim capacity As Integer
            capacity = Integer.Parse((queueArray.Length * growFactor) / 100L)
            If capacity < (queueArray.Length + MinimumGrow) Then
                capacity = queueArray.Length + MinimumGrow
            End If

            Call SetCapacity(capacity)
        End If
        queueArray(tail) = obj
        tail = (tail + 1) Mod queueArray.Length
        size += 1
    End Sub

    Private Sub SetCapacity(ByVal capacity As Integer)
        Dim destinationArray() As Object = New Object(capacity) {}
        If head < tail Then
            Array.Copy(queueArray, head, destinationArray, 0, size)
        Else
            Array.Copy(queueArray, head, destinationArray, 0, queueArray.Length - head)
            Array.Copy(queueArray, 0, destinationArray, queueArray.Length - head, tail)
        End If
        queueArray = destinationArray
        head = 0
        tail = IIf(size = capacity, 0, size)
    End Sub
End Class

Module1.vb:

Module Module1

    Sub Main()
        Dim newQueue As New Queue
        Dim left As Integer = 0
        Dim right As Integer = 0
        Console.Write("请输入行数:")
        Dim n As Integer = Integer.Parse(Console.ReadLine)
        For i As Integer = 0 To n - 1
            For j As Integer = 0 To n - 1 - 1
                Console.Write(" ")
            Next
            For k As Integer = 0 To i
                Dim num As Integer = 1
                If k <> i Then
                    right = CType(newQueue.Dequeue, Integer)
                    If k <> 0 Then
                        num = left + right
                    End If
                    left = right
                End If
                Console.Write(String.Format("{0,-4}", num.ToString))
                newQueue.Enqueue(num)
            Next
            Console.WriteLine()
        Next
        Console.ReadKey()
    End Sub

End Module

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

.Net学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值