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

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. Stream File
13. GUI
14. GUI Applications
15. 2D Graphics
16. I18N Internationlization
17. Reflection
18. Regular Expressions
19. Security
20. Socket Network
21. Thread
22. Windows
23. XML
24. Database ADO.net
25. Design Patterns
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial » Generics » Generic Stack 
9. 4. 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. 4. Generic Stack
9. 4. 1. Generic Stack
w_w__w._ja_v___a_2s__.__c___o__m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.