Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Collections
- Imports System.Collections.Generic
- ''' <summary>
- ''' Простая реализация стека на основе массива.
- ''' </summary>
- ''' <typeparam name="T">Тип элемента</typeparam>
- Public NotInheritable Class CustomStack(Of T)
- Public Sub New(capacity As Int32)
- arr = New T(capacity - 1) {}
- lastIndex = -1
- End Sub
- Public Sub New()
- Me.New(0)
- End Sub
- Public Function Peek() As T
- If lastIndex > -1 Then
- Return arr(lastIndex)
- Else
- Throw New InvalidOperationException("Stack empty.")
- End If
- End Function
- Public Sub Push(value As T)
- lastIndex += 1
- If arr.Length < lastIndex + 1 Then
- Me.Capacity = IIf(lastIndex > 0, lastIndex * 2, 1)
- End If
- arr(lastIndex) = value
- End Sub
- Public Function Pop() As T
- If lastIndex > -1 Then
- lastIndex -= 1
- Return arr(lastIndex + 1)
- Else
- Throw New InvalidOperationException("Stack empty.")
- End If
- End Function
- Public ReadOnly Property Count() As Integer
- Get
- Return lastIndex + 1
- End Get
- End Property
- Public Property Capacity() As Integer
- Get
- Return arr.Length
- End Get
- Set
- If Value >= Count Then
- Array.Resize(Of T)(arr, Value)
- Else
- Throw New ArgumentOutOfRangeException("Capacity was less than the current size.")
- End If
- End Set
- End Property
- Private arr As T()
- Private lastIndex As Integer
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement