Generic Stack : Generic Stack « Generics « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Generics » Generic Stack 
9.5.1.Generic Stack
Module StackTest
   Dim doubleElements() As Double = {1.12.23.34.45.56.6}
   Dim integerElements() As Integer = {1234567891011}

   Dim doubleStack As Stack(Of Double)
   Dim integerStack As Stack(Of Integer)

   Sub Main()
      doubleStack = New Stack(Of Double)(5)
      integerStack = New Stack(Of Integer)(10)

      TestPushDouble()
      TestPopDouble() 
      TestPushInteger()
      TestPopInteger() 
   End Sub 

   Sub TestPushDouble()
      Try
         Console.WriteLine("Pushing elements onto doubleStack")

         For Each element As Double In doubleElements
            Console.Write("{0:F1} ", element)
            doubleStack.Push(element)
         Next element
      Catch exception As GenericStackException
         Console.Error.WriteLine("Message: " & exception.Message)
         Console.Error.WriteLine(exception.StackTrace)
      End Try
   End Sub

   Sub TestPopDouble()
      Try
         Console.WriteLine("Popping elements from doubleStack")
         Dim popValue As Double

         While True
            popValue = doubleStack.Pop()
            Console.Write("{0:F1} ", popValue)
         End While
      Catch exception As GenericStackException
         Console.Error.WriteLine()
         Console.Error.WriteLine("Message: " & exception.Message)
         Console.Error.WriteLine(exception.StackTrace)
      End Try
   End Sub

   Sub TestPushInteger()
      Try
         Console.WriteLine("Pushing elements onto integerStack")
         For Each element As Integer In integerElements
            Console.Write("{0} ", element)
            integerStack.Push(element' push onto integerStack
         Next element
      Catch exception As GenericStackException
         Console.Error.WriteLine()
         Console.Error.WriteLine("Message: " & exception.Message)
         Console.Error.WriteLine(exception.StackTrace)
      End Try
   End Sub
   
   Sub TestPopInteger()
      Try
         Console.WriteLine("Popping elements from integerStack")
         Dim popValue As Integer

         While True
            popValue = integerStack.Pop()
            Console.Write("{0} ", popValue)
         End While
      Catch exception As GenericStackException
         Console.Error.WriteLine()
         Console.Error.WriteLine("Message: " & exception.Message)
         Console.Error.WriteLine(exception.StackTrace)
      End Try
   End Sub
End Module

Public Class Stack(Of E)
   Private top As Integer
   Private elements() As E

   Public Sub New()
      MyClass.New(10)
   End Sub ' New

   Public Sub New(ByVal stackSize As Integer)
      If stackSize > Then 
         elements = New E(stackSize - 1) {}
      Else
         elements = New E(9) {}
      End If

      top = -1
   End Sub ' New

   Public Sub Push(ByVal pushValue As E)
      If top = elements.Length - Then
         Throw New GenericStackException(String.Format("Stack is full, cannot push {0}", pushValue))
      End If

      top += 1
      elements(top= pushValue 
   End Sub

   Public Function Pop() As E
      If top = -Then
         Throw New GenericStackException("Stack is empty, cannot pop")
      End If

      top -= 1
      Return elements(top + 1
   End Function 
End Class


Public Class GenericStackException : Inherits ApplicationException
   Public Sub New()
      MyBase.New("Stack is empty")
   End Sub ' New

   Public Sub New(ByVal exception As String)
      MyBase.New(exception)
   End Sub
End Class
Pushing elements onto doubleStack
1.1 2.2 3.3 4.4 5.5 6.6 Message: Stack is full, cannot push 6.6
   at Stack`1.Push(E pushValue)
   at StackTest.TestPushDouble()
Popping elements from doubleStack
5.5 4.4 3.3 2.2 1.1
Message: Stack is empty, cannot pop
   at Stack`1.Pop()
   at StackTest.TestPopDouble()
Pushing elements onto integerStack
1 2 3 4 5 6 7 8 9 10 11
Message: Stack is full, cannot push 11
   at Stack`1.Push(E pushValue)
   at StackTest.TestPushInteger()
Popping elements from integerStack
10 9 8 7 6 5 4 3 2 1
Message: Stack is empty, cannot pop
   at Stack`1.Pop()
   at StackTest.TestPopInteger()
9.5.Generic Stack
9.5.1.Generic Stack
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.